From b825ffb48362df6479b5d648ff066350ef5611be Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Sun, 28 May 2023 15:26:45 +0200 Subject: [PATCH] Fix isValueOrValueArray propType not working well with TypeScript --- src/DatePicker.tsx | 4 ++-- src/shared/propTypes.ts | 7 ++++++- src/shared/types.ts | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/DatePicker.tsx b/src/DatePicker.tsx index a4b0db13..9249e63a 100644 --- a/src/DatePicker.tsx +++ b/src/DatePicker.tsx @@ -8,7 +8,7 @@ import Fit from 'react-fit'; import DateInput from './DateInput'; -import { isMaxDate, isMinDate } from './shared/propTypes'; +import { isMaxDate, isMinDate, rangeOf } from './shared/propTypes'; import type { ClassName, CloseReason, Detail, LooseValue, OpenReason, Value } from './shared/types'; @@ -423,7 +423,7 @@ export default function DatePicker(props: DatePickerProps) { const isValue = PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]); -const isValueOrValueArray = PropTypes.oneOfType([isValue, PropTypes.arrayOf(isValue)]); +const isValueOrValueArray = PropTypes.oneOfType([isValue, rangeOf(isValue)]); DatePicker.propTypes = { autoFocus: PropTypes.bool, diff --git a/src/shared/propTypes.ts b/src/shared/propTypes.ts index 68ecbf87..5c3821d8 100644 --- a/src/shared/propTypes.ts +++ b/src/shared/propTypes.ts @@ -1,6 +1,7 @@ import PropTypes from 'prop-types'; -import type { Validator } from 'prop-types'; +import type { Requireable, Validator } from 'prop-types'; +import type { Range } from './types'; const allViews = ['century', 'decade', 'year', 'month']; const allValueTypes = [...allViews.slice(1), 'day']; @@ -69,3 +70,7 @@ export const isRef = PropTypes.oneOfType([ current: PropTypes.any, }), ]); + +export const rangeOf = (type: Requireable): Requireable> => { + return PropTypes.arrayOf(type) as Requireable>; +}; diff --git a/src/shared/types.ts b/src/shared/types.ts index c97777cb..80e36f40 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -1,4 +1,4 @@ -type Range = [T, T]; +export type Range = [T, T]; export type ClassName = string | null | undefined | (string | null | undefined)[];