Skip to content

Commit 9a174e3

Browse files
committed
Improve type accuracy for value prop and all callbacks
1 parent d0e4a3d commit 9a174e3

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
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: 5 additions & 8 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;
@@ -160,10 +160,7 @@ export default function DateRangePicker(props: DateRangePickerProps) {
160160
}
161161
}
162162

163-
function onChange(
164-
value: Date | null | (Date | null)[],
165-
shouldCloseCalendar = shouldCloseCalendarProps,
166-
) {
163+
function onChange(value: Value, shouldCloseCalendar = shouldCloseCalendarProps) {
167164
if (shouldCloseCalendar) {
168165
closeCalendar();
169166
}
@@ -173,7 +170,7 @@ export default function DateRangePicker(props: DateRangePickerProps) {
173170
}
174171
}
175172

176-
function onChangeFrom(nextValue: Date | null | (Date | null)[], closeCalendar: boolean) {
173+
function onChangeFrom(nextValue: Value, closeCalendar: boolean) {
177174
const [nextValueFrom] = Array.isArray(nextValue) ? nextValue : [nextValue];
178175
const [, valueTo] = Array.isArray(value) ? value : [value];
179176

@@ -182,7 +179,7 @@ export default function DateRangePicker(props: DateRangePickerProps) {
182179
onChange([nextValueFrom || null, valueToDate], closeCalendar);
183180
}
184181

185-
function onChangeTo(nextValue: Date | null | (Date | null)[], closeCalendar: boolean) {
182+
function onChangeTo(nextValue: Value, closeCalendar: boolean) {
186183
const [, nextValueTo] = Array.isArray(nextValue) ? nextValue : [null, nextValue];
187184
const [valueFrom] = Array.isArray(value) ? value : [value];
188185

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, ValuePiece];

0 commit comments

Comments
 (0)