index.tsx 5.9 KB

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