warningPropsUtil.js 5.8 KB

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