From 5c72bb541a7fa099518990495ccd14c5e3b09fdb Mon Sep 17 00:00:00 2001 From: afc163 Date: Tue, 15 Oct 2024 12:09:47 +0800 Subject: [PATCH] refactor code --- src/hooks/useCheckedKeys.ts | 19 ++++++++++--------- src/hooks/useFilterTreeData.ts | 16 +++++----------- src/utils/valueUtil.ts | 26 +++++++++++++------------- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/hooks/useCheckedKeys.ts b/src/hooks/useCheckedKeys.ts index d5d1e938..6193b32f 100644 --- a/src/hooks/useCheckedKeys.ts +++ b/src/hooks/useCheckedKeys.ts @@ -10,19 +10,20 @@ export default ( keyEntities: Record, ) => React.useMemo(() => { - let checkedKeys: SafeKey[] = rawLabeledValues.map(({ value }) => value); - let halfCheckedKeys: SafeKey[] = rawHalfCheckedValues.map(({ value }) => value); + const getValues = (values: LabeledValueType[]) => values.map(({ value }) => value); + const checkedKeys = getValues(rawLabeledValues); + const halfCheckedKeys = getValues(rawHalfCheckedValues); const missingValues = checkedKeys.filter(key => !keyEntities[key]); - if (treeConduction) { - ({ checkedKeys, halfCheckedKeys } = conductCheck(checkedKeys, true, keyEntities)); - } + const finalCheckedKeys = treeConduction + ? conductCheck(checkedKeys, true, keyEntities).checkedKeys + : checkedKeys; return [ - // Checked keys should fill with missing keys which should de-duplicated - Array.from(new Set([...missingValues, ...checkedKeys])), - // Half checked keys - halfCheckedKeys, + Array.from(new Set([...missingValues, ...finalCheckedKeys])), + treeConduction + ? conductCheck(checkedKeys, true, keyEntities).halfCheckedKeys + : halfCheckedKeys, ]; }, [rawLabeledValues, rawHalfCheckedValues, treeConduction, keyEntities]); diff --git a/src/hooks/useFilterTreeData.ts b/src/hooks/useFilterTreeData.ts index 64373f10..9a16b19d 100644 --- a/src/hooks/useFilterTreeData.ts +++ b/src/hooks/useFilterTreeData.ts @@ -25,17 +25,11 @@ export default ( return treeData; } - let filterOptionFunc: FilterFn; - if (typeof filterTreeNode === 'function') { - filterOptionFunc = filterTreeNode; - } else { - const upperStr = searchValue.toUpperCase(); - filterOptionFunc = (_, dataNode) => { - const value = dataNode[treeNodeFilterProp]; - - return String(value).toUpperCase().includes(upperStr); - }; - } + const filterOptionFunc: FilterFn = + typeof filterTreeNode === 'function' + ? filterTreeNode + : (_, dataNode) => + String(dataNode[treeNodeFilterProp]).toUpperCase().includes(searchValue.toUpperCase()); function dig(list: DefaultOptionType[], keepAll: boolean = false) { return list.reduce((total, dataNode) => { diff --git a/src/utils/valueUtil.ts b/src/utils/valueUtil.ts index 220a0108..9e940a55 100644 --- a/src/utils/valueUtil.ts +++ b/src/utils/valueUtil.ts @@ -25,22 +25,22 @@ export function isCheckDisabled(node: DataNode) { return !node || node.disabled || node.disableCheckbox || node.checkable === false; } -/** Loop fetch all the keys exist in the tree */ -export function getAllKeys(treeData: DefaultOptionType[], fieldNames: InternalFieldName) { +/** 递归获取树中所有存在的键 */ +export function getAllKeys( + treeData: DefaultOptionType[], + fieldNames: InternalFieldName, +): SafeKey[] { const keys: SafeKey[] = []; - - function dig(list: DefaultOptionType[]) { - list.forEach(item => { - const children = item[fieldNames.children]; - if (children) { - keys.push(item[fieldNames.value]); - dig(children); + const traverseTree = (nodes: DefaultOptionType[]): void => { + nodes.forEach(node => { + keys.push(node[fieldNames.value]); + const children = node[fieldNames.children]; + if (Array.isArray(children)) { + traverseTree(children); } }); - } - - dig(treeData); - + }; + traverseTree(treeData); return keys; }