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 4246ea1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 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
4 changes: 2 additions & 2 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
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[]; // TODO: Change to [ValuePiece, ValuePiece];

0 comments on commit 4246ea1

Please sign in to comment.