UNPKG

3.81 kBJavaScriptView Raw
1import PropTypes from 'prop-types';
2import { SIZES } from './constants';
3import { isFunction, warn } from './utils';
4const INPUT_PROPS_BLACKLIST = [
5 { alt: 'onBlur', prop: 'onBlur' },
6 { alt: 'onInputChange', prop: 'onChange' },
7 { alt: 'onFocus', prop: 'onFocus' },
8 { alt: 'onKeyDown', prop: 'onKeyDown' },
9];
10export const sizeType = PropTypes.oneOf(SIZES);
11export 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}
17export function caseSensitiveType(props) {
18 const { caseSensitive, filterBy } = props;
19 warn(!caseSensitive || typeof filterBy !== 'function', 'Your `filterBy` function will override the `caseSensitive` prop.');
20}
21export 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}
29export 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}
36export 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}
42export function highlightOnlyResultType({ allowNew, highlightOnlyResult, }) {
43 warn(!(highlightOnlyResult && allowNew), '`highlightOnlyResult` will not work with `allowNew`.');
44}
45export function ignoreDiacriticsType(props) {
46 const { filterBy, ignoreDiacritics } = props;
47 warn(ignoreDiacritics || typeof filterBy !== 'function', 'Your `filterBy` function will override the `ignoreDiacritics` prop.');
48}
49export 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}
59export 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}
63export function labelKeyType({ allowNew, labelKey }) {
64 warn(!(isFunction(labelKey) && allowNew), '`labelKey` must be a string when `allowNew={true}`.');
65}
66export const optionType = PropTypes.oneOfType([
67 PropTypes.object,
68 PropTypes.string,
69]);
70export 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}