1 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
2 | import _typeof from 'babel-runtime/helpers/typeof';
|
3 | import _extends from 'babel-runtime/helpers/extends';
|
4 | import { Children } from 'react';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export function isSingle(mode) {
|
16 | return !mode || mode === 'single';
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export function isNull(n) {
|
25 | return n === null || n === undefined;
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export function escapeForReg(str) {
|
34 | return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | export function filter(key, item) {
|
44 | var _key = escapeForReg('' + key);
|
45 | var regExp = new RegExp('(' + _key + ')', 'ig');
|
46 |
|
47 | return regExp.test('' + item.value) || regExp.test('' + item.label);
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | export function loopMap(dataSource, callback) {
|
60 | var result = [];
|
61 | dataSource.forEach(function (option) {
|
62 | if (option.children) {
|
63 | var children = loopMap(option.children, callback);
|
64 | result.push(_extends({}, option, {
|
65 | children: children
|
66 | }));
|
67 | } else {
|
68 |
|
69 | var tmp = callback(option);
|
70 | tmp && result.push(tmp);
|
71 | }
|
72 | });
|
73 |
|
74 | return result;
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | export function parseDataSourceFromChildren(children) {
|
84 | var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
85 |
|
86 | var source = [];
|
87 |
|
88 | Children.forEach(children, function (child, index) {
|
89 | if (!child) {
|
90 | return;
|
91 | }
|
92 | var type = child.type,
|
93 | childProps = child.props;
|
94 |
|
95 | var item2 = { deep: deep };
|
96 |
|
97 | var isOption = false;
|
98 | var isOptionGroup = false;
|
99 |
|
100 | if (typeof type === 'function' && type._typeMark === 'next_select_option' || type === 'option') {
|
101 | isOption = true;
|
102 | }
|
103 | if (typeof type === 'function' && type._typeMark === 'next_select_option_group' || type === 'optgroup') {
|
104 | isOptionGroup = true;
|
105 | }
|
106 |
|
107 | if (!isOption && !isOptionGroup) {
|
108 | return;
|
109 | }
|
110 |
|
111 | if (isOption) {
|
112 |
|
113 |
|
114 | var isStrChild = typeof childProps.children === 'string';
|
115 |
|
116 | item2.value = 'value' in childProps ? childProps.value : 'key' in childProps ? childProps.key : isStrChild ? childProps.children : '' + index;
|
117 |
|
118 | item2.label = childProps.label || childProps.children || '' + item2.value;
|
119 | if ('title' in childProps) {
|
120 | item2.title = childProps.title;
|
121 | }
|
122 | childProps.disabled === true && (item2.disabled = true);
|
123 |
|
124 | _extends(item2, childProps['data-extra'] || {});
|
125 | } else if (isOptionGroup && deep < 1) {
|
126 |
|
127 | item2.label = childProps.label || 'Group';
|
128 |
|
129 | item2.children = parseDataSourceFromChildren(childProps.children, deep + 1);
|
130 | }
|
131 |
|
132 | source.push(item2);
|
133 | });
|
134 |
|
135 | return source;
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | export function normalizeDataSource(dataSource) {
|
149 | var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
150 | var showDataSourceChildren = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
151 |
|
152 | var source = [];
|
153 |
|
154 | dataSource.forEach(function (item, index) {
|
155 |
|
156 | if (/string|boolean|number/.test(typeof item === 'undefined' ? 'undefined' : _typeof(item)) || item === null || item === undefined) {
|
157 | item = { label: '' + item, value: item };
|
158 | }
|
159 |
|
160 |
|
161 | if (item && item.__isAddon) {
|
162 | return;
|
163 | }
|
164 |
|
165 | var item2 = { deep: deep };
|
166 |
|
167 | if (Array.isArray(item.children) && deep < 1 && showDataSourceChildren) {
|
168 |
|
169 | item2.label = item.label || item.value || 'Group ' + index;
|
170 |
|
171 | item2.children = normalizeDataSource(item.children, deep + 1);
|
172 | } else {
|
173 | var _item = item,
|
174 | value = _item.value,
|
175 | label = _item.label,
|
176 | disabled = _item.disabled,
|
177 | title = _item.title,
|
178 | others = _objectWithoutProperties(_item, ['value', 'label', 'disabled', 'title']);
|
179 |
|
180 |
|
181 |
|
182 | item2.value = typeof value !== 'undefined' ? value : '' + index;
|
183 | item2.label = label || '' + item2.value;
|
184 | if ('title' in item) {
|
185 | item2.title = title;
|
186 | }
|
187 | disabled === true && (item2.disabled = true);
|
188 |
|
189 | _extends(item2, others);
|
190 | }
|
191 |
|
192 | source.push(item2);
|
193 | });
|
194 |
|
195 | return source;
|
196 | }
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | export function flattingDataSource(dataSource) {
|
205 | var source = [];
|
206 |
|
207 | dataSource.forEach(function (item) {
|
208 | if (Array.isArray(item.children)) {
|
209 | source.push.apply(source, flattingDataSource(item.children));
|
210 | } else {
|
211 | source.push(item);
|
212 | }
|
213 | });
|
214 |
|
215 | return source;
|
216 | }
|
217 |
|
218 | export function filterDataSource(dataSource, key, filter, addonKey) {
|
219 | if (!Array.isArray(dataSource)) {
|
220 | return [];
|
221 | }
|
222 | if (typeof key === 'undefined' || key === null) {
|
223 | return [].concat(dataSource);
|
224 | }
|
225 |
|
226 | var addKey = true;
|
227 | var menuDataSource = loopMap(dataSource, function (option) {
|
228 | if (key === '' + option.value) {
|
229 | addKey = false;
|
230 | }
|
231 | return filter(key, option) && !option.__isAddon && option;
|
232 | });
|
233 |
|
234 |
|
235 | if (addonKey && key && addKey) {
|
236 | menuDataSource.unshift({
|
237 | value: key,
|
238 | label: key,
|
239 | __isAddon: true
|
240 | });
|
241 | }
|
242 |
|
243 | return menuDataSource;
|
244 | }
|
245 |
|
246 | function getKeyItemByValue(value, valueMap) {
|
247 | var item = void 0;
|
248 |
|
249 | if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
|
250 | if (value.hasOwnProperty('value')) {
|
251 | item = value;
|
252 | } else {
|
253 | item = _extends({
|
254 | value: ''
|
255 | }, value);
|
256 | }
|
257 | } else {
|
258 | item = valueMap['' + value] || {
|
259 | value: value,
|
260 | label: value
|
261 | };
|
262 | }
|
263 |
|
264 | return item;
|
265 | }
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | export function getValueDataSource(value, mapValueDS, mapMenuDS) {
|
275 | if (isNull(value)) {
|
276 | return {};
|
277 | }
|
278 |
|
279 | var newValue = [];
|
280 | var newValueDS = [];
|
281 | var newMapValueDS = {};
|
282 | var _newMapDS = _extends({}, mapValueDS, mapMenuDS);
|
283 |
|
284 | if (Array.isArray(value)) {
|
285 | value.forEach(function (v) {
|
286 | var item = getKeyItemByValue(v, _newMapDS);
|
287 |
|
288 | newValueDS.push(item);
|
289 | newMapValueDS['' + item.value] = item;
|
290 | newValue.push(item.value);
|
291 | });
|
292 |
|
293 | return {
|
294 | value: newValue,
|
295 | valueDS: newValueDS,
|
296 | mapValueDS: newMapValueDS
|
297 | };
|
298 | } else {
|
299 | var _mapValueDS;
|
300 |
|
301 | var item = getKeyItemByValue(value, _newMapDS);
|
302 |
|
303 | return {
|
304 | value: item.value,
|
305 | valueDS: item,
|
306 | mapValueDS: (_mapValueDS = {}, _mapValueDS['' + item.value] = item, _mapValueDS)
|
307 | };
|
308 | }
|
309 | }
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | export function valueToSelectKey(value) {
|
318 | var val = void 0;
|
319 | if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value.hasOwnProperty('value')) {
|
320 | val = value.value;
|
321 | } else {
|
322 | val = value;
|
323 | }
|
324 | return '' + val;
|
325 | }
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
\ | No newline at end of file |