Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,21 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
if (treeExpandedKeys) {
return [...treeExpandedKeys];
}
return searchValue ? searchExpandedKeys : expandedKeys;
}, [expandedKeys, searchExpandedKeys, treeExpandedKeys, searchValue]);
if (searchValue) {
return searchExpandedKeys;
}
if (searchExpandedKeys && loadData && !treeDefaultExpandAll) {
return expandedKeys || [];
}
return expandedKeys;
Comment on lines +139 to +142

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (searchExpandedKeys && loadData && !treeDefaultExpandAll) {
return expandedKeys || [];
}
return expandedKeys;
return expandedKeys || [];

这里和直接这样有啥区别?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里主要差异在 treeDefaultExpandAll 场景。
初始化时如果没有传 treeDefaultExpandedKeys,expandedKeys 是 undefined。直接 return expandedKeys || [] 会返回 [],下游 Tree 收到 expandedKeys={[]} 后会进入受控展开状态,defaultExpandAll 就不会生效,所以默认展开会失效。

}, [
expandedKeys,
searchExpandedKeys,
treeExpandedKeys,
searchValue,
loadData,
treeDefaultExpandAll,
]);

const onInternalExpand = (keys: Key[]) => {
setExpandedKeys(keys);
Expand Down Expand Up @@ -308,10 +321,8 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,

const hasLoadDataFn = useMemo(
() => (searchValue ? false : true),
// eslint-disable-next-line react-hooks/exhaustive-deps
[searchValue, treeExpandedKeys || expandedKeys],
([preSearchValue], [nextSearchValue, nextExcludeSearchExpandedKeys]) =>
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys),
[searchValue],
([preSearchValue], [nextSearchValue]) => preSearchValue !== nextSearchValue,
);

const syncLoadData = hasLoadDataFn ? loadData : null;
Expand Down
31 changes: 31 additions & 0 deletions tests/Select.loadData.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,35 @@ describe('TreeSelect.loadData', () => {
).toHaveLength(2 + i);
}
});

it('keeps load switcher after clearing search value', async () => {
const loadData = jest.fn(() => Promise.resolve());
const { container } = render(
<TreeSelect
open
showSearch
treeDataSimpleMode
loadData={loadData}
treeData={[{ id: 1, pId: 0, value: '1', title: 'Parent' }]}
/>,
);

const input = container.querySelector('input')!;

expect(container.querySelector('.rc-tree-select-tree-switcher_close')).toBeTruthy();

fireEvent.change(input, { target: { value: '1' } });
fireEvent.change(input, { target: { value: '' } });

expect(loadData).not.toHaveBeenCalled();
expect(container.querySelector('.rc-tree-select-tree-switcher_close')).toBeTruthy();
expect(container.querySelector('.rc-tree-select-tree-switcher-noop')).toBeFalsy();

fireEvent.click(container.querySelector('.rc-tree-select-tree-switcher_close')!);
await act(async () => {
await Promise.resolve();
});

expect(loadData).toHaveBeenCalledTimes(1);
});
});