diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 05462245..ae12c47c 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -133,8 +133,21 @@ const OptionList: React.ForwardRefRenderFunction = (_, if (treeExpandedKeys) { return [...treeExpandedKeys]; } - return searchValue ? searchExpandedKeys : expandedKeys; - }, [expandedKeys, searchExpandedKeys, treeExpandedKeys, searchValue]); + if (searchValue) { + return searchExpandedKeys; + } + if (searchExpandedKeys && loadData && !treeDefaultExpandAll) { + return expandedKeys || []; + } + return expandedKeys; + }, [ + expandedKeys, + searchExpandedKeys, + treeExpandedKeys, + searchValue, + loadData, + treeDefaultExpandAll, + ]); const onInternalExpand = (keys: Key[]) => { setExpandedKeys(keys); @@ -308,10 +321,8 @@ const OptionList: React.ForwardRefRenderFunction = (_, 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; diff --git a/tests/Select.loadData.spec.tsx b/tests/Select.loadData.spec.tsx index c938d95c..6ed20007 100644 --- a/tests/Select.loadData.spec.tsx +++ b/tests/Select.loadData.spec.tsx @@ -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( + , + ); + + 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); + }); });