Skip to content

Commit

Permalink
fix(comp-tree): support comp-tree-foreach stop,add insertNodesByKey (#…
Browse files Browse the repository at this point in the history
…818)

Co-authored-by: git <[email protected]>
  • Loading branch information
bbkkkk8 and git authored Jun 26, 2021
1 parent 480cfb9 commit d97aa92
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/Tree/src/typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface TreeActionType {
getCheckedKeys: () => CheckKeys;
filterByLevel: (level: number) => void;
insertNodeByKey: (opt: InsertNodeParams) => void;
insertNodesByKey: (opt: InsertNodeParams) => void;
deleteNodeByKey: (key: string) => void;
updateNodeByKey: (key: string, node: Omit<TreeDataItem, 'key'>) => void;
}
Expand Down
37 changes: 36 additions & 1 deletion src/components/Tree/src/useTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,39 @@ export function useTree(
if (treeItem[keyField] === parentKey) {
treeItem[childrenField] = treeItem[childrenField] || [];
treeItem[childrenField][push](node);
return true;
}
});
treeDataRef.value = treeData;
}
/**
* 批量添加节点
*/
function insertNodesByKey({ parentKey = null, list, push = 'push' }: InsertNodeParams) {
const treeData: any = cloneDeep(unref(treeDataRef));
if (!list || list.length < 1) {
return;
}
if (!parentKey) {
for (let i = 0; i < list.length; i++) {
treeData[push](list[i]);
}
} else {
const { key: keyField, children: childrenField } = unref(getReplaceFields);
if (!childrenField || !keyField) return;

forEach(treeData, (treeItem) => {
if (treeItem[keyField] === parentKey) {
treeItem[childrenField] = treeItem[childrenField] || [];
for (let i = 0; i < list.length; i++) {
treeItem[childrenField][push](list[i]);
}
treeDataRef.value = treeData;
return true;
}
});
}
}
// Delete node
function deleteNodeByKey(key: string, list?: TreeDataItem[]) {
if (!key) return;
Expand All @@ -111,5 +139,12 @@ export function useTree(
}
}
}
return { deleteNodeByKey, insertNodeByKey, filterByLevel, updateNodeByKey, getAllKeys };
return {
deleteNodeByKey,
insertNodeByKey,
insertNodesByKey,
filterByLevel,
updateNodeByKey,
getAllKeys,
};
}
5 changes: 4 additions & 1 deletion src/utils/helper/treeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ export function forEach<T = any>(
const list: any[] = [...tree];
const { children } = config;
for (let i = 0; i < list.length; i++) {
func(list[i]);
//func 返回true就终止遍历,避免大量节点场景下无意义循环,引起浏览器卡顿
if (func(list[i])) {
return;
}
children && list[i][children] && list.splice(i + 1, 0, ...list[i][children]);
}
}
Expand Down

0 comments on commit d97aa92

Please sign in to comment.