Skip to content

Commit bb13fc8

Browse files
author
DuQi
committed
fix: restore load switcher after clearing search
1 parent f1d2034 commit bb13fc8

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/OptionList.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,21 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
138138
if (treeExpandedKeys) {
139139
return [...treeExpandedKeys];
140140
}
141-
return searchValue ? searchExpandedKeys : expandedKeys;
142-
}, [expandedKeys, searchExpandedKeys, treeExpandedKeys, searchValue]);
141+
if (searchValue) {
142+
return searchExpandedKeys;
143+
}
144+
if (searchExpandedKeys && loadData && !treeDefaultExpandAll) {
145+
return expandedKeys || [];
146+
}
147+
return expandedKeys;
148+
}, [
149+
expandedKeys,
150+
searchExpandedKeys,
151+
treeExpandedKeys,
152+
searchValue,
153+
loadData,
154+
treeDefaultExpandAll,
155+
]);
143156

144157
const onInternalExpand = (keys: Key[]) => {
145158
setExpandedKeys(keys);
@@ -315,8 +328,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
315328
() => (searchValue ? false : true),
316329
// eslint-disable-next-line react-hooks/exhaustive-deps
317330
[searchValue, treeExpandedKeys || expandedKeys],
318-
([preSearchValue], [nextSearchValue, nextExcludeSearchExpandedKeys]) =>
319-
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys),
331+
([preSearchValue], [nextSearchValue]) => preSearchValue !== nextSearchValue,
320332
);
321333

322334
const syncLoadData = hasLoadDataFn ? loadData : null;

tests/Select.loadData.spec.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,28 @@ describe('TreeSelect.loadData', () => {
4343
).toHaveLength(2 + i);
4444
}
4545
});
46+
47+
it('keeps load switcher after clearing search value', () => {
48+
const loadData = jest.fn(() => Promise.resolve());
49+
const { container } = render(
50+
<TreeSelect
51+
open
52+
showSearch
53+
treeDataSimpleMode
54+
loadData={loadData}
55+
treeData={[{ id: 1, pId: 0, value: '1', title: 'Parent' }]}
56+
/>,
57+
);
58+
59+
const input = container.querySelector('input')!;
60+
61+
expect(container.querySelector('.rc-tree-select-tree-switcher_close')).toBeTruthy();
62+
63+
fireEvent.change(input, { target: { value: '1' } });
64+
fireEvent.change(input, { target: { value: '' } });
65+
66+
expect(loadData).not.toHaveBeenCalled();
67+
expect(container.querySelector('.rc-tree-select-tree-switcher_close')).toBeTruthy();
68+
expect(container.querySelector('.rc-tree-select-tree-switcher-noop')).toBeFalsy();
69+
});
4670
});

0 commit comments

Comments
 (0)