import React, {useMemo, useCallback} from 'react'; import dayjs from 'dayjs'; import {FlatList, View, ActivityIndicator, FlatListProps} from 'react-native'; // components import Month from './Month'; // data import {getMonths} from './utils/data'; // types import {Month_Type} from './utils/data'; import {LOCALE_TYPE} from './utils/locale'; import {Style} from './index'; interface Props { pastYearRange: number; futureYearRange: number; initialNumToRender: number; locale: LOCALE_TYPE; handlePress: (date: string) => void; startDate: string | null; endDate: string | null; style?: Style; flatListProps?: FlatListProps; isMonthFirst?: boolean; disabledBeforeToday?: boolean; disabledAfterToday?: boolean; } const LAYOUT_HEIGHT = 370; const CalendarList = ({ pastYearRange, futureYearRange, initialNumToRender, locale, handlePress, startDate, endDate, flatListProps, isMonthFirst, disabledBeforeToday, disabledAfterToday, style, }: Props) => { const months: Month_Type[] = useMemo( () => getMonths(pastYearRange, futureYearRange), [pastYearRange, futureYearRange], ); const getInitialIndex = useCallback(() => { return months.findIndex((month: Month_Type) => { const targetDate = startDate ? dayjs(startDate) : dayjs(); return month.id === targetDate.format('YYYY-MM'); }); }, []); const handleRenderItem = useCallback( ({item}: {item: Month_Type}) => ( ), [locale.today, startDate, endDate], ); return ( item.id} data={months} renderItem={handleRenderItem} getItemLayout={(_, index) => ({ length: LAYOUT_HEIGHT, offset: LAYOUT_HEIGHT * index, index, })} initialScrollIndex={getInitialIndex()} initialNumToRender={initialNumToRender} {...flatListProps} /> ); }; export default CalendarList;