valueUtil.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.fillFieldNames = fillFieldNames;
  7. exports.flattenOptions = flattenOptions;
  8. exports.getSeparatedContent = getSeparatedContent;
  9. exports.injectPropsWithOption = injectPropsWithOption;
  10. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  11. var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
  12. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  13. var _warning = require("../../vc-util/warning");
  14. function getKey(data, index) {
  15. var key = data.key;
  16. var value;
  17. if ('value' in data) {
  18. value = data.value;
  19. }
  20. if (key !== null && key !== undefined) {
  21. return key;
  22. }
  23. if (value !== undefined) {
  24. return value;
  25. }
  26. return "rc-index-key-".concat(index);
  27. }
  28. function fillFieldNames(fieldNames, childrenAsData) {
  29. var _ref = fieldNames || {},
  30. label = _ref.label,
  31. value = _ref.value,
  32. options = _ref.options;
  33. return {
  34. label: label || (childrenAsData ? 'children' : 'label'),
  35. value: value || 'value',
  36. options: options || 'options'
  37. };
  38. }
  39. /**
  40. * Flat options into flatten list.
  41. * We use `optionOnly` here is aim to avoid user use nested option group.
  42. * Here is simply set `key` to the index if not provided.
  43. */
  44. function flattenOptions(options) {
  45. var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  46. fieldNames = _ref2.fieldNames,
  47. childrenAsData = _ref2.childrenAsData;
  48. var flattenList = [];
  49. var _fillFieldNames = fillFieldNames(fieldNames, false),
  50. fieldLabel = _fillFieldNames.label,
  51. fieldValue = _fillFieldNames.value,
  52. fieldOptions = _fillFieldNames.options;
  53. function dig(list, isGroupOption) {
  54. list.forEach(function (data) {
  55. var label = data[fieldLabel];
  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: label,
  64. value: value
  65. });
  66. } else {
  67. var grpLabel = label;
  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. function injectPropsWithOption(option) {
  89. var newOption = (0, _objectSpread2.default)({}, option);
  90. if (!('props' in newOption)) {
  91. Object.defineProperty(newOption, 'props', {
  92. get: function get() {
  93. (0, _warning.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. function getSeparatedContent(text, tokens) {
  101. if (!tokens || !tokens.length) {
  102. return null;
  103. }
  104. var match = false;
  105. function separate(str, _ref3) {
  106. var _ref4 = (0, _toArray2.default)(_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((0, _toConsumableArray2.default)(prevList), (0, _toConsumableArray2.default)(separate(unitStr, restTokens)));
  116. }, []).filter(function (unit) {
  117. return unit;
  118. });
  119. }
  120. var list = separate(text, tokens);
  121. return match ? list : null;
  122. }