UNPKG

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