Skip to content

Commit 7d90ce4

Browse files
committed
fix:Tree search bug
1 parent f2a5bc3 commit 7d90ce4

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 3.1.13
2+
3+
`2023-12-15`
4+
5+
**更新日志**
6+
7+
- 🐞【Fixed】Switching tab causes edit data reset problem
8+
- 🐞【Fixed】Rename is reset after switching tab
9+
10+
111
## 3.1.12
212

313
`2023-12-15`

CHANGELOG_CN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 3.1.13
2+
3+
`2023-12-15`
4+
5+
**更新日志**
6+
7+
- 🐞【修复】切换tab导致编辑数据重置问题
8+
- 🐞【修复】切换tab后重命名被重置
9+
10+
111
## 3.1.12
212

313
`2023-12-15`

chat2db-client/src/blocks/Tree/index.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,45 @@ const isMatch = (target: string, searchValue: string) => {
5757
// 树结构搜索
5858
function searchTree(treeData: ITreeNode[], searchValue: string): ITreeNode[] {
5959
let result: ITreeNode[] = [];
60+
61+
// 深度优先遍历
6062
function dfs(node: ITreeNode, path: ITreeNode[] = []) {
6163
if (isMatch(node.name, searchValue)) {
64+
// debugger
6265
result = [...result,...path, node];
63-
return true;
66+
// return true;
6467
}
6568
if (!node.children) return false;
6669
for (const child of node.children) {
67-
if (dfs(child, [...path, node])) return true;
70+
// debugger
71+
if (dfs(child, [...path, node])){
72+
return true;
73+
}
6874
}
6975
return false;
7076
}
7177

78+
// 遍历树
7279
treeData.forEach((node) => dfs(node));
7380

81+
// 如果不匹配,说明该节点为path,不需要保留该节点的子元素,就把children置空
7482
result.forEach((item) => {
7583
if(!isMatch(item.name, searchValue)){
7684
item.children = null;
7785
}
7886
});
7987

88+
// tree转平级
8089
const smoothTreeList: ITreeNode[] = []
8190
smoothTree(result, smoothTreeList);
82-
return smoothTreeList;
91+
92+
// 对smoothTreeList根据uuid去重
93+
const deWeightList: ITreeNode[] = [];
94+
smoothTreeList.forEach((item) => {
95+
deWeightList.findIndex((i) => i.uuid === item.uuid) === -1 && deWeightList.push(item);
96+
});
97+
98+
return deWeightList;
8399
}
84100

85101
const itemHeight = 26; // 每个 item 的高度
@@ -126,11 +142,11 @@ const Tree = (props: IProps) => {
126142
if (searchValue && treeData) {
127143
const _searchTreeData = searchTree(cloneDeep(treeData), searchValue)
128144
setSearchTreeData(_searchTreeData);
145+
setScrollTop(0);
129146
} else {
130147
setSearchTreeData(null);
131148
}
132-
setScrollTop(0);
133-
}, [searchValue, smoothTreeData,treeData]);
149+
}, [searchValue, treeData]);
134150

135151
return (
136152
<LoadingContent isLoading={!treeData} className={classnames(className)}>

0 commit comments

Comments
 (0)