Skip to content

Commit 5c72bb5

Browse files
committed
refactor code
1 parent 72d007a commit 5c72bb5

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

src/hooks/useCheckedKeys.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ export default (
1010
keyEntities: Record<SafeKey, DataEntity>,
1111
) =>
1212
React.useMemo(() => {
13-
let checkedKeys: SafeKey[] = rawLabeledValues.map(({ value }) => value);
14-
let halfCheckedKeys: SafeKey[] = rawHalfCheckedValues.map(({ value }) => value);
13+
const getValues = (values: LabeledValueType[]) => values.map(({ value }) => value);
14+
const checkedKeys = getValues(rawLabeledValues);
15+
const halfCheckedKeys = getValues(rawHalfCheckedValues);
1516

1617
const missingValues = checkedKeys.filter(key => !keyEntities[key]);
1718

18-
if (treeConduction) {
19-
({ checkedKeys, halfCheckedKeys } = conductCheck(checkedKeys, true, keyEntities));
20-
}
19+
const finalCheckedKeys = treeConduction
20+
? conductCheck(checkedKeys, true, keyEntities).checkedKeys
21+
: checkedKeys;
2122

2223
return [
23-
// Checked keys should fill with missing keys which should de-duplicated
24-
Array.from(new Set([...missingValues, ...checkedKeys])),
25-
// Half checked keys
26-
halfCheckedKeys,
24+
Array.from(new Set([...missingValues, ...finalCheckedKeys])),
25+
treeConduction
26+
? conductCheck(checkedKeys, true, keyEntities).halfCheckedKeys
27+
: halfCheckedKeys,
2728
];
2829
}, [rawLabeledValues, rawHalfCheckedValues, treeConduction, keyEntities]);

src/hooks/useFilterTreeData.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ export default (
2525
return treeData;
2626
}
2727

28-
let filterOptionFunc: FilterFn;
29-
if (typeof filterTreeNode === 'function') {
30-
filterOptionFunc = filterTreeNode;
31-
} else {
32-
const upperStr = searchValue.toUpperCase();
33-
filterOptionFunc = (_, dataNode) => {
34-
const value = dataNode[treeNodeFilterProp];
35-
36-
return String(value).toUpperCase().includes(upperStr);
37-
};
38-
}
28+
const filterOptionFunc: FilterFn =
29+
typeof filterTreeNode === 'function'
30+
? filterTreeNode
31+
: (_, dataNode) =>
32+
String(dataNode[treeNodeFilterProp]).toUpperCase().includes(searchValue.toUpperCase());
3933

4034
function dig(list: DefaultOptionType[], keepAll: boolean = false) {
4135
return list.reduce<DefaultOptionType[]>((total, dataNode) => {

src/utils/valueUtil.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ export function isCheckDisabled(node: DataNode) {
2525
return !node || node.disabled || node.disableCheckbox || node.checkable === false;
2626
}
2727

28-
/** Loop fetch all the keys exist in the tree */
29-
export function getAllKeys(treeData: DefaultOptionType[], fieldNames: InternalFieldName) {
28+
/** 递归获取树中所有存在的键 */
29+
export function getAllKeys(
30+
treeData: DefaultOptionType[],
31+
fieldNames: InternalFieldName,
32+
): SafeKey[] {
3033
const keys: SafeKey[] = [];
31-
32-
function dig(list: DefaultOptionType[]) {
33-
list.forEach(item => {
34-
const children = item[fieldNames.children];
35-
if (children) {
36-
keys.push(item[fieldNames.value]);
37-
dig(children);
34+
const traverseTree = (nodes: DefaultOptionType[]): void => {
35+
nodes.forEach(node => {
36+
keys.push(node[fieldNames.value]);
37+
const children = node[fieldNames.children];
38+
if (Array.isArray(children)) {
39+
traverseTree(children);
3840
}
3941
});
40-
}
41-
42-
dig(treeData);
43-
42+
};
43+
traverseTree(treeData);
4444
return keys;
4545
}
4646

0 commit comments

Comments
 (0)