warningPropsUtil.js
4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import _typeof from "@babel/runtime/helpers/esm/typeof";
import * as React from 'react';
import warning, { noteOnce } from "rc-util/es/warning";
import toNodeArray from "rc-util/es/Children/toArray";
import { convertChildrenToData } from './legacyUtil';
import { toArray } from './commonUtil';
function warningProps(props) {
var mode = props.mode,
options = props.options,
children = props.children,
backfill = props.backfill,
allowClear = props.allowClear,
placeholder = props.placeholder,
getInputElement = props.getInputElement,
showSearch = props.showSearch,
onSearch = props.onSearch,
defaultOpen = props.defaultOpen,
autoFocus = props.autoFocus,
labelInValue = props.labelInValue,
value = props.value,
inputValue = props.inputValue,
optionLabelProp = props.optionLabelProp;
var multiple = mode === 'multiple' || mode === 'tags';
var mergedShowSearch = showSearch !== undefined ? showSearch : multiple || mode === 'combobox';
var mergedOptions = options || convertChildrenToData(children); // `tags` should not set option as disabled
warning(mode !== 'tags' || mergedOptions.every(function (opt) {
return !opt.disabled;
}), 'Please avoid setting option to disabled in tags mode since user can always type text as tag.'); // `combobox` & `tags` should option be `string` type
if (mode === 'tags' || mode === 'combobox') {
var hasNumberValue = mergedOptions.some(function (item) {
if (item.options) {
return item.options.some(function (opt) {
return typeof ('value' in opt ? opt.value : opt.key) === 'number';
});
}
return typeof ('value' in item ? item.value : item.key) === 'number';
});
warning(!hasNumberValue, '`value` of Option should not use number type when `mode` is `tags` or `combobox`.');
} // `combobox` should not use `optionLabelProp`
warning(mode !== 'combobox' || !optionLabelProp, '`combobox` mode not support `optionLabelProp`. Please set `value` on Option directly.'); // Only `combobox` support `backfill`
warning(mode === 'combobox' || !backfill, '`backfill` only works with `combobox` mode.'); // Only `combobox` support `getInputElement`
warning(mode === 'combobox' || !getInputElement, '`getInputElement` only work with `combobox` mode.'); // Customize `getInputElement` should not use `allowClear` & `placeholder`
noteOnce(mode !== 'combobox' || !getInputElement || !allowClear || !placeholder, 'Customize `getInputElement` should customize clear and placeholder logic instead of configuring `allowClear` and `placeholder`.'); // `onSearch` should use in `combobox` or `showSearch`
if (onSearch && !mergedShowSearch && mode !== 'combobox' && mode !== 'tags') {
warning(false, '`onSearch` should work with `showSearch` instead of use alone.');
}
noteOnce(!defaultOpen || autoFocus, '`defaultOpen` makes Select open without focus which means it will not close by click outside. You can set `autoFocus` if needed.');
if (value !== undefined && value !== null) {
var values = toArray(value);
warning(!labelInValue || values.every(function (val) {
return _typeof(val) === 'object' && ('key' in val || 'value' in val);
}), '`value` should in shape of `{ value: string | number, label?: ReactNode }` when you set `labelInValue` to `true`');
warning(!multiple || Array.isArray(value), '`value` should be array when `mode` is `multiple` or `tags`');
} // Syntactic sugar should use correct children type
if (children) {
var invalidateChildType = null;
toNodeArray(children).some(function (node) {
if (!React.isValidElement(node) || !node.type) {
return false;
}
var type = node.type;
if (type.isSelectOption) {
return false;
}
if (type.isSelectOptGroup) {
var allChildrenValid = toNodeArray(node.props.children).every(function (subNode) {
if (!React.isValidElement(subNode) || !node.type || subNode.type.isSelectOption) {
return true;
}
invalidateChildType = subNode.type;
return false;
});
if (allChildrenValid) {
return false;
}
return true;
}
invalidateChildType = type;
return true;
});
if (invalidateChildType) {
warning(false, "`children` should be `Select.Option` or `Select.OptGroup` instead of `".concat(invalidateChildType.displayName || invalidateChildType.name || invalidateChildType, "`."));
}
warning(inputValue === undefined, '`inputValue` is deprecated, please use `searchValue` instead.');
}
}
export default warningProps;