import { MonthYearPickerDialogueAndroid, MonthYearPickerViewIOS, } from '@hero-design/react-native-month-year-picker'; import format from 'date-fns/fp/format'; import React, { useState } from 'react'; import { Platform, TouchableOpacity } from 'react-native'; import { useTheme } from '../../theme'; import { noop } from '../../utils/functions'; import Box from '../Box'; import ContentNavigator from '../ContentNavigator'; import Icon from '../Icon'; import Typography from '../Typography'; import CalendarRowItem from './CalendarRowItem'; import { StyledCalendarDayNameCell, StyledCalendarHeader, StyledCalendarRow, StyledCalendarRowItem, StyledContainer, StyledDisabledCalendarRowItem, } from './StyledCalendar'; import { getCalendarButtonState, getCalendarDate, isEqDate, shouldUseMonthPicker, } from './helpers'; import CalendarRange from './CalendarRange'; import { DAYS_OF_WEEK } from './constants'; import type { CalendarProps } from './types'; import { useCalendarLayout } from './shared/hooks/useCalendarLayout'; const Calendar = ({ value, visibleDate, onChange, onPreviousPress = noop, onNextPress = noop, onTitlePress = noop, minDate, maxDate, markedDates = [], testID, onMonthChange = noop, onToggleMonthPicker = noop, monthPickerConfirmLabel, monthPickerCancelLabel, }: CalendarProps) => { const theme = useTheme(); const { onLayout, contentHeight, contentWidth, calendarItemWidth } = useCalendarLayout(); const { parsedMaskedDate, daysOfPreviousMonth, daysOfCurrentMonth, daysOfNextMonth, } = getCalendarDate({ visibleDate, markedDates, minDate, maxDate, onMonthChange, }); const { disablePrevButton, disableNextButton } = getCalendarButtonState({ visibleDate, minDate, maxDate, }); const shouldShowMonthPicker = shouldUseMonthPicker(onMonthChange); const [monthPickerVisible, setMonthPickerVisible] = useState(false); const now = new Date(); return ( { onToggleMonthPicker?.(!monthPickerVisible); setMonthPickerVisible(!monthPickerVisible); }} > {format('MMMM yyyy', visibleDate)} ) } onPreviousPress={onPreviousPress} onNextPress={onNextPress} onPress={shouldShowMonthPicker ? undefined : onTitlePress} previousDisabled={disablePrevButton} nextDisabled={disableNextButton} fontSize="large" /> {Platform.OS === 'ios' && monthPickerVisible ? ( ) : ( {DAYS_OF_WEEK.map((day) => ( {day} ))} {daysOfPreviousMonth.map((date) => date ? ( onChange?.(date)} textIntent="subdued" marked={parsedMaskedDate[date.toDateString()]} itemWidth={contentWidth > 0 ? contentWidth / 7 : undefined} /> ) : ( ) )} {daysOfCurrentMonth.map((date) => date ? ( onChange?.(date)} marked={parsedMaskedDate[date.toDateString()]} /> ) : ( ) )} {daysOfNextMonth.map((date) => date ? ( onChange?.(date)} textIntent="subdued" marked={parsedMaskedDate[date.toDateString()]} /> ) : ( ) )} {Platform.OS === 'android' && monthPickerVisible && ( { setMonthPickerVisible(false); onToggleMonthPicker?.(false); if (action === 'dateSetAction' && !!date) { onMonthChange(date); } }} /> )} )} ); }; export default Object.assign(Calendar, { Range: CalendarRange, });