import { createCalendar } from '@internationalized/date'; import { useRef } from 'react'; import { DateValue, useDatePicker, AriaDatePickerProps, useDateField, useDateSegment, } from 'react-aria'; import { DateFieldState, DateSegment as TDateSegment, useDateFieldState, useDatePickerState, } from 'react-stately'; type DatepickerProps = { label: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any onChange: (...event: any) => void; value: DateValue; }; type DateFieldProps = AriaDatePickerProps; type DateSegmentProps = { segment: TDateSegment; state: DateFieldState; }; export function Datepicker({ label, onChange, value, ...props }: DatepickerProps) { const state = useDatePickerState({ label, onChange, value, ...props, }); const ref = useRef(null); const { labelProps, fieldProps, groupProps } = useDatePicker( { label, onChange, value }, state, ref ); return (
{/* Label for the Datepicker */} {/* This div will act as a grouping element */}
{/* This div will act as the input element */}
); } function DateField(props: DateFieldProps) { const state = useDateFieldState({ ...props, // why is this? locale: 'es', createCalendar, }); const ref = useRef(null); const { fieldProps } = useDateField(props, state, ref); return (
{state.segments.map((segment, i) => ( ))}
); } function DateSegment({ segment, state }: DateSegmentProps) { const ref = useRef(null); const { segmentProps } = useDateSegment(segment, state, ref); return (
{segment.isPlaceholder ? '' : segment.text}
); }