UNPKG

7.16 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4var _typeof3 = require("@babel/runtime/helpers/typeof");
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9exports.warningNullOptions = warningNullOptions;
10var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
11var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
12var _warning = _interopRequireWildcard(require("rc-util/lib/warning"));
13var React = _interopRequireWildcard(require("react"));
14var _BaseSelect = require("../BaseSelect");
15var _commonUtil = require("./commonUtil");
16var _legacyUtil = require("./legacyUtil");
17function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
18function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof3(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
19function warningProps(props) {
20 var mode = props.mode,
21 options = props.options,
22 children = props.children,
23 backfill = props.backfill,
24 allowClear = props.allowClear,
25 placeholder = props.placeholder,
26 getInputElement = props.getInputElement,
27 showSearch = props.showSearch,
28 onSearch = props.onSearch,
29 defaultOpen = props.defaultOpen,
30 autoFocus = props.autoFocus,
31 labelInValue = props.labelInValue,
32 value = props.value,
33 inputValue = props.inputValue,
34 optionLabelProp = props.optionLabelProp;
35 var multiple = (0, _BaseSelect.isMultiple)(mode);
36 var mergedShowSearch = showSearch !== undefined ? showSearch : multiple || mode === 'combobox';
37 var mergedOptions = options || (0, _legacyUtil.convertChildrenToData)(children);
38
39 // `tags` should not set option as disabled
40 (0, _warning.default)(mode !== 'tags' || mergedOptions.every(function (opt) {
41 return !opt.disabled;
42 }), 'Please avoid setting option to disabled in tags mode since user can always type text as tag.');
43
44 // `combobox` & `tags` should option be `string` type
45 if (mode === 'tags' || mode === 'combobox') {
46 var hasNumberValue = mergedOptions.some(function (item) {
47 if (item.options) {
48 return item.options.some(function (opt) {
49 return typeof ('value' in opt ? opt.value : opt.key) === 'number';
50 });
51 }
52 return typeof ('value' in item ? item.value : item.key) === 'number';
53 });
54 (0, _warning.default)(!hasNumberValue, '`value` of Option should not use number type when `mode` is `tags` or `combobox`.');
55 }
56
57 // `combobox` should not use `optionLabelProp`
58 (0, _warning.default)(mode !== 'combobox' || !optionLabelProp, '`combobox` mode not support `optionLabelProp`. Please set `value` on Option directly.');
59
60 // Only `combobox` support `backfill`
61 (0, _warning.default)(mode === 'combobox' || !backfill, '`backfill` only works with `combobox` mode.');
62
63 // Only `combobox` support `getInputElement`
64 (0, _warning.default)(mode === 'combobox' || !getInputElement, '`getInputElement` only work with `combobox` mode.');
65
66 // Customize `getInputElement` should not use `allowClear` & `placeholder`
67 (0, _warning.noteOnce)(mode !== 'combobox' || !getInputElement || !allowClear || !placeholder, 'Customize `getInputElement` should customize clear and placeholder logic instead of configuring `allowClear` and `placeholder`.');
68
69 // `onSearch` should use in `combobox` or `showSearch`
70 if (onSearch && !mergedShowSearch && mode !== 'combobox' && mode !== 'tags') {
71 (0, _warning.default)(false, '`onSearch` should work with `showSearch` instead of use alone.');
72 }
73 (0, _warning.noteOnce)(!defaultOpen || autoFocus, '`defaultOpen` makes Select open without focus which means it will not close by click outside. You can set `autoFocus` if needed.');
74 if (value !== undefined && value !== null) {
75 var values = (0, _commonUtil.toArray)(value);
76 (0, _warning.default)(!labelInValue || values.every(function (val) {
77 return (0, _typeof2.default)(val) === 'object' && ('key' in val || 'value' in val);
78 }), '`value` should in shape of `{ value: string | number, label?: ReactNode }` when you set `labelInValue` to `true`');
79 (0, _warning.default)(!multiple || Array.isArray(value), '`value` should be array when `mode` is `multiple` or `tags`');
80 }
81
82 // Syntactic sugar should use correct children type
83 if (children) {
84 var invalidateChildType = null;
85 (0, _toArray.default)(children).some(function (node) {
86 if (! /*#__PURE__*/React.isValidElement(node) || !node.type) {
87 return false;
88 }
89 var _ref = node,
90 type = _ref.type;
91 if (type.isSelectOption) {
92 return false;
93 }
94 if (type.isSelectOptGroup) {
95 var allChildrenValid = (0, _toArray.default)(node.props.children).every(function (subNode) {
96 if (! /*#__PURE__*/React.isValidElement(subNode) || !node.type || subNode.type.isSelectOption) {
97 return true;
98 }
99 invalidateChildType = subNode.type;
100 return false;
101 });
102 if (allChildrenValid) {
103 return false;
104 }
105 return true;
106 }
107 invalidateChildType = type;
108 return true;
109 });
110 if (invalidateChildType) {
111 (0, _warning.default)(false, "`children` should be `Select.Option` or `Select.OptGroup` instead of `".concat(invalidateChildType.displayName || invalidateChildType.name || invalidateChildType, "`."));
112 }
113 (0, _warning.default)(inputValue === undefined, '`inputValue` is deprecated, please use `searchValue` instead.');
114 }
115}
116
117// value in Select option should not be null
118// note: OptGroup has options too
119function warningNullOptions(options, fieldNames) {
120 if (options) {
121 var recursiveOptions = function recursiveOptions(optionsList) {
122 var inGroup = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
123 for (var i = 0; i < optionsList.length; i++) {
124 var option = optionsList[i];
125 if (option[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.value] === null) {
126 (0, _warning.default)(false, '`value` in Select options should not be `null`.');
127 return true;
128 }
129 if (!inGroup && Array.isArray(option[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.options]) && recursiveOptions(option[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.options], true)) {
130 break;
131 }
132 }
133 };
134 recursiveOptions(options);
135 }
136}
137var _default = exports.default = warningProps;
\No newline at end of file