warningPropsUtil.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. exports.warningNullOptions = warningNullOptions;
  9. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  10. var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
  11. var _warning = _interopRequireWildcard(require("rc-util/lib/warning"));
  12. var React = _interopRequireWildcard(require("react"));
  13. var _BaseSelect = require("../BaseSelect");
  14. var _commonUtil = require("./commonUtil");
  15. var _legacyUtil = require("./legacyUtil");
  16. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
  17. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof3(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; }
  18. function warningProps(props) {
  19. var mode = props.mode,
  20. options = props.options,
  21. children = props.children,
  22. backfill = props.backfill,
  23. allowClear = props.allowClear,
  24. placeholder = props.placeholder,
  25. getInputElement = props.getInputElement,
  26. showSearch = props.showSearch,
  27. onSearch = props.onSearch,
  28. defaultOpen = props.defaultOpen,
  29. autoFocus = props.autoFocus,
  30. labelInValue = props.labelInValue,
  31. value = props.value,
  32. inputValue = props.inputValue,
  33. optionLabelProp = props.optionLabelProp;
  34. var multiple = (0, _BaseSelect.isMultiple)(mode);
  35. var mergedShowSearch = showSearch !== undefined ? showSearch : multiple || mode === 'combobox';
  36. var mergedOptions = options || (0, _legacyUtil.convertChildrenToData)(children);
  37. // `tags` should not set option as disabled
  38. (0, _warning.default)(mode !== 'tags' || mergedOptions.every(function (opt) {
  39. return !opt.disabled;
  40. }), 'Please avoid setting option to disabled in tags mode since user can always type text as tag.');
  41. // `combobox` & `tags` should option be `string` type
  42. if (mode === 'tags' || mode === 'combobox') {
  43. var hasNumberValue = mergedOptions.some(function (item) {
  44. if (item.options) {
  45. return item.options.some(function (opt) {
  46. return typeof ('value' in opt ? opt.value : opt.key) === 'number';
  47. });
  48. }
  49. return typeof ('value' in item ? item.value : item.key) === 'number';
  50. });
  51. (0, _warning.default)(!hasNumberValue, '`value` of Option should not use number type when `mode` is `tags` or `combobox`.');
  52. }
  53. // `combobox` should not use `optionLabelProp`
  54. (0, _warning.default)(mode !== 'combobox' || !optionLabelProp, '`combobox` mode not support `optionLabelProp`. Please set `value` on Option directly.');
  55. // Only `combobox` support `backfill`
  56. (0, _warning.default)(mode === 'combobox' || !backfill, '`backfill` only works with `combobox` mode.');
  57. // Only `combobox` support `getInputElement`
  58. (0, _warning.default)(mode === 'combobox' || !getInputElement, '`getInputElement` only work with `combobox` mode.');
  59. // Customize `getInputElement` should not use `allowClear` & `placeholder`
  60. (0, _warning.noteOnce)(mode !== 'combobox' || !getInputElement || !allowClear || !placeholder, 'Customize `getInputElement` should customize clear and placeholder logic instead of configuring `allowClear` and `placeholder`.');
  61. // `onSearch` should use in `combobox` or `showSearch`
  62. if (onSearch && !mergedShowSearch && mode !== 'combobox' && mode !== 'tags') {
  63. (0, _warning.default)(false, '`onSearch` should work with `showSearch` instead of use alone.');
  64. }
  65. (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.');
  66. if (value !== undefined && value !== null) {
  67. var values = (0, _commonUtil.toArray)(value);
  68. (0, _warning.default)(!labelInValue || values.every(function (val) {
  69. return (0, _typeof2.default)(val) === 'object' && ('key' in val || 'value' in val);
  70. }), '`value` should in shape of `{ value: string | number, label?: ReactNode }` when you set `labelInValue` to `true`');
  71. (0, _warning.default)(!multiple || Array.isArray(value), '`value` should be array when `mode` is `multiple` or `tags`');
  72. }
  73. // Syntactic sugar should use correct children type
  74. if (children) {
  75. var invalidateChildType = null;
  76. (0, _toArray.default)(children).some(function (node) {
  77. if (! /*#__PURE__*/React.isValidElement(node) || !node.type) {
  78. return false;
  79. }
  80. var _ref = node,
  81. type = _ref.type;
  82. if (type.isSelectOption) {
  83. return false;
  84. }
  85. if (type.isSelectOptGroup) {
  86. var allChildrenValid = (0, _toArray.default)(node.props.children).every(function (subNode) {
  87. if (! /*#__PURE__*/React.isValidElement(subNode) || !node.type || subNode.type.isSelectOption) {
  88. return true;
  89. }
  90. invalidateChildType = subNode.type;
  91. return false;
  92. });
  93. if (allChildrenValid) {
  94. return false;
  95. }
  96. return true;
  97. }
  98. invalidateChildType = type;
  99. return true;
  100. });
  101. if (invalidateChildType) {
  102. (0, _warning.default)(false, "`children` should be `Select.Option` or `Select.OptGroup` instead of `".concat(invalidateChildType.displayName || invalidateChildType.name || invalidateChildType, "`."));
  103. }
  104. (0, _warning.default)(inputValue === undefined, '`inputValue` is deprecated, please use `searchValue` instead.');
  105. }
  106. }
  107. // value in Select option should not be null
  108. // note: OptGroup has options too
  109. function warningNullOptions(options, fieldNames) {
  110. if (options) {
  111. var recursiveOptions = function recursiveOptions(optionsList) {
  112. var inGroup = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  113. for (var i = 0; i < optionsList.length; i++) {
  114. var option = optionsList[i];
  115. if (option[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.value] === null) {
  116. (0, _warning.default)(false, '`value` in Select options should not be `null`.');
  117. return true;
  118. }
  119. if (!inGroup && Array.isArray(option[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.options]) && recursiveOptions(option[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.options], true)) {
  120. break;
  121. }
  122. }
  123. };
  124. recursiveOptions(options);
  125. }
  126. }
  127. var _default = exports.default = warningProps;