Skip to content

Commit 982e986

Browse files
committed
revert refactor
1 parent d7f5671 commit 982e986

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

src/utils/strategyUtil.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export function formatStrategyValues(
1717
): SafeKey[] {
1818
const valueSet = new Set(values);
1919

20-
const strategyHandlers = {
21-
[SHOW_CHILD]: (key: SafeKey) => {
20+
if (strategy === SHOW_CHILD) {
21+
return values.filter(key => {
2222
const entity = keyEntities[key];
2323
return (
2424
!entity ||
@@ -28,14 +28,14 @@ export function formatStrategyValues(
2828
({ node }) => isCheckDisabled(node) || valueSet.has(node[fieldNames.value]),
2929
)
3030
);
31-
},
32-
[SHOW_PARENT]: (key: SafeKey) => {
31+
});
32+
}
33+
if (strategy === SHOW_PARENT) {
34+
return values.filter(key => {
3335
const entity = keyEntities[key];
34-
const parent = entity?.parent;
36+
const parent = entity ? entity.parent : null;
3537
return !parent || isCheckDisabled(parent.node) || !valueSet.has(parent.key);
36-
},
37-
[SHOW_ALL]: () => true,
38-
};
39-
40-
return values.filter(strategyHandlers[strategy] || strategyHandlers[SHOW_ALL]);
38+
});
39+
}
40+
return values;
4141
}

src/utils/valueUtil.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
11
import type { DataNode, FieldNames, SafeKey } from '../interface';
22
import type { DefaultOptionType, InternalFieldName } from '../TreeSelect';
33

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) => {
128
const { label, value, children } = fieldNames || {};
139
return {
1410
_title: label ? [label] : ['title', 'label'],
1511
value: value || 'value',
1612
key: value || 'value',
1713
children: children || 'children',
1814
};
19-
}
15+
};
2016

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;
2419

25-
/** 递归获取树中所有存在的键 */
26-
export function getAllKeys(
20+
export const getAllKeys = (
2721
treeData: DefaultOptionType[],
2822
fieldNames: InternalFieldName,
29-
): SafeKey[] {
23+
): SafeKey[] => {
3024
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);
3732
}
3833
});
3934
};
40-
traverseTree(treeData);
35+
36+
dig(treeData);
37+
4138
return keys;
42-
}
39+
};
4340

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

Comments
 (0)