|
| 1 | +import { |
| 2 | + Calendar, |
| 3 | + toDateId, |
| 4 | + useDateRange, |
| 5 | +} from "@marceloterreiro/flash-calendar"; |
| 6 | +import type { Meta } from "@storybook/react"; |
| 7 | +import { addDays, format } from "date-fns"; |
| 8 | +import { useEffect, useState } from "react"; |
| 9 | +import { View } from "react-native"; |
| 10 | + |
| 11 | +const CalendarMeta: Meta<typeof Calendar> = { |
| 12 | + title: "Calendar/Github Issues", |
| 13 | + decorators: [], |
| 14 | +}; |
| 15 | + |
| 16 | +export default CalendarMeta; |
| 17 | + |
| 18 | +const today = toDateId(new Date()); |
| 19 | +const todayPlusFive = toDateId(addDays(new Date(), 5)); |
| 20 | + |
| 21 | +// See more: https://github.com/MarceloPrado/flash-calendar/issues/69 |
| 22 | +export const Issue69 = () => { |
| 23 | + // This state is simplified for the example. In my case it would come from a context. |
| 24 | + const [start, setStart] = useState<string | undefined>(today); |
| 25 | + const [end, setEnd] = useState<string | undefined>(todayPlusFive); |
| 26 | + |
| 27 | + const { onCalendarDayPress, isDateRangeValid, calendarActiveDateRanges } = |
| 28 | + useDateRange({ startId: start, endId: end }); // I am setting the default range here because I want the data from my context to be pre-selected here |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + // Every time the selected range changed I want to update my context (in this example the state) |
| 32 | + if (isDateRangeValid && calendarActiveDateRanges.length > 0) { |
| 33 | + const range = calendarActiveDateRanges[0]; |
| 34 | + setStart(range.startId); |
| 35 | + setEnd(range?.endId); |
| 36 | + } |
| 37 | + }, [calendarActiveDateRanges, isDateRangeValid]); |
| 38 | + |
| 39 | + return ( |
| 40 | + <View style={{ flex: 1 }}> |
| 41 | + <Calendar |
| 42 | + calendarActiveDateRanges={calendarActiveDateRanges} |
| 43 | + calendarColorScheme="light" |
| 44 | + calendarFirstDayOfWeek="monday" |
| 45 | + calendarMonthId={today} |
| 46 | + getCalendarWeekDayFormat={formatWeekDay} |
| 47 | + // getCalendarWeekDayFormat={(date) => format(date, "EEEEEE")} |
| 48 | + onCalendarDayPress={onCalendarDayPress} |
| 49 | + /> |
| 50 | + </View> |
| 51 | + ); |
| 52 | +}; |
| 53 | + |
| 54 | +const formatWeekDay = (date: Date) => format(date, "EEEEEE"); |
0 commit comments