index.tsx 6.5 KB

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