Skip to content

Commit c178539

Browse files
committed
chore: hoist state to context
1 parent c72c25f commit c178539

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/OptionList.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
4545
treeExpandAction,
4646
treeTitleRender,
4747
onPopupScroll,
48-
maxCount,
4948
displayValues,
49+
isOverMaxCount,
5050
} = React.useContext(TreeSelectContext);
5151

5252
const {
@@ -250,11 +250,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
250250
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys),
251251
);
252252

253-
const isOverMaxCount = React.useMemo<boolean>(
254-
() => multiple && maxCount !== undefined && displayValues?.length >= maxCount,
255-
[multiple, maxCount, displayValues?.length],
256-
);
257-
258253
const onActiveChange = (key: Key) => {
259254
if (isOverMaxCount && !displayValues?.some(v => v.value === key)) {
260255
return;
@@ -275,7 +270,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
275270

276271
const processedTreeData = React.useMemo(
277272
() => traverse(memoTreeData),
278-
[memoTreeData, isOverMaxCount, displayValues, fieldNames],
273+
[memoTreeData, isOverMaxCount],
279274
);
280275

281276
// ========================== Render ==========================

src/TreeSelect.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,11 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
613613
});
614614

615615
// ========================== Context ===========================
616-
const treeSelectContext = React.useMemo<TreeSelectContextProps>(
617-
() => ({
616+
const treeSelectContext = React.useMemo<TreeSelectContextProps>(() => {
617+
const isOverMaxCount =
618+
mergedMultiple && maxCount !== undefined && cachedDisplayValues?.length >= maxCount;
619+
620+
return {
618621
virtual,
619622
dropdownMatchSelectWidth,
620623
listHeight,
@@ -626,25 +629,25 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
626629
treeExpandAction,
627630
treeTitleRender,
628631
onPopupScroll,
629-
maxCount,
630632
displayValues: cachedDisplayValues,
631-
}),
632-
[
633-
virtual,
634-
dropdownMatchSelectWidth,
635-
listHeight,
636-
listItemHeight,
637-
listItemScrollOffset,
638-
filteredTreeData,
639-
mergedFieldNames,
640-
onOptionSelect,
641-
treeExpandAction,
642-
treeTitleRender,
643-
onPopupScroll,
644-
maxCount,
645-
cachedDisplayValues,
646-
],
647-
);
633+
isOverMaxCount,
634+
};
635+
}, [
636+
virtual,
637+
dropdownMatchSelectWidth,
638+
listHeight,
639+
listItemHeight,
640+
listItemScrollOffset,
641+
filteredTreeData,
642+
mergedFieldNames,
643+
onOptionSelect,
644+
treeExpandAction,
645+
treeTitleRender,
646+
onPopupScroll,
647+
maxCount,
648+
cachedDisplayValues,
649+
mergedMultiple,
650+
]);
648651

649652
// ======================= Legacy Context =======================
650653
const legacyContext = React.useMemo(

src/TreeSelectContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export interface TreeSelectContextProps {
1414
treeExpandAction?: ExpandAction;
1515
treeTitleRender?: (node: any) => React.ReactNode;
1616
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
17-
maxCount?: number;
1817
displayValues?: LabeledValueType[];
18+
isOverMaxCount?: boolean;
1919
}
2020

2121
const TreeSelectContext = React.createContext<TreeSelectContextProps>(null as any);

0 commit comments

Comments
 (0)