warningPropsUtil.js 4.1 KB

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