index.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import React, { useEffect, useState } from 'react';
  2. import { View } from 'react-native';
  3. import { Modal } from '../../Modal';
  4. import DateTimePicker, { DateType } from 'react-native-ui-datepicker';
  5. import dayjs from 'dayjs';
  6. import 'dayjs/locale/en';
  7. import calendar from 'dayjs/plugin/calendar';
  8. import updateLocale from 'dayjs/plugin/updateLocale';
  9. import localeData from 'dayjs/plugin/localeData';
  10. import customParseFormat from 'dayjs/plugin/customParseFormat';
  11. dayjs.locale('en');
  12. dayjs.extend(calendar);
  13. dayjs.extend(updateLocale);
  14. dayjs.extend(localeData);
  15. dayjs.extend(customParseFormat);
  16. import { styles } from './style';
  17. import { Colors } from '../../../theme';
  18. import { Button } from 'src/components/Button';
  19. import { ButtonVariants } from 'src/types/components';
  20. import Navigation from './Navigation';
  21. export default function RangeCalendar({
  22. isModalVisible,
  23. closeModal,
  24. allowRangeSelection = true,
  25. disableFutureDates = false,
  26. disablePastDates = false,
  27. highlightedDates,
  28. selectedDate,
  29. minDate: externalMinDate,
  30. maxDate: externalMaxDate,
  31. initialStartDate,
  32. initialEndDate
  33. }: {
  34. isModalVisible: boolean;
  35. closeModal: (startDate?: string | null, endDate?: string | null) => void;
  36. allowRangeSelection?: boolean;
  37. disableFutureDates?: boolean;
  38. disablePastDates?: boolean;
  39. highlightedDates?: string[];
  40. selectedDate?: string;
  41. minDate?: string | Date | dayjs.Dayjs;
  42. maxDate?: string | Date | dayjs.Dayjs;
  43. initialStartDate?: string | null;
  44. initialEndDate?: string | null;
  45. }) {
  46. const [selectedStartDate, setSelectedStartDate] = useState<string | null>(null);
  47. const [selectedEndDate, setSelectedEndDate] = useState<string | null>(null);
  48. const [singleDate, setSingleDate] = useState<DateType>(undefined);
  49. const [startDate, setStartDate] = useState<DateType>(undefined);
  50. const [endDate, setEndDate] = useState<DateType>(undefined);
  51. const computedMinDate = externalMinDate
  52. ? dayjs(externalMinDate)
  53. : highlightedDates && highlightedDates.length > 0
  54. ? dayjs(highlightedDates[0])
  55. : undefined;
  56. const computedMaxDate = externalMaxDate
  57. ? dayjs(externalMaxDate)
  58. : highlightedDates && highlightedDates.length > 0
  59. ? dayjs(highlightedDates[highlightedDates.length - 1])
  60. : undefined;
  61. useEffect(() => {
  62. if (allowRangeSelection) {
  63. if (initialStartDate) {
  64. setSelectedStartDate(initialStartDate);
  65. setStartDate(dayjs(initialStartDate));
  66. }
  67. if (initialEndDate) {
  68. setSelectedEndDate(initialEndDate);
  69. setEndDate(dayjs(initialEndDate));
  70. }
  71. } else if (selectedDate) {
  72. setSelectedStartDate(selectedDate);
  73. setSingleDate(dayjs(selectedDate));
  74. }
  75. }, [initialStartDate, initialEndDate, selectedDate, allowRangeSelection]);
  76. const getDisabledDates = (date: DateType) => {
  77. const dateString = dayjs(date).format('YYYY-MM-DD');
  78. if (disableFutureDates) {
  79. if (dayjs(date).isAfter(dayjs(), 'day')) {
  80. return true;
  81. }
  82. }
  83. if (disablePastDates) {
  84. if (dayjs(date).isBefore(dayjs(), 'day')) {
  85. return true;
  86. }
  87. }
  88. if (highlightedDates && highlightedDates.length > 0) {
  89. return !highlightedDates.includes(dateString);
  90. }
  91. return false;
  92. };
  93. const handleDateChange = (params: any) => {
  94. if (allowRangeSelection) {
  95. setStartDate(params.startDate);
  96. setEndDate(params.endDate);
  97. if (params.startDate) {
  98. setSelectedStartDate(dayjs(params.startDate).format('YYYY-MM-DD'));
  99. }
  100. if (params.endDate) {
  101. setSelectedEndDate(dayjs(params.endDate).format('YYYY-MM-DD'));
  102. } else {
  103. setSelectedEndDate(null);
  104. }
  105. } else {
  106. setSingleDate(params.date);
  107. setSelectedStartDate(dayjs(params.date).format('YYYY-MM-DD'));
  108. setSelectedEndDate(null);
  109. }
  110. };
  111. const resetSelections = () => {
  112. closeModal();
  113. setSelectedStartDate(null);
  114. setSelectedEndDate(null);
  115. setStartDate(undefined);
  116. setEndDate(undefined);
  117. setSingleDate(undefined);
  118. };
  119. const handleClose = () => {
  120. closeModal(selectedStartDate, selectedEndDate);
  121. setSelectedStartDate(null);
  122. setSelectedEndDate(null);
  123. setStartDate(undefined);
  124. setEndDate(undefined);
  125. setSingleDate(undefined);
  126. };
  127. return (
  128. <Modal
  129. visibleInPercent={'auto'}
  130. visible={isModalVisible}
  131. onRequestClose={resetSelections}
  132. headerTitle={allowRangeSelection ? 'Select Dates' : 'Select Date'}
  133. >
  134. <View style={styles.modalContent}>
  135. <DateTimePicker
  136. mode={allowRangeSelection ? 'range' : 'single'}
  137. date={singleDate}
  138. startDate={startDate}
  139. endDate={endDate}
  140. onChange={handleDateChange}
  141. minDate={computedMinDate}
  142. maxDate={computedMaxDate}
  143. disabledDates={getDisabledDates}
  144. firstDayOfWeek={1}
  145. timePicker={false}
  146. showOutsideDays={true}
  147. weekdaysFormat={'short'}
  148. components={{
  149. IconPrev: <Navigation direction="prev" />,
  150. IconNext: <Navigation direction="next" />
  151. }}
  152. styles={{
  153. header: {
  154. paddingBottom: 10
  155. },
  156. month_selector_label: {
  157. color: Colors.DARK_BLUE,
  158. fontWeight: 'bold',
  159. fontSize: 15
  160. },
  161. year_selector_label: {
  162. color: Colors.DARK_BLUE,
  163. fontWeight: 'bold',
  164. fontSize: 15
  165. },
  166. button_prev: {},
  167. button_next: {},
  168. weekday_label: {
  169. color: Colors.DARK_BLUE,
  170. fontWeight: 'bold',
  171. fontSize: 12
  172. },
  173. days: {},
  174. day_cell: {},
  175. day: {},
  176. day_label: {
  177. color: Colors.DARK_BLUE,
  178. fontSize: 14,
  179. fontWeight: 'normal'
  180. },
  181. today: {
  182. borderWidth: 1,
  183. borderRadius: 23,
  184. width: 46,
  185. height: 46,
  186. maxHeight: 46,
  187. borderColor: Colors.ORANGE
  188. },
  189. today_label: {
  190. color: Colors.DARK_BLUE,
  191. fontWeight: '500'
  192. },
  193. selected: {
  194. backgroundColor: Colors.ORANGE,
  195. borderRadius: 23,
  196. width: 46,
  197. height: 46,
  198. maxHeight: 46,
  199. marginVertical: 'auto',
  200. alignItems: 'center',
  201. justifyContent: 'center'
  202. },
  203. selected_label: {
  204. color: 'white',
  205. fontWeight: '500'
  206. },
  207. range_start: {
  208. backgroundColor: Colors.ORANGE,
  209. borderRadius: 23,
  210. width: 46,
  211. height: 46,
  212. alignItems: 'center',
  213. justifyContent: 'center'
  214. },
  215. range_start_label: {
  216. color: 'white'
  217. },
  218. range_end: {
  219. backgroundColor: Colors.ORANGE,
  220. borderRadius: 23,
  221. width: 46,
  222. height: 46,
  223. alignItems: 'center',
  224. justifyContent: 'center'
  225. },
  226. range_end_label: {
  227. color: 'white'
  228. },
  229. range_middle_label: {
  230. color: Colors.DARK_BLUE,
  231. fontWeight: '500'
  232. },
  233. range_fill: {
  234. backgroundColor: Colors.ORANGE,
  235. opacity: 0.2
  236. },
  237. disabled: {
  238. opacity: 0.3
  239. },
  240. disabled_label: {
  241. color: Colors.TEXT_GRAY
  242. },
  243. outside_label: {
  244. color: Colors.LIGHT_GRAY
  245. }
  246. }}
  247. />
  248. </View>
  249. <View style={styles.modalFooter}>
  250. <Button
  251. children="Done"
  252. onPress={handleClose}
  253. disabled={!selectedStartDate}
  254. variant={!selectedStartDate ? ButtonVariants.OPACITY : ButtonVariants.FILL}
  255. containerStyles={{ borderWidth: 0 }}
  256. />
  257. </View>
  258. </Modal>
  259. );
  260. }