@@ -2,59 +2,49 @@ import * as React from 'react';
22import type { DefaultOptionType , InternalFieldName , TreeSelectProps } from '../TreeSelect' ;
33import { fillLegacyProps } from '../utils/legacyUtil' ;
44
5- type GetFuncType < T > = T extends boolean ? never : T ;
6- type FilterFn = GetFuncType < TreeSelectProps [ 'filterTreeNode' ] > ;
5+ type FilterFn = NonNullable < TreeSelectProps [ 'filterTreeNode' ] > ;
76
8- export default (
7+ const useFilterTreeData = (
98 treeData : DefaultOptionType [ ] ,
109 searchValue : string ,
11- {
12- treeNodeFilterProp,
13- filterTreeNode,
14- fieldNames,
15- } : {
10+ options : {
1611 fieldNames : InternalFieldName ;
1712 treeNodeFilterProp : string ;
1813 filterTreeNode : TreeSelectProps [ 'filterTreeNode' ] ;
1914 } ,
2015) => {
16+ const { fieldNames, treeNodeFilterProp, filterTreeNode } = options ;
2117 const { children : fieldChildren } = fieldNames ;
2218
2319 return React . useMemo ( ( ) => {
2420 if ( ! searchValue || filterTreeNode === false ) {
2521 return treeData ;
2622 }
2723
28- let filterOptionFunc : FilterFn ;
29- if ( typeof filterTreeNode === 'function' ) {
30- filterOptionFunc = filterTreeNode ;
31- } else {
32- const upperStr = searchValue . toUpperCase ( ) ;
33- filterOptionFunc = ( _ , dataNode ) => {
34- const value = dataNode [ treeNodeFilterProp ] ;
35-
36- return String ( value ) . toUpperCase ( ) . includes ( upperStr ) ;
37- } ;
38- }
39-
40- function dig ( list : DefaultOptionType [ ] , keepAll : boolean = false ) {
41- return list . reduce < DefaultOptionType [ ] > ( ( total , dataNode ) => {
42- const children = dataNode [ fieldChildren ] ;
43-
44- const match = keepAll || filterOptionFunc ( searchValue , fillLegacyProps ( dataNode ) ) ;
45- const childList = dig ( children || [ ] , match ) ;
46-
47- if ( match || childList . length ) {
48- total . push ( {
49- ...dataNode ,
24+ const filterOptionFunc : FilterFn =
25+ typeof filterTreeNode === 'function'
26+ ? filterTreeNode
27+ : ( _ , dataNode ) =>
28+ String ( dataNode [ treeNodeFilterProp ] ) . toUpperCase ( ) . includes ( searchValue . toUpperCase ( ) ) ;
29+
30+ const filterTreeNodes = ( nodes : DefaultOptionType [ ] , keepAll = false ) : DefaultOptionType [ ] =>
31+ nodes . reduce < DefaultOptionType [ ] > ( ( filtered , node ) => {
32+ const children = node [ fieldChildren ] ;
33+ const isMatch = keepAll || filterOptionFunc ( searchValue , fillLegacyProps ( node ) ) ;
34+ const filteredChildren = filterTreeNodes ( children || [ ] , isMatch ) ;
35+
36+ if ( isMatch || filteredChildren . length ) {
37+ filtered . push ( {
38+ ...node ,
5039 isLeaf : undefined ,
51- [ fieldChildren ] : childList ,
40+ [ fieldChildren ] : filteredChildren ,
5241 } ) ;
5342 }
54- return total ;
43+ return filtered ;
5544 } , [ ] ) ;
56- }
5745
58- return dig ( treeData ) ;
46+ return filterTreeNodes ( treeData ) ;
5947 } , [ treeData , searchValue , fieldChildren , treeNodeFilterProp , filterTreeNode ] ) ;
6048} ;
49+
50+ export default useFilterTreeData ;
0 commit comments