valueUtil.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import _toArray from "@babel/runtime/helpers/esm/toArray";
  3. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  4. import warning from "rc-util/es/warning";
  5. function getKey(data, index) {
  6. var key = data.key;
  7. var value;
  8. if ('value' in data) {
  9. value = data.value;
  10. }
  11. if (key !== null && key !== undefined) {
  12. return key;
  13. }
  14. if (value !== undefined) {
  15. return value;
  16. }
  17. return "rc-index-key-".concat(index);
  18. }
  19. export function isValidCount(value) {
  20. return typeof value !== 'undefined' && !Number.isNaN(value);
  21. }
  22. export function fillFieldNames(fieldNames, childrenAsData) {
  23. var _ref = fieldNames || {},
  24. label = _ref.label,
  25. value = _ref.value,
  26. options = _ref.options,
  27. groupLabel = _ref.groupLabel;
  28. var mergedLabel = label || (childrenAsData ? 'children' : 'label');
  29. return {
  30. label: mergedLabel,
  31. value: value || 'value',
  32. options: options || 'options',
  33. groupLabel: groupLabel || mergedLabel
  34. };
  35. }
  36. /**
  37. * Flat options into flatten list.
  38. * We use `optionOnly` here is aim to avoid user use nested option group.
  39. * Here is simply set `key` to the index if not provided.
  40. */
  41. export function flattenOptions(options) {
  42. var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  43. fieldNames = _ref2.fieldNames,
  44. childrenAsData = _ref2.childrenAsData;
  45. var flattenList = [];
  46. var _fillFieldNames = fillFieldNames(fieldNames, false),
  47. fieldLabel = _fillFieldNames.label,
  48. fieldValue = _fillFieldNames.value,
  49. fieldOptions = _fillFieldNames.options,
  50. groupLabel = _fillFieldNames.groupLabel;
  51. function dig(list, isGroupOption) {
  52. if (!Array.isArray(list)) {
  53. return;
  54. }
  55. list.forEach(function (data) {
  56. if (isGroupOption || !(fieldOptions in data)) {
  57. var value = data[fieldValue];
  58. // Option
  59. flattenList.push({
  60. key: getKey(data, flattenList.length),
  61. groupOption: isGroupOption,
  62. data: data,
  63. label: data[fieldLabel],
  64. value: value
  65. });
  66. } else {
  67. var grpLabel = data[groupLabel];
  68. if (grpLabel === undefined && childrenAsData) {
  69. grpLabel = data.label;
  70. }
  71. // Option Group
  72. flattenList.push({
  73. key: getKey(data, flattenList.length),
  74. group: true,
  75. data: data,
  76. label: grpLabel
  77. });
  78. dig(data[fieldOptions], true);
  79. }
  80. });
  81. }
  82. dig(options, false);
  83. return flattenList;
  84. }
  85. /**
  86. * Inject `props` into `option` for legacy usage
  87. */
  88. export function injectPropsWithOption(option) {
  89. var newOption = _objectSpread({}, option);
  90. if (!('props' in newOption)) {
  91. Object.defineProperty(newOption, 'props', {
  92. get: function get() {
  93. warning(false, 'Return type is option instead of Option instance. Please read value directly instead of reading from `props`.');
  94. return newOption;
  95. }
  96. });
  97. }
  98. return newOption;
  99. }
  100. export var getSeparatedContent = function getSeparatedContent(text, tokens, end) {
  101. if (!tokens || !tokens.length) {
  102. return null;
  103. }
  104. var match = false;
  105. var separate = function separate(str, _ref3) {
  106. var _ref4 = _toArray(_ref3),
  107. token = _ref4[0],
  108. restTokens = _ref4.slice(1);
  109. if (!token) {
  110. return [str];
  111. }
  112. var list = str.split(token);
  113. match = match || list.length > 1;
  114. return list.reduce(function (prevList, unitStr) {
  115. return [].concat(_toConsumableArray(prevList), _toConsumableArray(separate(unitStr, restTokens)));
  116. }, []).filter(Boolean);
  117. };
  118. var list = separate(text, tokens);
  119. if (match) {
  120. return typeof end !== 'undefined' ? list.slice(0, end) : list;
  121. } else {
  122. return null;
  123. }
  124. };