1 | import PropTypes from 'prop-types';
|
2 | import { SIZES } from './constants';
|
3 | import { isFunction, warn } from './utils';
|
4 | const INPUT_PROPS_BLACKLIST = [
|
5 | { alt: 'onBlur', prop: 'onBlur' },
|
6 | { alt: 'onInputChange', prop: 'onChange' },
|
7 | { alt: 'onFocus', prop: 'onFocus' },
|
8 | { alt: 'onKeyDown', prop: 'onKeyDown' },
|
9 | ];
|
10 | export const sizeType = PropTypes.oneOf(SIZES);
|
11 | export function checkPropType(validator, callback) {
|
12 | return (props, propName, componentName) => {
|
13 | PropTypes.checkPropTypes({ [propName]: validator }, props, 'prop', componentName);
|
14 | isFunction(callback) && callback(props, propName, componentName);
|
15 | };
|
16 | }
|
17 | export function caseSensitiveType(props) {
|
18 | const { caseSensitive, filterBy } = props;
|
19 | warn(!caseSensitive || typeof filterBy !== 'function', 'Your `filterBy` function will override the `caseSensitive` prop.');
|
20 | }
|
21 | export function deprecated(validator, reason) {
|
22 | return (props, propName, componentName) => {
|
23 | if (props[propName] != null) {
|
24 | warn(false, `The \`${propName}\` prop is deprecated. ${reason}`);
|
25 | }
|
26 | return PropTypes.checkPropTypes({ [propName]: validator }, props, 'prop', componentName);
|
27 | };
|
28 | }
|
29 | export function defaultInputValueType(props) {
|
30 | const { defaultInputValue, defaultSelected, multiple, selected } = props;
|
31 | const name = defaultSelected.length ? 'defaultSelected' : 'selected';
|
32 | warn(!(!multiple &&
|
33 | defaultInputValue &&
|
34 | (defaultSelected.length || (selected && selected.length))), `\`defaultInputValue\` will be overridden by the value from \`${name}\`.`);
|
35 | }
|
36 | export function defaultSelectedType(props) {
|
37 | const { defaultSelected, multiple } = props;
|
38 | warn(multiple || defaultSelected.length <= 1, 'You are passing multiple options to the `defaultSelected` prop of a ' +
|
39 | 'Typeahead in single-select mode. The selections will be truncated to a ' +
|
40 | 'single selection.');
|
41 | }
|
42 | export function highlightOnlyResultType({ allowNew, highlightOnlyResult, }) {
|
43 | warn(!(highlightOnlyResult && allowNew), '`highlightOnlyResult` will not work with `allowNew`.');
|
44 | }
|
45 | export function ignoreDiacriticsType(props) {
|
46 | const { filterBy, ignoreDiacritics } = props;
|
47 | warn(ignoreDiacritics || typeof filterBy !== 'function', 'Your `filterBy` function will override the `ignoreDiacritics` prop.');
|
48 | }
|
49 | export function inputPropsType({ inputProps }) {
|
50 | if (!(inputProps &&
|
51 | Object.prototype.toString.call(inputProps) === '[object Object]')) {
|
52 | return;
|
53 | }
|
54 | INPUT_PROPS_BLACKLIST.forEach(({ alt, prop }) => {
|
55 | const msg = alt ? ` Use the top-level \`${alt}\` prop instead.` : null;
|
56 | warn(!inputProps[prop], `The \`${prop}\` property of \`inputProps\` will be ignored.${msg}`);
|
57 | });
|
58 | }
|
59 | export function isRequiredForA11y(props, propName, componentName) {
|
60 | warn(props[propName] != null, `The prop \`${propName}\` is required to make \`${componentName}\` ` +
|
61 | 'accessible for users of assistive technologies such as screen readers.');
|
62 | }
|
63 | export function labelKeyType({ allowNew, labelKey }) {
|
64 | warn(!(isFunction(labelKey) && allowNew), '`labelKey` must be a string when `allowNew={true}`.');
|
65 | }
|
66 | export const optionType = PropTypes.oneOfType([
|
67 | PropTypes.object,
|
68 | PropTypes.string,
|
69 | ]);
|
70 | export function selectedType({ multiple, onChange, selected }) {
|
71 | warn(multiple || !selected || selected.length <= 1, 'You are passing multiple options to the `selected` prop of a Typeahead ' +
|
72 | 'in single-select mode. This may lead to unexpected behaviors or errors.');
|
73 | warn(!selected || (selected && isFunction(onChange)), 'You provided a `selected` prop without an `onChange` handler. If you ' +
|
74 | 'want the typeahead to be uncontrolled, use `defaultSelected`. ' +
|
75 | 'Otherwise, set `onChange`.');
|
76 | }
|