diff --git a/src/DateTimeRangePicker.spec.tsx b/src/DateTimeRangePicker.spec.tsx index e60b319..38c7000 100644 --- a/src/DateTimeRangePicker.spec.tsx +++ b/src/DateTimeRangePicker.spec.tsx @@ -1116,7 +1116,7 @@ describe('DateTimeRangePicker', () => { const onChange = vi.fn(); const valueFrom = new Date(2018, 0, 1); const valueTo = new Date(2018, 6, 1); - const value = [valueFrom, valueTo]; + const value = [valueFrom, valueTo] as [Date, Date]; const { container } = render( { const onChange = vi.fn(); const valueFrom = new Date(2018, 0, 1); const valueTo = new Date(2018, 6, 1); - const value = [valueFrom, valueTo]; + const value = [valueFrom, valueTo] as [Date, Date]; const { container } = render( void; onCalendarOpen?: () => void; - onChange?: (value: Date | null | (Date | null)[]) => void; + onChange?: (value: Value) => void; onClockClose?: () => void; onClockOpen?: () => void; onFocus?: (event: React.FocusEvent) => void; @@ -213,10 +213,7 @@ export default function DateTimeRangePicker(props: DateTimeRangePickerProps) { closeClock(); }, [closeCalendar, closeClock]); - function onChange( - value: Date | null | (Date | null)[], - shouldCloseWidgets = shouldCloseWidgetsProps, - ) { + function onChange(value: Value, shouldCloseWidgets = shouldCloseWidgetsProps) { if (shouldCloseWidgets) { closeWidgets(); } @@ -242,7 +239,13 @@ export default function DateTimeRangePicker(props: DateTimeRangePickerProps) { onChange([valueFromDate, valueTo], closeCalendar); } - function onDateChange(nextValue: Date | null | (Date | null)[], shouldCloseWidgets?: boolean) { + type DatePiece = Date | null; + + function onDateChange( + nextValue: DatePiece | [DatePiece, DatePiece], + shouldCloseWidgets?: boolean, + ) { + // React-Calendar passes an array of values when selectRange is enabled const [rawNextValueFrom, rawNextValueTo] = Array.isArray(nextValue) ? nextValue : [nextValue]; const [valueFrom, valueTo] = Array.isArray(value) ? value : [value]; diff --git a/src/shared/types.ts b/src/shared/types.ts index c10cca0..8ca0a28 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -4,4 +4,10 @@ export type ClassName = string | null | undefined | (string | null | undefined)[ export type Detail = 'hour' | 'minute' | 'second'; -export type LooseValue = string | Date | null | (string | Date | null)[]; +type LooseValuePiece = string | Date | null; + +export type LooseValue = LooseValuePiece | [LooseValuePiece, LooseValuePiece]; + +type ValuePiece = Date | null; + +export type Value = ValuePiece | [ValuePiece, ValuePiece];