Skip to content

Commit

Permalink
chore: hoist traverse operation to TreeSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
aojunhao123 committed Nov 14, 2024
1 parent c178539 commit e9b59f9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
8 changes: 7 additions & 1 deletion examples/mutiple-with-maxCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ export default () => {
return (
<>
<h2>multiple with maxCount</h2>
<TreeSelect style={{ width: 300 }} multiple maxCount={3} treeData={treeData} />
<TreeSelect
style={{ width: 300 }}
fieldNames={{ value: 'value', label: 'title' }}
multiple
maxCount={3}
treeData={treeData}
/>

<h2>checkable with maxCount</h2>
<TreeSelect
Expand Down
20 changes: 2 additions & 18 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
setActiveKey(key);
};

const traverse = (nodes: EventDataNode<any>[]): EventDataNode<any>[] => {
return nodes.map(node => ({
...node,
disabled:
isOverMaxCount && !displayValues?.some(v => v.value === node[fieldNames.value])
? true
: node.disabled,
children: node.children ? traverse(node.children) : undefined,
}));
};

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

// ========================== Render ==========================
if (processedTreeData.length === 0) {
if (memoTreeData.length === 0) {
return (
<div role="listbox" className={`${prefixCls}-empty`} onMouseDown={onListMouseDown}>
{notFoundContent}
Expand Down Expand Up @@ -304,7 +288,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
ref={treeRef}
focusable={false}
prefixCls={`${prefixCls}-tree`}
treeData={processedTreeData}
treeData={memoTreeData}
height={listHeight}
itemHeight={listItemHeight}
itemScrollOffset={listItemScrollOffset}
Expand Down
26 changes: 21 additions & 5 deletions src/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -613,17 +613,33 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
});

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

const traverse = (nodes: DataNode[]): DataNode[] => {
return nodes.map(node => ({
...node,
disabled:
isOverMaxCount && !cachedDisplayValues?.some(v => v.value === node.value)
? true
: node.disabled,
children: node.children ? traverse(node.children) : undefined,
}));
};

const processedTreeData = React.useMemo(
() => traverse(filteredTreeData),
[filteredTreeData, isOverMaxCount],
);

const treeSelectContext = React.useMemo<TreeSelectContextProps>(() => {
return {
virtual,
dropdownMatchSelectWidth,
listHeight,
listItemHeight,
listItemScrollOffset,
treeData: filteredTreeData,
treeData: processedTreeData,
fieldNames: mergedFieldNames,
onSelect: onOptionSelect,
treeExpandAction,
Expand All @@ -638,7 +654,7 @@ const TreeSelect = React.forwardRef<BaseSelectRef, TreeSelectProps>((props, ref)
listHeight,
listItemHeight,
listItemScrollOffset,
filteredTreeData,
processedTreeData,
mergedFieldNames,
onOptionSelect,
treeExpandAction,
Expand Down

0 comments on commit e9b59f9

Please sign in to comment.