UNPKG

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