From 3631367e147d786ac05fd6b677808f320d945d06 Mon Sep 17 00:00:00 2001 From: Lebaud Antoine Date: Thu, 15 Jun 2023 16:05:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(react)=20extend=20Calendar=20function?= =?UTF-8?q?alities=20with=20range=20selection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extend the functionality of the DatePicker component to include support for a range calendar. This enhancement allows users to select a date range spanning multiple calendar cells, enabling more flexible date selection. --- .../components/Forms/DatePicker/Calendar.tsx | 473 ++++++++++-------- .../Forms/DatePicker/CalendarGrid.tsx | 4 +- 2 files changed, 254 insertions(+), 223 deletions(-) diff --git a/packages/react/src/components/Forms/DatePicker/Calendar.tsx b/packages/react/src/components/Forms/DatePicker/Calendar.tsx index b099997..70330e2 100644 --- a/packages/react/src/components/Forms/DatePicker/Calendar.tsx +++ b/packages/react/src/components/Forms/DatePicker/Calendar.tsx @@ -1,4 +1,4 @@ -import React, { useMemo, useRef } from "react"; +import React, { forwardRef, Ref, useMemo, useRef } from "react"; import { CalendarDate, createCalendar, @@ -11,15 +11,24 @@ import { useDateFormatter, useLocale, } from "@react-aria/i18n"; -import { useCalendarState } from "@react-stately/calendar"; -import { useCalendar } from "@react-aria/calendar"; +import { + CalendarState, + RangeCalendarState, + useCalendarState, + useRangeCalendarState, +} from "@react-stately/calendar"; +import { + CalendarAria, + useCalendar, + useRangeCalendar, +} from "@react-aria/calendar"; import { useSelect, UseSelectReturnValue, UseSelectStateChange, } from "downshift"; import classNames from "classnames"; -import { CalendarProps } from "react-aria"; +import { CalendarProps, RangeCalendarProps } from "react-aria"; import { range } from ":/utils"; import { Button } from ":/components/Button"; import { CalendarGrid } from ":/components/Forms/DatePicker/CalendarGrid"; @@ -75,231 +84,253 @@ const DropdownValues = ({ options, downShift }: DropdownValuesProps) => ( ); -interface CalendarSubProps extends CalendarProps { +interface CalendarAuxProps extends CalendarAria { minYear?: number; maxYear?: number; + state: RangeCalendarState | CalendarState; } -export const Calendar = ({ - minYear = 1900, // in gregorian calendar. - maxYear = 2050, // in gregorian calendar. - ...props -}: CalendarSubProps) => { - const { locale } = useLocale(); - const { t } = useCunningham(); - const ref = useRef(null); +const CalendarAux = forwardRef( + ( + { + state, + minYear = 1900, // in gregorian calendar. + maxYear = 2050, // in gregorian calendar. + prevButtonProps, + nextButtonProps, + calendarProps, + }: CalendarAuxProps, + ref: Ref + ) => { + const { t } = useCunningham(); + const useTimeZoneFormatter = (formatOptions: DateFormatterOptions) => { + return useDateFormatter({ + ...formatOptions, + timeZone: state.timeZone, + }); + }; + + const monthItemsFormatter = useTimeZoneFormatter({ month: "long" }); + const selectedMonthItemFormatter = useTimeZoneFormatter({ month: "short" }); + const yearItemsFormatter = useTimeZoneFormatter({ year: "numeric" }); + + const monthItems: Array