index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {
  2. View
  3. } from "react-native";
  4. import type IDateTimeSheetProps from "./type";
  5. import stylesheet, {
  6. useStyles
  7. } from "./stylesheet";
  8. import {
  9. NCoreUIKitLocalize,
  10. NCoreUIKitTheme
  11. } from "../../core/hooks";
  12. import DateSelector from "../dateSelector";
  13. import BottomSheet from "../bottomSheet";
  14. import CheckBox from "../checkBox";
  15. import Loading from "../loading";
  16. import Button from "../button";
  17. import Text from "../text";
  18. const DateTimeSheet = ({
  19. localeBasedFirstDayOfWeek = true,
  20. isShowTodayIndicator = true,
  21. dayOfWeekLength = "short",
  22. LoadingIconComponentProp,
  23. selectMultipleObject,
  24. isWorkWithRealtime,
  25. isShowTools = true,
  26. dateTimePickerKey,
  27. isMultipleSelect,
  28. bottomSheetProps,
  29. isWorkWithRRule,
  30. selectDateRule,
  31. bottomSheetRef,
  32. customLocalize,
  33. setIsLoading,
  34. selectObject,
  35. setDateRange,
  36. setDateRule,
  37. customTheme,
  38. setIsActive,
  39. pickerType,
  40. dateRange,
  41. maxChoice,
  42. minChoice,
  43. isLoading,
  44. isActive,
  45. dateRule,
  46. setDate,
  47. variant,
  48. cancel,
  49. clean,
  50. title,
  51. date,
  52. ok
  53. }: IDateTimeSheetProps) => {
  54. const {
  55. spaces
  56. } = NCoreUIKitTheme.useContext(customTheme);
  57. const {
  58. localize
  59. } = NCoreUIKitLocalize.useContext(customLocalize);
  60. const {
  61. loadingContainer: loadingContainerDynamicStyle,
  62. contentContainer: contentContainerDynamicStyle,
  63. headerContainer: headerContainerDynamicStyle,
  64. bottomContainer: bottomContainerDynamicStyle,
  65. toolsContainer: toolsContainerDynamicStyle,
  66. cancelButton: cancelButtonDynamicStyle,
  67. headerTitle: headerTitleDynamicStyle,
  68. okButton: okButtonDynamicStyle
  69. } = useStyles({
  70. spaces
  71. });
  72. const renderBottomSheetContent = () => {
  73. if(isLoading) {
  74. return <View
  75. style={[
  76. stylesheet.loadingContainer,
  77. loadingContainerDynamicStyle
  78. ]}
  79. >
  80. {LoadingIconComponentProp ? <LoadingIconComponentProp
  81. color="emphasized"
  82. size={20}
  83. /> : <Loading/>}
  84. </View>;
  85. }
  86. return <View
  87. style={[
  88. contentContainerDynamicStyle
  89. ]}
  90. >
  91. <DateSelector
  92. localeBasedFirstDayOfWeek={localeBasedFirstDayOfWeek}
  93. isShowTodayIndicator={isShowTodayIndicator}
  94. dayOfWeekLength={dayOfWeekLength}
  95. dateRange={dateRange}
  96. dateRule={dateRule}
  97. variant={variant}
  98. date={date}
  99. />
  100. </View>;
  101. };
  102. const renderTools = () => {
  103. if(!isShowTools) {
  104. return null;
  105. }
  106. if(minChoice && maxChoice !== -1) {
  107. return null;
  108. }
  109. let isAnySelected = false;
  110. if(variant === "single" && date) {
  111. isAnySelected = true;
  112. }
  113. if(variant === "range" && dateRange && (dateRange.start || dateRange.end)) {
  114. isAnySelected = true;
  115. }
  116. if(variant === "rrule" && dateRule) {
  117. isAnySelected = true;
  118. }
  119. return <View
  120. style={[
  121. stylesheet.toolsContainer,
  122. toolsContainerDynamicStyle
  123. ]}
  124. >
  125. {!minChoice ? <CheckBox
  126. isChecked={isAnySelected ? "checked" : null}
  127. title={localize("clean-all")}
  128. spreadBehaviour="free"
  129. onPress={() => {
  130. clean();
  131. }}
  132. /> : null}
  133. </View>;
  134. };
  135. const renderBottomSheetHeader = () => {
  136. return <View
  137. style={[
  138. stylesheet.headerContainer,
  139. headerContainerDynamicStyle
  140. ]}
  141. >
  142. {title ? <Text
  143. variant="titleMediumSize"
  144. style={[
  145. headerTitleDynamicStyle
  146. ]}
  147. >
  148. {title}
  149. </Text> : null}
  150. {renderTools()}
  151. </View>;
  152. };
  153. const renderBottomSheetBottom = () => {
  154. if(isWorkWithRealtime) {
  155. return null;
  156. }
  157. return <View
  158. style={[
  159. stylesheet.bottomContainer,
  160. bottomContainerDynamicStyle
  161. ]}
  162. >
  163. <Button
  164. title={localize("cancel")}
  165. spreadBehaviour="stretch"
  166. variant="outline"
  167. type="neutral"
  168. onPress={() => {
  169. cancel();
  170. }}
  171. style={{
  172. ...cancelButtonDynamicStyle
  173. }}
  174. />
  175. <Button
  176. spreadBehaviour="stretch"
  177. title={localize("ok")}
  178. onPress={() => {
  179. ok();
  180. }}
  181. style={{
  182. ...okButtonDynamicStyle
  183. }}
  184. />
  185. </View>;
  186. };
  187. return <BottomSheet
  188. {...bottomSheetProps}
  189. key={`${dateTimePickerKey}-bottomsheet`}
  190. customKey={dateTimePickerKey}
  191. ref={bottomSheetRef}
  192. isAutoHeight={true}
  193. isActive={isActive}
  194. renderBottom={() => {
  195. return renderBottomSheetBottom();
  196. }}
  197. renderHeader={() => {
  198. return renderBottomSheetHeader();
  199. }}
  200. onClose={() => {
  201. setIsActive(false);
  202. }}
  203. >
  204. {renderBottomSheetContent()}
  205. </BottomSheet>;
  206. };
  207. export default DateTimeSheet;