warningPropsUtil.js 3.7 KB

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