Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Oct 15, 2024
1 parent 72d007a commit 5c72bb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
19 changes: 10 additions & 9 deletions src/hooks/useCheckedKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ export default (
keyEntities: Record<SafeKey, DataEntity>,
) =>
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]);
16 changes: 5 additions & 11 deletions src/hooks/useFilterTreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultOptionType[]>((total, dataNode) => {
Expand Down
26 changes: 13 additions & 13 deletions src/utils/valueUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 5c72bb5

Please sign in to comment.