valueUtil.js 2.9 KB

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