Skip to content

Commit 0f48638

Browse files
committed
Optimize:Optimized tree structure search
1 parent 4a1ab78 commit 0f48638

5 files changed

Lines changed: 71 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 3.1.12
2+
3+
`2023-12-15`
4+
5+
**Changelog**
6+
7+
- ⚡️【Optimize】Optimized tree structure search
8+
- ⚡️【Optimize】Tree structure search box resident
9+
110
## 3.1.11
211

312
`2023-12-13`

CHANGELOG_CN.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 3.1.12
2+
3+
`2023-12-15`
4+
5+
**更新日志**
6+
7+
- ⚡️【优化】优化树结构搜索
8+
- ⚡️【优化】树结构搜索框常驻
9+
110
## 3.1.11
211

312
`2023-12-13`

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo, useEffect, useMemo, useState, createContext, useContext, useRef } from 'react';
1+
import React, { memo, useEffect, useMemo, useState, createContext, useContext } from 'react';
22
import styles from './index.less';
33
import classnames from 'classnames';
44
import Iconfont from '@/components/Iconfont';
@@ -48,6 +48,40 @@ const smoothTree = (treeData: ITreeNode[], result: ITreeNode[] = [], parentNode?
4848
return result;
4949
};
5050

51+
// 判断是否匹配
52+
const isMatch = (target: string, searchValue: string) => {
53+
const reg = new RegExp(searchValue, 'i');
54+
return reg.test(target || '');
55+
};
56+
57+
// 树结构搜索
58+
function searchTree(treeData: ITreeNode[], searchValue: string): ITreeNode[] {
59+
let result: ITreeNode[] = [];
60+
function dfs(node: ITreeNode, path: ITreeNode[] = []) {
61+
if (isMatch(node.name, searchValue)) {
62+
result = [...path, node];
63+
return true;
64+
}
65+
if (!node.children) return false;
66+
for (const child of node.children) {
67+
if (dfs(child, [...path, node])) return true;
68+
}
69+
return false;
70+
}
71+
72+
treeData.some((node) => dfs(node));
73+
74+
result.forEach((item) => {
75+
if(!isMatch(item.name, searchValue)){
76+
item.children = null;
77+
}
78+
});
79+
80+
const smoothTreeList: ITreeNode[] = []
81+
smoothTree(result, smoothTreeList);
82+
return smoothTreeList;
83+
}
84+
5185
const itemHeight = 26; // 每个 item 的高度
5286
const paddingCount = 2;
5387

@@ -89,17 +123,13 @@ const Tree = (props: IProps) => {
89123
}, [smoothTreeData, searchTreeData, startIdx]);
90124

91125
useEffect(() => {
92-
if (searchValue) {
93-
const ls = smoothTreeData.filter((item) => {
94-
const reg = new RegExp(searchValue, 'i');
95-
return reg.test(item.name || '');
96-
});
97-
setSearchTreeData(ls);
126+
if (searchValue && treeData) {
127+
setSearchTreeData(searchTree(cloneDeep(treeData), searchValue));
98128
} else {
99129
setSearchTreeData(null);
100130
}
101131
setScrollTop(0);
102-
}, [searchValue]);
132+
}, [searchValue, smoothTreeData]);
103133

104134
return (
105135
<LoadingContent isLoading={!treeData} className={classnames(className)}>

chat2db-client/src/pages/main/workspace/components/OperationLine/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
.searchBox{
2020
flex-shrink: 0;
21-
padding: 2px 4px;
21+
padding: 4px;
2222
border-top: 1px solid var(--color-border-secondary);
2323
}

chat2db-client/src/pages/main/workspace/components/OperationLine/index.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const OperationLine = (props: IProps) => {
7474
boxSize={20}
7575
size={14}
7676
/>
77-
{searchIng ? (
77+
{/* {searchIng ? (
7878
<Iconfont
7979
onClick={() => {
8080
setSearchIng(false);
@@ -94,22 +94,22 @@ const OperationLine = (props: IProps) => {
9494
boxSize={20}
9595
size={14}
9696
/>
97-
)}
97+
)} */}
9898
</div>
9999
{/* <div>1</div> */}
100100
</div>
101-
{searchIng && (
102-
<div className={styles.searchBox}>
103-
<Input
104-
size="small"
105-
prefix={<Iconfont code="&#xe888;" />}
106-
value={searchValue}
107-
onChange={(e) => setSearchValue(e.target.value)}
108-
allowClear
109-
placeholder={i18n('workspace.tree.search.placeholder')}
110-
/>
111-
</div>
112-
)}
101+
{/* {searchIng && ( */}
102+
<div className={styles.searchBox}>
103+
<Input
104+
size="small"
105+
prefix={<Iconfont code="&#xe888;" />}
106+
value={searchValue}
107+
onChange={(e) => setSearchValue(e.target.value)}
108+
allowClear
109+
placeholder={i18n('workspace.tree.search.placeholder')}
110+
/>
111+
</div>
112+
{/* )} */}
113113
</>
114114
);
115115
};

0 commit comments

Comments
 (0)