Skip to content

Commit

Permalink
fix:Tree search bug
Browse files Browse the repository at this point in the history
  • Loading branch information
shanhexi committed Dec 15, 2023
1 parent f2a5bc3 commit 7d90ce4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 3.1.13

`2023-12-15`

**更新日志**

- 🐞【Fixed】Switching tab causes edit data reset problem
- 🐞【Fixed】Rename is reset after switching tab


## 3.1.12

`2023-12-15`
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG_CN.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 3.1.13

`2023-12-15`

**更新日志**

- 🐞【修复】切换tab导致编辑数据重置问题
- 🐞【修复】切换tab后重命名被重置


## 3.1.12

`2023-12-15`
Expand Down
26 changes: 21 additions & 5 deletions chat2db-client/src/blocks/Tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,45 @@ const isMatch = (target: string, searchValue: string) => {
// 树结构搜索
function searchTree(treeData: ITreeNode[], searchValue: string): ITreeNode[] {
let result: ITreeNode[] = [];

// 深度优先遍历
function dfs(node: ITreeNode, path: ITreeNode[] = []) {
if (isMatch(node.name, searchValue)) {
// debugger
result = [...result,...path, node];
return true;
// return true;
}
if (!node.children) return false;
for (const child of node.children) {
if (dfs(child, [...path, node])) return true;
// debugger
if (dfs(child, [...path, node])){
return true;
}
}
return false;
}

// 遍历树
treeData.forEach((node) => dfs(node));

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

// tree转平级
const smoothTreeList: ITreeNode[] = []
smoothTree(result, smoothTreeList);
return smoothTreeList;

// 对smoothTreeList根据uuid去重
const deWeightList: ITreeNode[] = [];
smoothTreeList.forEach((item) => {
deWeightList.findIndex((i) => i.uuid === item.uuid) === -1 && deWeightList.push(item);
});

return deWeightList;
}

const itemHeight = 26; // 每个 item 的高度
Expand Down Expand Up @@ -126,11 +142,11 @@ const Tree = (props: IProps) => {
if (searchValue && treeData) {
const _searchTreeData = searchTree(cloneDeep(treeData), searchValue)
setSearchTreeData(_searchTreeData);
setScrollTop(0);
} else {
setSearchTreeData(null);
}
setScrollTop(0);
}, [searchValue, smoothTreeData,treeData]);
}, [searchValue, treeData]);

return (
<LoadingContent isLoading={!treeData} className={classnames(className)}>
Expand Down

0 comments on commit 7d90ce4

Please sign in to comment.