Skip to content

Commit

Permalink
Fix isValueOrValueArray propType not working well with TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmaj committed May 28, 2023
1 parent 3db9dcd commit b26ca7a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/shared/propTypes.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand Down Expand Up @@ -69,3 +70,7 @@ export const isRef = PropTypes.oneOfType([
current: PropTypes.any,
}),
]);

export const rangeOf = <T>(type: Requireable<T>): Requireable<Range<T>> => {
return PropTypes.arrayOf(type) as Requireable<Range<T>>;
};
2 changes: 1 addition & 1 deletion src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Range<T> = [T, T];
export type Range<T> = [T, T];

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

Expand Down

0 comments on commit b26ca7a

Please sign in to comment.