Skip to content

Commit b26ca7a

Browse files
committed
Fix isValueOrValueArray propType not working well with TypeScript
1 parent 3db9dcd commit b26ca7a

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/DatePicker.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Fit from 'react-fit';
88

99
import DateInput from './DateInput';
1010

11-
import { isMaxDate, isMinDate } from './shared/propTypes';
11+
import { isMaxDate, isMinDate, rangeOf } from './shared/propTypes';
1212

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

@@ -423,7 +423,7 @@ export default function DatePicker(props: DatePickerProps) {
423423

424424
const isValue = PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]);
425425

426-
const isValueOrValueArray = PropTypes.oneOfType([isValue, PropTypes.arrayOf(isValue)]);
426+
const isValueOrValueArray = PropTypes.oneOfType([isValue, rangeOf(isValue)]);
427427

428428
DatePicker.propTypes = {
429429
autoFocus: PropTypes.bool,

src/shared/propTypes.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import PropTypes from 'prop-types';
22

3-
import type { Validator } from 'prop-types';
3+
import type { Requireable, Validator } from 'prop-types';
4+
import type { Range } from './types';
45

56
const allViews = ['century', 'decade', 'year', 'month'];
67
const allValueTypes = [...allViews.slice(1), 'day'];
@@ -69,3 +70,7 @@ export const isRef = PropTypes.oneOfType([
6970
current: PropTypes.any,
7071
}),
7172
]);
73+
74+
export const rangeOf = <T>(type: Requireable<T>): Requireable<Range<T>> => {
75+
return PropTypes.arrayOf(type) as Requireable<Range<T>>;
76+
};

src/shared/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Range<T> = [T, T];
1+
export type Range<T> = [T, T];
22

33
export type ClassName = string | null | undefined | (string | null | undefined)[];
44

0 commit comments

Comments
 (0)