import React, { PropsWithChildren, useMemo, useRef, RefAttributes, } from "react"; import { DateRangePickerState, DatePickerState, } from "@react-stately/datepicker"; import { DateRangePickerAria, DatePickerAria } from "@react-aria/datepicker"; import classNames from "classnames"; import { I18nProvider } from "@react-aria/i18n"; import { Button } from ":/components/Button"; import { Popover } from ":/components/Popover"; import { Field, FieldProps } from ":/components/Forms/Field"; import { useCunningham } from ":/components/Provider"; import { Calendar, CalendarRange, } from ":/components/Forms/DatePicker/Calendar"; import { convertDateValueToString } from ":/components/Forms/DatePicker/utils"; export type DatePickerAuxSubProps = FieldProps & { // eslint-disable-next-line react/no-unused-prop-types label?: string; // eslint-disable-next-line react/no-unused-prop-types minValue?: string; // eslint-disable-next-line react/no-unused-prop-types maxValue?: string; disabled?: boolean; name?: string; locale?: string; timezone?: string; }; export type DatePickerAuxProps = PropsWithChildren & DatePickerAuxSubProps & RefAttributes & { pickerState: DateRangePickerState | DatePickerState; pickerProps: Pick< DateRangePickerAria | DatePickerAria, "buttonProps" | "groupProps" >; calendar: React.ReactElement; isFocused: boolean; labelAsPlaceholder: boolean; optionalClassName?: string; isRange?: boolean; onClear: () => void; }; /** * This component is used by date and date range picker components. * It contains the common logic between the two. */ const DatePickerAux = ({ className, pickerState, pickerProps, onClear, isFocused, labelAsPlaceholder, calendar, children, name, locale, disabled = false, optionalClassName, isRange, ref, ...props }: DatePickerAuxProps) => { const { t, currentLocale } = useCunningham(); const pickerRef = useRef(null); const isDateInvalid = useMemo( () => pickerState.validationState === "invalid" || props.state === "error", [pickerState.validationState, props.state] ); return (
!pickerState.isOpen && pickerState.open()} > {"dateRange" in pickerState ? ( <> ) : ( )}
{children}
{pickerState.isOpen && ( {calendar} )}
); }; export default DatePickerAux;