warningPropsUtil.js 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _warning = _interopRequireWildcard(require("../../vc-util/warning"));
  7. var _legacyUtil = require("./legacyUtil");
  8. var _commonUtil = require("./commonUtil");
  9. var _propsUtil = require("../../_util/props-util");
  10. var _BaseSelect = require("../BaseSelect");
  11. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
  12. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof 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; }
  13. function warningProps(props) {
  14. const {
  15. mode,
  16. options,
  17. children,
  18. backfill,
  19. allowClear,
  20. placeholder,
  21. getInputElement,
  22. showSearch,
  23. onSearch,
  24. defaultOpen,
  25. autofocus,
  26. labelInValue,
  27. value,
  28. inputValue,
  29. optionLabelProp
  30. } = props;
  31. const multiple = (0, _BaseSelect.isMultiple)(mode);
  32. const mergedShowSearch = showSearch !== undefined ? showSearch : multiple || mode === 'combobox';
  33. const mergedOptions = options || (0, _legacyUtil.convertChildrenToData)(children);
  34. // `tags` should not set option as disabled
  35. (0, _warning.default)(mode !== 'tags' || mergedOptions.every(opt => !opt.disabled), 'Please avoid setting option to disabled in tags mode since user can always type text as tag.');
  36. // `combobox` should not use `optionLabelProp`
  37. (0, _warning.default)(mode !== 'combobox' || !optionLabelProp, '`combobox` mode not support `optionLabelProp`. Please set `value` on Option directly.');
  38. // Only `combobox` support `backfill`
  39. (0, _warning.default)(mode === 'combobox' || !backfill, '`backfill` only works with `combobox` mode.');
  40. // Only `combobox` support `getInputElement`
  41. (0, _warning.default)(mode === 'combobox' || !getInputElement, '`getInputElement` only work with `combobox` mode.');
  42. // Customize `getInputElement` should not use `allowClear` & `placeholder`
  43. (0, _warning.noteOnce)(mode !== 'combobox' || !getInputElement || !allowClear || !placeholder, 'Customize `getInputElement` should customize clear and placeholder logic instead of configuring `allowClear` and `placeholder`.');
  44. // `onSearch` should use in `combobox` or `showSearch`
  45. if (onSearch && !mergedShowSearch && mode !== 'combobox' && mode !== 'tags') {
  46. (0, _warning.default)(false, '`onSearch` should work with `showSearch` instead of use alone.');
  47. }
  48. (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.');
  49. if (value !== undefined && value !== null) {
  50. const values = (0, _commonUtil.toArray)(value);
  51. (0, _warning.default)(!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`');
  52. (0, _warning.default)(!multiple || Array.isArray(value), '`value` should be array when `mode` is `multiple` or `tags`');
  53. }
  54. // Syntactic sugar should use correct children type
  55. if (children) {
  56. let invalidateChildType = null;
  57. children.some(node => {
  58. var _a;
  59. if (!(0, _propsUtil.isValidElement)(node) || !node.type) {
  60. return false;
  61. }
  62. const {
  63. type
  64. } = node;
  65. if (type.isSelectOption) {
  66. return false;
  67. }
  68. if (type.isSelectOptGroup) {
  69. const childs = ((_a = node.children) === null || _a === void 0 ? void 0 : _a.default()) || [];
  70. const allChildrenValid = childs.every(subNode => {
  71. if (!(0, _propsUtil.isValidElement)(subNode) || !node.type || subNode.type.isSelectOption) {
  72. return true;
  73. }
  74. invalidateChildType = subNode.type;
  75. return false;
  76. });
  77. if (allChildrenValid) {
  78. return false;
  79. }
  80. return true;
  81. }
  82. invalidateChildType = type;
  83. return true;
  84. });
  85. if (invalidateChildType) {
  86. (0, _warning.default)(false, `\`children\` should be \`Select.Option\` or \`Select.OptGroup\` instead of \`${invalidateChildType.displayName || invalidateChildType.name || invalidateChildType}\`.`);
  87. }
  88. (0, _warning.default)(inputValue === undefined, '`inputValue` is deprecated, please use `searchValue` instead.');
  89. }
  90. }
  91. var _default = exports.default = warningProps;