|
@@ -1,78 +1,109 @@
|
|
|
-import React, { useMemo, useState } from 'react';
|
|
|
+import React, { useState } from 'react';
|
|
|
import { View } from 'react-native';
|
|
|
-import CalendarPicker, {
|
|
|
- CustomDatesStylesFunc,
|
|
|
- CustomDayHeaderStylesFunc
|
|
|
-} from 'react-native-calendar-picker';
|
|
|
import moment from 'moment';
|
|
|
import { Modal } from '../../Modal';
|
|
|
-import Navigation from './Navigation';
|
|
|
|
|
|
import { styles } from './style';
|
|
|
import { Colors } from '../../../theme';
|
|
|
+import { Calendar } from 'react-native-calendars';
|
|
|
|
|
|
export default function RangeCalendar({
|
|
|
isModalVisible,
|
|
|
closeModal,
|
|
|
- allowRangeSelection = true
|
|
|
+ allowRangeSelection = true,
|
|
|
+ disableFutureDates = false,
|
|
|
}: {
|
|
|
isModalVisible: boolean;
|
|
|
- closeModal: (selectedDate: Date | null) => void;
|
|
|
+ closeModal: (startDate: Date | null, endDate: Date | null) => void;
|
|
|
allowRangeSelection?: boolean;
|
|
|
+ disableFutureDates?: boolean;
|
|
|
}) {
|
|
|
const [selectedStartDate, setSelectedStartDate] = useState<Date | null>(null);
|
|
|
const [selectedEndDate, setSelectedEndDate] = useState<Date | null>(null);
|
|
|
|
|
|
- const handleOnDateChange = (date: Date, type: string) => {
|
|
|
- if (type === 'END_DATE') {
|
|
|
- setSelectedEndDate(date);
|
|
|
- } else {
|
|
|
- setSelectedStartDate(date);
|
|
|
- setSelectedEndDate(null);
|
|
|
- }
|
|
|
+ const customThemeStyles = {
|
|
|
+ textSectionTitleColor: Colors.ORANGE,
|
|
|
+ todayTextColor: Colors.DARK_BLUE,
|
|
|
+ dayTextColor: Colors.DARK_BLUE,
|
|
|
+ textDisabledColor: Colors.LIGHT_GRAY,
|
|
|
+ dotColor: Colors.DARK_BLUE,
|
|
|
+ arrowColor: Colors.DARK_BLUE,
|
|
|
+ monthTextColor: Colors.DARK_BLUE,
|
|
|
+ indicatorColor: Colors.DARK_BLUE,
|
|
|
+ textDayFontWeight: 'normal',
|
|
|
+ textMonthFontWeight: 'bold',
|
|
|
+ textDayHeaderFontWeight: 'bold',
|
|
|
+ textDayFontSize: 14,
|
|
|
+ textMonthFontSize: 14,
|
|
|
+ textDayHeaderFontSize: 12,
|
|
|
+ selectedDayBackgroundColor: Colors.ORANGE,
|
|
|
+ 'stylesheet.calendar.header': {
|
|
|
+ header: styles.calendarHeader
|
|
|
+ },
|
|
|
};
|
|
|
|
|
|
- const customDayHeaderStyles: CustomDayHeaderStylesFunc = () => {
|
|
|
- return {
|
|
|
- textStyle: styles.dayHeader
|
|
|
- };
|
|
|
+ const onDayPress = (day: any) => {
|
|
|
+ if (!allowRangeSelection) {
|
|
|
+ setSelectedStartDate(day.dateString);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!selectedStartDate || (selectedStartDate && selectedEndDate)) {
|
|
|
+ setSelectedStartDate(day.dateString);
|
|
|
+ setSelectedEndDate(null);
|
|
|
+ } else if (!selectedEndDate) {
|
|
|
+ if (day.dateString < selectedStartDate) {
|
|
|
+ setSelectedEndDate(selectedStartDate);
|
|
|
+ setSelectedStartDate(day.dateString);
|
|
|
+ } else {
|
|
|
+ setSelectedEndDate(day.dateString);
|
|
|
+ }
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
- const customSelectedDatesStyles: CustomDatesStylesFunc = useMemo(() => {
|
|
|
- return (date: moment.Moment) => {
|
|
|
- if (date.isSame(moment(), 'day')) {
|
|
|
- return {
|
|
|
- containerStyle: {},
|
|
|
- textStyle: {
|
|
|
- borderWidth: 1,
|
|
|
- borderColor: Colors.DARK_BLUE,
|
|
|
- borderRadius: 17,
|
|
|
- height: 34,
|
|
|
- width: 34,
|
|
|
- textAlign: 'center',
|
|
|
- verticalAlign: 'middle'
|
|
|
- },
|
|
|
- style: {
|
|
|
- backgroundColor: Colors.WHITE
|
|
|
- }
|
|
|
- };
|
|
|
+ const markedDates = (() => {
|
|
|
+ const marked: { [key: string]: any } = {};
|
|
|
+ let start = selectedStartDate as unknown as string;
|
|
|
+ let end = selectedEndDate as unknown as string;
|
|
|
+ if (disableFutureDates) {
|
|
|
+ const today = moment().add(1, 'day');
|
|
|
+ const lastDay = moment().add(2, 'years');
|
|
|
+ while (today.isBefore(lastDay, 'day')) {
|
|
|
+ const dateString = today.format('YYYY-MM-DD');
|
|
|
+ if (!marked[dateString]) {
|
|
|
+ marked[dateString] = { disableTouchEvent: true, disabled: true };
|
|
|
+ }
|
|
|
+ today.add(1, 'day');
|
|
|
}
|
|
|
- return {
|
|
|
- containerStyle: {},
|
|
|
- textStyle: {}
|
|
|
+ }
|
|
|
+ if (start && end) {
|
|
|
+ marked[start] = { startingDay: true, color: Colors.ORANGE, textColor: 'white' };
|
|
|
+ let day = start;
|
|
|
+ while (day < end) {
|
|
|
+ day = moment(day).add(1, 'days').format('YYYY-MM-DD');
|
|
|
+ if (day === end) {
|
|
|
+ marked[day] = { endingDay: true, color: Colors.ORANGE, textColor: 'white' };
|
|
|
+ } else {
|
|
|
+ marked[day] = { color: 'transparent', textColor: Colors.DARK_BLUE };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (start) {
|
|
|
+ marked[start] = {
|
|
|
+ selected: true,
|
|
|
+ startingDay: true,
|
|
|
+ endingDay: true,
|
|
|
+ color: Colors.ORANGE,
|
|
|
+ textColor: 'white',
|
|
|
};
|
|
|
- };
|
|
|
- }, []);
|
|
|
+ }
|
|
|
+ return marked;
|
|
|
+ })();
|
|
|
|
|
|
const resetSelections = () => {
|
|
|
- closeModal(selectedStartDate);
|
|
|
+ closeModal(selectedStartDate, selectedEndDate);
|
|
|
setSelectedStartDate(null);
|
|
|
setSelectedEndDate(null);
|
|
|
};
|
|
|
|
|
|
- const prevNavigationComponent = useMemo(() => <Navigation direction="prev" />, []);
|
|
|
- const nextNavigationComponent = useMemo(() => <Navigation direction="next" />, []);
|
|
|
-
|
|
|
return (
|
|
|
<Modal
|
|
|
visibleInPercent={'auto'}
|
|
@@ -81,31 +112,15 @@ export default function RangeCalendar({
|
|
|
headerTitle="Select Date"
|
|
|
>
|
|
|
<View style={styles.modalContent}>
|
|
|
- <CalendarPicker
|
|
|
- scaleFactor={400}
|
|
|
- allowRangeSelection={allowRangeSelection}
|
|
|
- allowBackwardRangeSelect
|
|
|
- onDateChange={handleOnDateChange as any}
|
|
|
- selectedStartDate={selectedStartDate as Date}
|
|
|
- selectedEndDate={selectedEndDate as Date}
|
|
|
- scrollable
|
|
|
- showDayStragglers
|
|
|
- previousComponent={prevNavigationComponent}
|
|
|
- nextComponent={nextNavigationComponent}
|
|
|
- dayLabelsWrapper={styles.labelsWrapper}
|
|
|
- selectedRangeStyle={styles.rangeStyle}
|
|
|
- selectedRangeEndTextStyle={styles.rangeStartEndTextStyle}
|
|
|
- selectedRangeStartTextStyle={styles.rangeStartEndTextStyle}
|
|
|
- selectedRangeEndStyle={[styles.rangeStartEndStyle, styles.rangeEndStyle]}
|
|
|
- selectedRangeStartStyle={[styles.rangeStartEndStyle, styles.rangeStartStyle]}
|
|
|
- selectedDayTextColor={Colors.WHITE}
|
|
|
- customDayHeaderStyles={customDayHeaderStyles}
|
|
|
- customDatesStyles={customSelectedDatesStyles}
|
|
|
- disabledDatesTextStyle={styles.disabledDates}
|
|
|
- textStyle={styles.textStyle}
|
|
|
- monthTitleStyle={styles.dateTitle}
|
|
|
- yearTitleStyle={styles.dateTitle}
|
|
|
- headerWrapperStyle={styles.headerWrapper}
|
|
|
+ <Calendar
|
|
|
+ onDayPress={onDayPress}
|
|
|
+ markingType={'period'}
|
|
|
+ markedDates={markedDates}
|
|
|
+ enableSwipeMonths={true}
|
|
|
+ firstDay={1}
|
|
|
+ theme={{
|
|
|
+ ...customThemeStyles,
|
|
|
+ }}
|
|
|
/>
|
|
|
</View>
|
|
|
</Modal>
|