@@ -9,7 +9,7 @@ import * as React from 'react';
9
9
import LegacyContext from './LegacyContext' ;
10
10
import TreeSelectContext from './TreeSelectContext' ;
11
11
import type { Key , SafeKey } from './interface' ;
12
- import { getAllKeys , isCheckDisabled } from './utils/valueUtil' ;
12
+ import { getAllKeys , isCheckDisabled , isValidCount } from './utils/valueUtil' ;
13
13
14
14
const HIDDEN_STYLE = {
15
15
width : 0 ,
@@ -45,6 +45,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
45
45
treeExpandAction,
46
46
treeTitleRender,
47
47
onPopupScroll,
48
+ maxCount,
48
49
} = React . useContext ( TreeSelectContext ) ;
49
50
50
51
const {
@@ -76,6 +77,11 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
76
77
( prev , next ) => next [ 0 ] && prev [ 1 ] !== next [ 1 ] ,
77
78
) ;
78
79
80
+ const isOverMaxCount = React . useMemo < boolean > (
81
+ ( ) => multiple && isValidCount ( maxCount ) && checkedKeys . length >= maxCount ,
82
+ [ checkedKeys , maxCount , multiple ] ,
83
+ ) ;
84
+
79
85
// ========================== Active ==========================
80
86
const [ activeKey , setActiveKey ] = React . useState < Key > ( null ) ;
81
87
const activeEntity = keyEntities [ activeKey as SafeKey ] ;
@@ -150,8 +156,13 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
150
156
return ;
151
157
}
152
158
159
+ const isSelected = ! checkedKeys . includes ( node . key ) ;
160
+ if ( maxCount && isSelected && checkedKeys . length >= maxCount ) {
161
+ return ;
162
+ }
163
+
153
164
onSelect ( node . key , {
154
- selected : ! checkedKeys . includes ( node . key ) ,
165
+ selected : isSelected ,
155
166
} ) ;
156
167
157
168
if ( ! multiple ) {
@@ -197,10 +208,10 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
197
208
} ) ) ;
198
209
199
210
const loadDataFun = useMemo (
200
- ( ) => searchValue ? null : ( loadData as any ) ,
211
+ ( ) => ( searchValue ? null : ( loadData as any ) ) ,
201
212
[ searchValue , treeExpandedKeys || expandedKeys ] ,
202
213
( [ preSearchValue ] , [ nextSearchValue , nextExcludeSearchExpandedKeys ] ) =>
203
- preSearchValue !== nextSearchValue && ! ! ( nextSearchValue || nextExcludeSearchExpandedKeys )
214
+ preSearchValue !== nextSearchValue && ! ! ( nextSearchValue || nextExcludeSearchExpandedKeys ) ,
204
215
) ;
205
216
206
217
// ========================== Render ==========================
0 commit comments