Skip to content

Commit 9ff3181

Browse files
committed
chore: change limit maxCount
1 parent 8f47675 commit 9ff3181

File tree

4 files changed

+44
-58
lines changed

4 files changed

+44
-58
lines changed

examples/mutiple-with-maxCount.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export default () => {
8080
<h2>checkable with maxCount</h2>
8181
<TreeSelect
8282
style={{ width: 300 }}
83-
multiple
8483
treeCheckable
8584
// showCheckedStrategy="SHOW_ALL"
8685
// showCheckedStrategy="SHOW_PARENT"

src/OptionList.tsx

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
4848
treeExpandAction,
4949
treeTitleRender,
5050
onPopupScroll,
51-
isOverMaxCount,
52-
maxCount,
51+
leftMaxCount,
5352
showCheckedStrategy,
5453
} = React.useContext(TreeSelectContext);
5554

@@ -160,33 +159,19 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
160159
// eslint-disable-next-line react-hooks/exhaustive-deps
161160
}, [searchValue]);
162161

163-
const disabledCacheRef = React.useRef(new Map<Key, boolean>());
164-
const lastCheckedKeysRef = React.useRef<Key[]>([]);
165-
const lastMaxCountRef = React.useRef<number>(null);
162+
// const getSelectableKeys = (targetNode: DataNode, names: FieldNames): Key[] => {
163+
// const keys = [targetNode[names.value]];
164+
// if (!Array.isArray(targetNode.children)) {
165+
// return keys;
166+
// }
166167

167-
const resetCache = React.useCallback(() => {
168-
disabledCacheRef.current.clear();
169-
lastCheckedKeysRef.current = [...checkedKeys];
170-
lastMaxCountRef.current = maxCount;
171-
}, [checkedKeys, maxCount]);
172-
173-
React.useEffect(() => {
174-
resetCache();
175-
}, [checkedKeys, maxCount]);
176-
177-
const getSelectableKeys = (targetNode: DataNode, names: FieldNames): Key[] => {
178-
const keys = [targetNode[names.value]];
179-
if (!Array.isArray(targetNode.children)) {
180-
return keys;
181-
}
182-
183-
return targetNode.children.reduce((acc, child) => {
184-
if (!child.disabled) {
185-
acc.push(...getSelectableKeys(child, names));
186-
}
187-
return acc;
188-
}, keys);
189-
};
168+
// return targetNode.children.reduce((acc, child) => {
169+
// if (!child.disabled) {
170+
// acc.push(...getSelectableKeys(child, names));
171+
// }
172+
// return acc;
173+
// }, keys);
174+
// };
190175

191176
const nodeDisabled = useEvent((node: DataNode) => {
192177
const nodeValue = node[fieldNames.value];
@@ -195,33 +180,35 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
195180
return false;
196181
}
197182

198-
if (isOverMaxCount) {
199-
return true;
183+
if (leftMaxCount === null) {
184+
return false;
200185
}
201186

202-
const cacheKey = `${nodeValue}-${checkedKeys.join(',')}-${maxCount}`;
187+
// const cacheKey = `${nodeValue}-${checkedKeys.join(',')}-${maxCount}`;
203188

204-
// check cache
205-
if (disabledCacheRef.current.has(cacheKey)) {
206-
return disabledCacheRef.current.get(cacheKey);
207-
}
189+
// // check cache
190+
// if (disabledCacheRef.current.has(cacheKey)) {
191+
// return disabledCacheRef.current.get(cacheKey);
192+
// }
208193

209-
// calculate disabled state
210-
const selectableNodeKeys = getSelectableKeys(node, fieldNames);
211-
const simulatedCheckedKeys = [...checkedKeys, ...selectableNodeKeys];
212-
const simulatedDisplayValues = formatStrategyValues(
213-
simulatedCheckedKeys as SafeKey[],
214-
showCheckedStrategy,
215-
keyEntities,
216-
fieldNames,
217-
);
194+
// // calculate disabled state
195+
// const selectableNodeKeys = getSelectableKeys(node, fieldNames);
196+
// const simulatedCheckedKeys = [...checkedKeys, ...selectableNodeKeys];
197+
// const simulatedDisplayValues = formatStrategyValues(
198+
// simulatedCheckedKeys as SafeKey[],
199+
// showCheckedStrategy,
200+
// keyEntities,
201+
// fieldNames,
202+
// );
203+
204+
// const isDisabled = simulatedDisplayValues.length > maxCount;
218205

219-
const isDisabled = simulatedDisplayValues.length > maxCount;
206+
// // update cache
207+
// disabledCacheRef.current.set(cacheKey, isDisabled);
220208

221-
// update cache
222-
disabledCacheRef.current.set(cacheKey, isDisabled);
209+
// return isDisabled;
223210

224-
return isDisabled;
211+
return false;
225212
});
226213

227214
// ========================== Get First Selectable Node ==========================

src/TreeSelect.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
422422
mergedFieldNames,
423423
);
424424

425+
// Not allow pass with `maxCount`
426+
if (maxCount && formattedKeyList.length > maxCount) {
427+
return;
428+
}
429+
425430
const labeledValues = convert2LabelValues(newRawValues);
426431
setInternalValue(labeledValues);
427432

@@ -600,9 +605,6 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
600605
});
601606

602607
// ========================== Context ===========================
603-
const isOverMaxCount =
604-
mergedMultiple && maxCount !== undefined && cachedDisplayValues?.length >= maxCount;
605-
606608
const treeSelectContext = React.useMemo<TreeSelectContextProps>(() => {
607609
return {
608610
virtual,
@@ -616,8 +618,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
616618
treeExpandAction,
617619
treeTitleRender,
618620
onPopupScroll,
619-
isOverMaxCount,
620-
maxCount,
621+
leftMaxCount: maxCount ? maxCount - cachedDisplayValues.length : null,
621622
showCheckedStrategy: mergedShowCheckedStrategy,
622623
};
623624
}, [
@@ -633,7 +634,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
633634
treeTitleRender,
634635
onPopupScroll,
635636
maxCount,
636-
mergedMultiple,
637+
cachedDisplayValues.length,
637638
mergedShowCheckedStrategy,
638639
]);
639640

src/TreeSelectContext.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import type { ExpandAction } from 'rc-tree/lib/Tree';
33
import type { DataNode, FieldNames, Key } from './interface';
4-
import { CheckedStrategy } from './utils/strategyUtil';
4+
import type { CheckedStrategy } from './utils/strategyUtil';
55

66
export interface TreeSelectContextProps {
77
virtual?: boolean;
@@ -15,8 +15,7 @@ export interface TreeSelectContextProps {
1515
treeExpandAction?: ExpandAction;
1616
treeTitleRender?: (node: any) => React.ReactNode;
1717
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
18-
isOverMaxCount?: boolean;
19-
maxCount?: number;
18+
leftMaxCount?: number | null;
2019
showCheckedStrategy?: CheckedStrategy;
2120
}
2221

0 commit comments

Comments
 (0)