From c1785399381c3bdf5e13929ebb6156b7378d9c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B1=AA?= <1844749591@qq.com> Date: Thu, 14 Nov 2024 11:49:33 +0800 Subject: [PATCH] chore: hoist state to context --- src/OptionList.tsx | 9 ++------- src/TreeSelect.tsx | 43 +++++++++++++++++++++------------------- src/TreeSelectContext.ts | 2 +- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 01aada45..c2aa3af0 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -45,8 +45,8 @@ const OptionList: React.ForwardRefRenderFunction = (_, treeExpandAction, treeTitleRender, onPopupScroll, - maxCount, displayValues, + isOverMaxCount, } = React.useContext(TreeSelectContext); const { @@ -250,11 +250,6 @@ const OptionList: React.ForwardRefRenderFunction = (_, preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys), ); - const isOverMaxCount = React.useMemo( - () => multiple && maxCount !== undefined && displayValues?.length >= maxCount, - [multiple, maxCount, displayValues?.length], - ); - const onActiveChange = (key: Key) => { if (isOverMaxCount && !displayValues?.some(v => v.value === key)) { return; @@ -275,7 +270,7 @@ const OptionList: React.ForwardRefRenderFunction = (_, const processedTreeData = React.useMemo( () => traverse(memoTreeData), - [memoTreeData, isOverMaxCount, displayValues, fieldNames], + [memoTreeData, isOverMaxCount], ); // ========================== Render ========================== diff --git a/src/TreeSelect.tsx b/src/TreeSelect.tsx index 6a2ab514..a356d016 100644 --- a/src/TreeSelect.tsx +++ b/src/TreeSelect.tsx @@ -613,8 +613,11 @@ const TreeSelect = React.forwardRef((props, ref) }); // ========================== Context =========================== - const treeSelectContext = React.useMemo( - () => ({ + const treeSelectContext = React.useMemo(() => { + const isOverMaxCount = + mergedMultiple && maxCount !== undefined && cachedDisplayValues?.length >= maxCount; + + return { virtual, dropdownMatchSelectWidth, listHeight, @@ -626,25 +629,25 @@ const TreeSelect = React.forwardRef((props, ref) treeExpandAction, treeTitleRender, onPopupScroll, - maxCount, displayValues: cachedDisplayValues, - }), - [ - virtual, - dropdownMatchSelectWidth, - listHeight, - listItemHeight, - listItemScrollOffset, - filteredTreeData, - mergedFieldNames, - onOptionSelect, - treeExpandAction, - treeTitleRender, - onPopupScroll, - maxCount, - cachedDisplayValues, - ], - ); + isOverMaxCount, + }; + }, [ + virtual, + dropdownMatchSelectWidth, + listHeight, + listItemHeight, + listItemScrollOffset, + filteredTreeData, + mergedFieldNames, + onOptionSelect, + treeExpandAction, + treeTitleRender, + onPopupScroll, + maxCount, + cachedDisplayValues, + mergedMultiple, + ]); // ======================= Legacy Context ======================= const legacyContext = React.useMemo( diff --git a/src/TreeSelectContext.ts b/src/TreeSelectContext.ts index 8135b086..2d335e4b 100644 --- a/src/TreeSelectContext.ts +++ b/src/TreeSelectContext.ts @@ -14,8 +14,8 @@ export interface TreeSelectContextProps { treeExpandAction?: ExpandAction; treeTitleRender?: (node: any) => React.ReactNode; onPopupScroll?: React.UIEventHandler; - maxCount?: number; displayValues?: LabeledValueType[]; + isOverMaxCount?: boolean; } const TreeSelectContext = React.createContext(null as any);