warningPropsUtil.js 5.5 KB

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