UNPKG

11.1 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
6
7var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
8
9var _typeof2 = require('babel-runtime/helpers/typeof');
10
11var _typeof3 = _interopRequireDefault(_typeof2);
12
13var _extends2 = require('babel-runtime/helpers/extends');
14
15var _extends3 = _interopRequireDefault(_extends2);
16
17exports.isSingle = isSingle;
18exports.isNull = isNull;
19exports.escapeForReg = escapeForReg;
20exports.filter = filter;
21exports.loopMap = loopMap;
22exports.parseDataSourceFromChildren = parseDataSourceFromChildren;
23exports.normalizeDataSource = normalizeDataSource;
24exports.flattingDataSource = flattingDataSource;
25exports.filterDataSource = filterDataSource;
26exports.getValueDataSource = getValueDataSource;
27exports.valueToSelectKey = valueToSelectKey;
28
29var _react = require('react');
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33/**
34 * util module
35 */
36
37/**
38 * 是否是单选模式
39 * @param {string} mode
40 * @return {boolean} is single mode
41 */
42function isSingle(mode) {
43 return !mode || mode === 'single';
44}
45
46/**
47 * 在 Select 中,认为 null 和 undefined 都是空值
48 * @param {*} n any object
49 * @return {boolean}
50 */
51function isNull(n) {
52 return n === null || n === undefined;
53}
54
55/**
56 * 将字符串中的正则表达式关键字符添加转义
57 * @param {string} str
58 * @return {string}
59 */
60function escapeForReg(str) {
61 return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
62}
63
64/**
65 * filter by key
66 * @param {string} key filter key
67 * @param {object} item item object
68 * @return {boolean} it's filtered
69 */
70function filter(key, item) {
71 var _key = escapeForReg('' + key);
72 var regExp = new RegExp('(' + _key + ')', 'ig');
73
74 return regExp.test('' + item.value) || regExp.test('' + item.label);
75}
76
77/**
78 * loop map
79 * @param {Array} dataSource
80 * @param {function} callback
81 * @return {Array}
82 * ----
83 * @callback ~loopCallback
84 * @param {object} option
85 */
86function loopMap(dataSource, callback) {
87 var result = [];
88 dataSource.forEach(function (option) {
89 if (option.children) {
90 var children = loopMap(option.children, callback);
91 result.push((0, _extends3.default)({}, option, {
92 children: children
93 }));
94 } else {
95 // eslint-disable-next-line callback-return
96 var tmp = callback(option);
97 tmp && result.push(tmp);
98 }
99 });
100
101 return result;
102}
103
104/**
105 * Parse dataSource from MenuItem
106 * @static
107 * @param {Array<Element>} children
108 * @param {number} [deep=0] recursion deep level
109 */
110function parseDataSourceFromChildren(children) {
111 var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
112
113 var source = [];
114
115 _react.Children.forEach(children, function (child, index) {
116 if (!child) {
117 return;
118 }
119 var type = child.type,
120 childProps = child.props;
121
122 var item2 = { deep: deep };
123
124 var isOption = false;
125 var isOptionGroup = false;
126
127 if (typeof type === 'function' && type._typeMark === 'next_select_option' || type === 'option') {
128 isOption = true;
129 }
130 if (typeof type === 'function' && type._typeMark === 'next_select_option_group' || type === 'optgroup') {
131 isOptionGroup = true;
132 }
133
134 if (!isOption && !isOptionGroup) {
135 return;
136 }
137
138 if (isOption) {
139 // option
140 // If children is a string, it can be used as value
141 var isStrChild = typeof childProps.children === 'string';
142 // value > key > string children > index
143 item2.value = 'value' in childProps ? childProps.value : 'key' in childProps ? childProps.key : isStrChild ? childProps.children : '' + index;
144
145 item2.label = childProps.label || childProps.children || '' + item2.value;
146 if ('title' in childProps) {
147 item2.title = childProps.title;
148 }
149 childProps.disabled === true && (item2.disabled = true);
150 // You can put your extra data here, and use it in `itemRender` or `labelRender`
151 (0, _extends3.default)(item2, childProps['data-extra'] || {});
152 } else if (isOptionGroup && deep < 1) {
153 // option group
154 item2.label = childProps.label || 'Group';
155 // parse children nodes
156 item2.children = parseDataSourceFromChildren(childProps.children, deep + 1);
157 }
158
159 source.push(item2);
160 });
161
162 return source;
163}
164
165/**
166 * Normalize dataSource
167 * @static
168 * @param {Array} dataSource
169 * @param {number} [deep=0] recursion deep level
170 * ----
171 * value priority: value > 'index'
172 * label priority: label > 'value' > 'index'
173 * disabled: disabled === true
174 */
175function normalizeDataSource(dataSource) {
176 var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
177 var showDataSourceChildren = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
178
179 var source = [];
180
181 dataSource.forEach(function (item, index) {
182 // enable array of basic type
183 if (/string|boolean|number/.test(typeof item === 'undefined' ? 'undefined' : (0, _typeof3.default)(item)) || item === null || item === undefined) {
184 item = { label: '' + item, value: item };
185 }
186
187 // filter off addon item
188 if (item && item.__isAddon) {
189 return;
190 }
191
192 var item2 = { deep: deep };
193 // deep < 1: only 2 level allowed
194 if (Array.isArray(item.children) && deep < 1 && showDataSourceChildren) {
195 // handle group
196 item2.label = item.label || item.value || 'Group ' + index;
197 // parse children
198 item2.children = normalizeDataSource(item.children, deep + 1);
199 } else {
200 var _item = item,
201 value = _item.value,
202 label = _item.label,
203 disabled = _item.disabled,
204 title = _item.title,
205 others = (0, _objectWithoutProperties3.default)(_item, ['value', 'label', 'disabled', 'title']);
206 // undefined 认为是没传取 index 值替代
207
208 item2.value = typeof value !== 'undefined' ? value : '' + index;
209 item2.label = label || '' + item2.value;
210 if ('title' in item) {
211 item2.title = title;
212 }
213 disabled === true && (item2.disabled = true);
214
215 (0, _extends3.default)(item2, others);
216 }
217
218 source.push(item2);
219 });
220
221 return source;
222}
223
224/**
225 * Get flatten dataSource
226 * @static
227 * @param {Array} dataSource structured dataSource
228 * @return {Array}
229 */
230function flattingDataSource(dataSource) {
231 var source = [];
232
233 dataSource.forEach(function (item) {
234 if (Array.isArray(item.children)) {
235 source.push.apply(source, flattingDataSource(item.children));
236 } else {
237 source.push(item);
238 }
239 });
240
241 return source;
242}
243
244function filterDataSource(dataSource, key, filter, addonKey) {
245 if (!Array.isArray(dataSource)) {
246 return [];
247 }
248 if (typeof key === 'undefined' || key === null) {
249 return [].concat(dataSource);
250 }
251
252 var addKey = true;
253 var menuDataSource = loopMap(dataSource, function (option) {
254 if (key === '' + option.value) {
255 addKey = false;
256 }
257 return filter(key, option) && !option.__isAddon && option;
258 });
259
260 // if key not in menuDataSource, add key to dataSource
261 if (addonKey && key && addKey) {
262 menuDataSource.unshift({
263 value: key,
264 label: key,
265 __isAddon: true
266 });
267 }
268
269 return menuDataSource;
270}
271
272function getKeyItemByValue(value, valueMap) {
273 var item = void 0;
274
275 if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object') {
276 if (value.hasOwnProperty('value')) {
277 item = value;
278 } else {
279 item = (0, _extends3.default)({
280 value: ''
281 }, value);
282 }
283 } else {
284 item = valueMap['' + value] || {
285 value: value,
286 label: value
287 };
288 }
289
290 return item;
291}
292
293/**
294 * compute valueDataSource by new value
295 * @param {Array/String} value 数据
296 * @param {Object} mapValueDS 上个value的缓存数据 value => {value,label} 的映射关系表
297 * @param {*} mapMenuDS 通过 dataSource 建立 value => {value,label} 的映射关系表
298 * @returns {Object} value: [value]; valueDS: [{value,label}]; mapValueDS: {value: {value,label}}
299 */
300function getValueDataSource(value, mapValueDS, mapMenuDS) {
301 if (isNull(value)) {
302 return {};
303 }
304
305 var newValue = [];
306 var newValueDS = [];
307 var newMapValueDS = {};
308 var _newMapDS = (0, _extends3.default)({}, mapValueDS, mapMenuDS);
309
310 if (Array.isArray(value)) {
311 value.forEach(function (v) {
312 var item = getKeyItemByValue(v, _newMapDS);
313
314 newValueDS.push(item);
315 newMapValueDS['' + item.value] = item;
316 newValue.push(item.value);
317 });
318
319 return {
320 value: newValue, // [value]
321 valueDS: newValueDS, // [{value,label}]
322 mapValueDS: newMapValueDS // {value: {value,label}}
323 };
324 } else {
325 var _mapValueDS;
326
327 var item = getKeyItemByValue(value, _newMapDS);
328
329 return {
330 value: item.value,
331 valueDS: item,
332 mapValueDS: (_mapValueDS = {}, _mapValueDS['' + item.value] = item, _mapValueDS)
333 };
334 }
335}
336
337/**
338 * Get flatten dataSource
339 * @static
340 * @param {any} value structured dataSource
341 * @return {String}
342 */
343function valueToSelectKey(value) {
344 var val = void 0;
345 if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object' && value.hasOwnProperty('value')) {
346 val = value.value;
347 } else {
348 val = value;
349 }
350 return '' + val;
351}
352
353/**
354 * UP Down 改进双向链表方法
355 */
356// function DoubleLinkList(element){
357// this.prev = null;
358// this.next = null;
359// this.element = element;
360// }
361//
362// export function mapDoubleLinkList(dataSource){
363//
364// const mapDS = {};
365// let doubleLink = null;
366//
367// let head = null;
368// let tail = null;
369//
370// function append(element) {
371// if (!doubleLink) {
372// doubleLink = new DoubleLinkList(element);
373// head = doubleLink;
374// tail = doubleLink;
375// return doubleLink;
376// }
377//
378// const node = new DoubleLinkList(element);
379// tail.next = node;
380// node.prev = tail;
381// tail = node;
382//
383// return tail;
384// }
385//
386// dataSource.forEach((item => {
387// if (item.disabled) {
388// return;
389// }
390// mapDS[`${item.value}`] = append(item);
391// }));
392//
393// return mapDS;
394// }
395//
\No newline at end of file