|
1 | 1 | import type { DataNode, FieldNames, SafeKey } from '../interface';
|
2 | 2 | import type { DefaultOptionType, InternalFieldName } from '../TreeSelect';
|
3 | 3 |
|
4 |
| -export function toArray<T>(value: T | T[]): T[] { |
5 |
| - if (Array.isArray(value)) { |
6 |
| - return value; |
7 |
| - } |
8 |
| - return value !== undefined ? [value] : []; |
9 |
| -} |
10 |
| - |
11 |
| -export function fillFieldNames(fieldNames?: FieldNames) { |
| 4 | +export const toArray = <T>(value: T | T[]): T[] => |
| 5 | + Array.isArray(value) ? value : value !== undefined ? [value] : []; |
| 6 | + |
| 7 | +export const fillFieldNames = (fieldNames?: FieldNames) => { |
12 | 8 | const { label, value, children } = fieldNames || {};
|
13 | 9 | return {
|
14 | 10 | _title: label ? [label] : ['title', 'label'],
|
15 | 11 | value: value || 'value',
|
16 | 12 | key: value || 'value',
|
17 | 13 | children: children || 'children',
|
18 | 14 | };
|
19 |
| -} |
| 15 | +}; |
20 | 16 |
|
21 |
| -export function isCheckDisabled(node: DataNode) { |
22 |
| - return !node || node.disabled || node.disableCheckbox || node.checkable === false; |
23 |
| -} |
| 17 | +export const isCheckDisabled = (node: DataNode): boolean => |
| 18 | + !node || node.disabled || node.disableCheckbox || node.checkable === false; |
24 | 19 |
|
25 |
| -/** 递归获取树中所有存在的键 */ |
26 |
| -export function getAllKeys( |
| 20 | +export const getAllKeys = ( |
27 | 21 | treeData: DefaultOptionType[],
|
28 | 22 | fieldNames: InternalFieldName,
|
29 |
| -): SafeKey[] { |
| 23 | +): SafeKey[] => { |
30 | 24 | const keys: SafeKey[] = [];
|
31 |
| - const traverseTree = (nodes: DefaultOptionType[]): void => { |
32 |
| - nodes.forEach(node => { |
33 |
| - keys.push(node[fieldNames.value]); |
34 |
| - const children = node[fieldNames.children]; |
35 |
| - if (Array.isArray(children)) { |
36 |
| - traverseTree(children); |
| 25 | + |
| 26 | + const dig = (list: DefaultOptionType[]): void => { |
| 27 | + list.forEach(item => { |
| 28 | + const children = item[fieldNames.children]; |
| 29 | + if (children) { |
| 30 | + keys.push(item[fieldNames.value]); |
| 31 | + dig(children); |
37 | 32 | }
|
38 | 33 | });
|
39 | 34 | };
|
40 |
| - traverseTree(treeData); |
| 35 | + |
| 36 | + dig(treeData); |
| 37 | + |
41 | 38 | return keys;
|
42 |
| -} |
| 39 | +}; |
43 | 40 |
|
44 |
| -export function isNil(val: any) { |
45 |
| - return val === null || val === undefined; |
46 |
| -} |
| 41 | +export const isNil = (val: any): boolean => val === null || val === undefined; |
0 commit comments