valueUtil.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 '../../vc-util/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 fillFieldNames(fieldNames, childrenAsData) {
  20. var _ref = fieldNames || {},
  21. label = _ref.label,
  22. value = _ref.value,
  23. options = _ref.options;
  24. return {
  25. label: label || (childrenAsData ? 'children' : 'label'),
  26. value: value || 'value',
  27. options: options || 'options'
  28. };
  29. }
  30. /**
  31. * Flat options into flatten list.
  32. * We use `optionOnly` here is aim to avoid user use nested option group.
  33. * Here is simply set `key` to the index if not provided.
  34. */
  35. export function flattenOptions(options) {
  36. var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  37. fieldNames = _ref2.fieldNames,
  38. childrenAsData = _ref2.childrenAsData;
  39. var flattenList = [];
  40. var _fillFieldNames = fillFieldNames(fieldNames, false),
  41. fieldLabel = _fillFieldNames.label,
  42. fieldValue = _fillFieldNames.value,
  43. fieldOptions = _fillFieldNames.options;
  44. function dig(list, isGroupOption) {
  45. list.forEach(function (data) {
  46. var label = data[fieldLabel];
  47. if (isGroupOption || !(fieldOptions in data)) {
  48. var value = data[fieldValue];
  49. // Option
  50. flattenList.push({
  51. key: getKey(data, flattenList.length),
  52. groupOption: isGroupOption,
  53. data: data,
  54. label: label,
  55. value: value
  56. });
  57. } else {
  58. var grpLabel = label;
  59. if (grpLabel === undefined && childrenAsData) {
  60. grpLabel = data.label;
  61. }
  62. // Option Group
  63. flattenList.push({
  64. key: getKey(data, flattenList.length),
  65. group: true,
  66. data: data,
  67. label: grpLabel
  68. });
  69. dig(data[fieldOptions], true);
  70. }
  71. });
  72. }
  73. dig(options, false);
  74. return flattenList;
  75. }
  76. /**
  77. * Inject `props` into `option` for legacy usage
  78. */
  79. export function injectPropsWithOption(option) {
  80. var newOption = _objectSpread({}, option);
  81. if (!('props' in newOption)) {
  82. Object.defineProperty(newOption, 'props', {
  83. get: function get() {
  84. warning(false, 'Return type is option instead of Option instance. Please read value directly instead of reading from `props`.');
  85. return newOption;
  86. }
  87. });
  88. }
  89. return newOption;
  90. }
  91. export function getSeparatedContent(text, tokens) {
  92. if (!tokens || !tokens.length) {
  93. return null;
  94. }
  95. var match = false;
  96. function separate(str, _ref3) {
  97. var _ref4 = _toArray(_ref3),
  98. token = _ref4[0],
  99. restTokens = _ref4.slice(1);
  100. if (!token) {
  101. return [str];
  102. }
  103. var list = str.split(token);
  104. match = match || list.length > 1;
  105. return list.reduce(function (prevList, unitStr) {
  106. return [].concat(_toConsumableArray(prevList), _toConsumableArray(separate(unitStr, restTokens)));
  107. }, []).filter(function (unit) {
  108. return unit;
  109. });
  110. }
  111. var list = separate(text, tokens);
  112. return match ? list : null;
  113. }