Skip to content

Commit

Permalink
chore: hoist state to context
Browse files Browse the repository at this point in the history
  • Loading branch information
aojunhao123 committed Nov 14, 2024
1 parent c72c25f commit c178539
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
9 changes: 2 additions & 7 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
treeExpandAction,
treeTitleRender,
onPopupScroll,
maxCount,
displayValues,
isOverMaxCount,
} = React.useContext(TreeSelectContext);

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

const isOverMaxCount = React.useMemo<boolean>(
() => multiple && maxCount !== undefined && displayValues?.length >= maxCount,
[multiple, maxCount, displayValues?.length],
);

const onActiveChange = (key: Key) => {
if (isOverMaxCount && !displayValues?.some(v => v.value === key)) {
return;
Expand All @@ -275,7 +270,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,

const processedTreeData = React.useMemo(
() => traverse(memoTreeData),
[memoTreeData, isOverMaxCount, displayValues, fieldNames],
[memoTreeData, isOverMaxCount],
);

// ========================== Render ==========================
Expand Down
43 changes: 23 additions & 20 deletions src/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,11 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
});

// ========================== Context ===========================
const treeSelectContext = React.useMemo<TreeSelectContextProps>(
() => ({
const treeSelectContext = React.useMemo<TreeSelectContextProps>(() => {
const isOverMaxCount =
mergedMultiple && maxCount !== undefined && cachedDisplayValues?.length >= maxCount;

return {
virtual,
dropdownMatchSelectWidth,
listHeight,
Expand All @@ -626,25 +629,25 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((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(
Expand Down
2 changes: 1 addition & 1 deletion src/TreeSelectContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface TreeSelectContextProps {
treeExpandAction?: ExpandAction;
treeTitleRender?: (node: any) => React.ReactNode;
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
maxCount?: number;
displayValues?: LabeledValueType[];
isOverMaxCount?: boolean;
}

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

0 comments on commit c178539

Please sign in to comment.