Skip to content

Commit e5b1bdd

Browse files
committed
Improve type accuracy for value prop and all callbacks
1 parent 4d7ae72 commit e5b1bdd

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/DateRangePicker.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ describe('DateRangePicker', () => {
666666
const onChange = vi.fn();
667667
const valueFrom = new Date(2018, 0, 1);
668668
const valueTo = new Date(2018, 6, 1);
669-
const value = [valueFrom, valueTo];
669+
const value = [valueFrom, valueTo] as [Date, Date];
670670

671671
const { container } = render(<DateRangePicker onChange={onChange} value={value} />);
672672

@@ -760,7 +760,7 @@ describe('DateRangePicker', () => {
760760
const onChange = vi.fn();
761761
const valueFrom = new Date(2018, 0, 1);
762762
const valueTo = new Date(2018, 6, 1);
763-
const value = [valueFrom, valueTo];
763+
const value = [valueFrom, valueTo] as [Date, Date];
764764

765765
const { container } = render(<DateRangePicker onChange={onChange} value={value} />);
766766

src/DateRangePicker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import DateInput from 'react-date-picker/dist/cjs/DateInput';
1010

1111
import { isMaxDate, isMinDate } from './shared/propTypes';
1212

13-
import type { ClassName, Detail, LooseValue } from './shared/types';
13+
import type { ClassName, Detail, LooseValue, Value } from './shared/types';
1414

1515
const baseClassName = 'react-daterange-picker';
1616
const outsideActionEvents = ['mousedown', 'focusin', 'touchstart'];
@@ -77,7 +77,7 @@ type DateRangePickerProps = {
7777
nativeInputAriaLabel?: string;
7878
onCalendarClose?: () => void;
7979
onCalendarOpen?: () => void;
80-
onChange?: (value: Date | null | (Date | null)[]) => void;
80+
onChange?: (value: Value) => void;
8181
onFocus?: (event: React.FocusEvent<HTMLDivElement>) => void;
8282
openCalendarOnFocus?: boolean;
8383
portalContainer?: HTMLElement;

src/shared/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ export type ClassName = string | null | undefined | (string | null | undefined)[
22

33
export type Detail = 'century' | 'decade' | 'year' | 'month';
44

5-
export type LooseValue = string | Date | null | (string | Date | null)[];
5+
type LooseValuePiece = string | Date | null;
6+
7+
export type LooseValue = LooseValuePiece | [LooseValuePiece, LooseValuePiece];
68

79
export type RangeType = 'century' | 'decade' | 'year' | 'month' | 'day';
10+
11+
type ValuePiece = Date | null;
12+
13+
export type Value = ValuePiece | ValuePiece[]; // TODO: Change to [ValuePiece, ValuePiece];

0 commit comments

Comments
 (0)