UNPKG

5.61 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.checkPropType = checkPropType;
9exports.caseSensitiveType = caseSensitiveType;
10exports.deprecated = deprecated;
11exports.defaultInputValueType = defaultInputValueType;
12exports.defaultSelectedType = defaultSelectedType;
13exports.highlightOnlyResultType = highlightOnlyResultType;
14exports.ignoreDiacriticsType = ignoreDiacriticsType;
15exports.inputPropsType = inputPropsType;
16exports.isRequiredForA11y = isRequiredForA11y;
17exports.labelKeyType = labelKeyType;
18exports.selectedType = selectedType;
19exports.optionType = exports.sizeType = void 0;
20
21var _propTypes = _interopRequireDefault(require("prop-types"));
22
23var _utils = require("./utils");
24
25var _constants = require("./constants");
26
27var INPUT_PROPS_BLACKLIST = [{
28 alt: 'onBlur',
29 prop: 'onBlur'
30}, {
31 alt: 'onInputChange',
32 prop: 'onChange'
33}, {
34 alt: 'onFocus',
35 prop: 'onFocus'
36}, {
37 alt: 'onKeyDown',
38 prop: 'onKeyDown'
39}];
40
41var sizeType = _propTypes["default"].oneOf((0, _utils.values)(_constants.SIZE));
42/**
43 * Allows additional warnings or messaging related to prop validation.
44 */
45
46
47exports.sizeType = sizeType;
48
49function checkPropType(validator, callback) {
50 return function (props, propName, componentName) {
51 var _PropTypes$checkPropT;
52
53 _propTypes["default"].checkPropTypes((_PropTypes$checkPropT = {}, _PropTypes$checkPropT[propName] = validator, _PropTypes$checkPropT), props, 'prop', componentName);
54
55 (0, _utils.isFunction)(callback) && callback(props, propName, componentName);
56 };
57}
58
59function caseSensitiveType(props, propName, componentName) {
60 var caseSensitive = props.caseSensitive,
61 filterBy = props.filterBy;
62 (0, _utils.warn)(!caseSensitive || typeof filterBy !== 'function', 'Your `filterBy` function will override the `caseSensitive` prop.');
63}
64
65function deprecated(validator, reason) {
66 return function (props, propName, componentName) {
67 var _PropTypes$checkPropT2;
68
69 if (props[propName] != null) {
70 (0, _utils.warn)(false, "The `" + propName + "` prop is deprecated. " + reason);
71 }
72
73 return _propTypes["default"].checkPropTypes((_PropTypes$checkPropT2 = {}, _PropTypes$checkPropT2[propName] = validator, _PropTypes$checkPropT2), props, 'prop', componentName);
74 };
75}
76
77function defaultInputValueType(props, propName, componentName) {
78 var defaultInputValue = props.defaultInputValue,
79 defaultSelected = props.defaultSelected,
80 multiple = props.multiple,
81 selected = props.selected;
82 var name = defaultSelected.length ? 'defaultSelected' : 'selected';
83 (0, _utils.warn)(!(!multiple && defaultInputValue && (defaultSelected.length || selected && selected.length)), "`defaultInputValue` will be overridden by the value from `" + name + "`.");
84}
85
86function defaultSelectedType(props, propName, componentName) {
87 var defaultSelected = props.defaultSelected,
88 multiple = props.multiple;
89 (0, _utils.warn)(multiple || defaultSelected.length <= 1, 'You are passing multiple options to the `defaultSelected` prop of a ' + 'Typeahead in single-select mode. The selections will be truncated to a ' + 'single selection.');
90}
91
92function highlightOnlyResultType(props, propName, componentName) {
93 var allowNew = props.allowNew,
94 highlightOnlyResult = props.highlightOnlyResult;
95 (0, _utils.warn)(!(highlightOnlyResult && allowNew), '`highlightOnlyResult` will not work with `allowNew`.');
96}
97
98function ignoreDiacriticsType(props, propName, componentName) {
99 var filterBy = props.filterBy,
100 ignoreDiacritics = props.ignoreDiacritics;
101 (0, _utils.warn)(ignoreDiacritics || typeof filterBy !== 'function', 'Your `filterBy` function will override the `ignoreDiacritics` prop.');
102}
103
104function inputPropsType(props, propName, componentName) {
105 var inputProps = props.inputProps;
106
107 if (!(inputProps && Object.prototype.toString.call(inputProps) === '[object Object]')) {
108 return;
109 } // Blacklisted properties.
110
111
112 INPUT_PROPS_BLACKLIST.forEach(function (_ref) {
113 var alt = _ref.alt,
114 prop = _ref.prop;
115 var msg = alt ? " Use the top-level `" + alt + "` prop instead." : null;
116 (0, _utils.warn)(!inputProps[prop], "The `" + prop + "` property of `inputProps` will be ignored." + msg);
117 });
118}
119
120function isRequiredForA11y(props, propName, componentName) {
121 (0, _utils.warn)(props[propName] != null, "The prop `" + propName + "` is required to make `" + componentName + "` " + 'accessible for users of assistive technologies such as screen readers.');
122}
123
124function labelKeyType(props, propName, componentName) {
125 var allowNew = props.allowNew,
126 labelKey = props.labelKey;
127 (0, _utils.warn)(!((0, _utils.isFunction)(labelKey) && allowNew), '`labelKey` must be a string when `allowNew={true}`.');
128}
129
130var optionType = _propTypes["default"].oneOfType([_propTypes["default"].object, _propTypes["default"].string]);
131
132exports.optionType = optionType;
133
134function selectedType(props, propName, componentName) {
135 var multiple = props.multiple,
136 onChange = props.onChange,
137 selected = props.selected;
138 (0, _utils.warn)(multiple || !selected || selected.length <= 1, 'You are passing multiple options to the `selected` prop of a Typeahead ' + 'in single-select mode. This may lead to unexpected behaviors or errors.');
139 (0, _utils.warn)(!selected || selected && (0, _utils.isFunction)(onChange), 'You provided a `selected` prop without an `onChange` handler. If you ' + 'want the typeahead to be uncontrolled, use `defaultSelected`. ' + 'Otherwise, set `onChange`.');
140}
\No newline at end of file