Skip to content

Commit

Permalink
Improve type accuracy for value prop and all callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed Apr 5, 2023
1 parent d0e4a3d commit 9a174e3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/DateRangePicker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ describe('DateRangePicker', () => {
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(<DateRangePicker onChange={onChange} value={value} />);

Expand Down Expand Up @@ -760,7 +760,7 @@ describe('DateRangePicker', () => {
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(<DateRangePicker onChange={onChange} value={value} />);

Expand Down
13 changes: 5 additions & 8 deletions src/DateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import DateInput from 'react-date-picker/dist/cjs/DateInput';

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

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

const baseClassName = 'react-daterange-picker';
const outsideActionEvents = ['mousedown', 'focusin', 'touchstart'];
Expand Down Expand Up @@ -77,7 +77,7 @@ type DateRangePickerProps = {
nativeInputAriaLabel?: string;
onCalendarClose?: () => void;
onCalendarOpen?: () => void;
onChange?: (value: Date | null | (Date | null)[]) => void;
onChange?: (value: Value) => void;
onFocus?: (event: React.FocusEvent<HTMLDivElement>) => void;
openCalendarOnFocus?: boolean;
portalContainer?: HTMLElement;
Expand Down Expand Up @@ -160,10 +160,7 @@ export default function DateRangePicker(props: DateRangePickerProps) {
}
}

function onChange(
value: Date | null | (Date | null)[],
shouldCloseCalendar = shouldCloseCalendarProps,
) {
function onChange(value: Value, shouldCloseCalendar = shouldCloseCalendarProps) {
if (shouldCloseCalendar) {
closeCalendar();
}
Expand All @@ -173,7 +170,7 @@ export default function DateRangePicker(props: DateRangePickerProps) {
}
}

function onChangeFrom(nextValue: Date | null | (Date | null)[], closeCalendar: boolean) {
function onChangeFrom(nextValue: Value, closeCalendar: boolean) {
const [nextValueFrom] = Array.isArray(nextValue) ? nextValue : [nextValue];
const [, valueTo] = Array.isArray(value) ? value : [value];

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

function onChangeTo(nextValue: Date | null | (Date | null)[], closeCalendar: boolean) {
function onChangeTo(nextValue: Value, closeCalendar: boolean) {
const [, nextValueTo] = Array.isArray(nextValue) ? nextValue : [null, nextValue];
const [valueFrom] = Array.isArray(value) ? value : [value];

Expand Down
8 changes: 7 additions & 1 deletion src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ export type ClassName = string | null | undefined | (string | null | undefined)[

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

export type LooseValue = string | Date | null | (string | Date | null)[];
type LooseValuePiece = string | Date | null;

export type LooseValue = LooseValuePiece | [LooseValuePiece, LooseValuePiece];

export type RangeType = 'century' | 'decade' | 'year' | 'month' | 'day';

type ValuePiece = Date | null;

export type Value = ValuePiece | [ValuePiece, ValuePiece];

0 comments on commit 9a174e3

Please sign in to comment.