valueUtil.js 3.2 KB

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