@@ -57,29 +57,45 @@ const isMatch = (target: string, searchValue: string) => {
5757// 树结构搜索
5858function 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
85101const 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