Skip to content

Commit b1e1947

Browse files
author
DuQi
committed
fix: restore load switcher after clearing search
1 parent 34b4676 commit b1e1947

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
@@ -133,8 +133,21 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
133133
if (treeExpandedKeys) {
134134
return [...treeExpandedKeys];
135135
}
136-
return searchValue ? searchExpandedKeys : expandedKeys;
137-
}, [expandedKeys, searchExpandedKeys, treeExpandedKeys, searchValue]);
136+
if (searchValue) {
137+
return searchExpandedKeys;
138+
}
139+
if (searchExpandedKeys && loadData && !treeDefaultExpandAll) {
140+
return expandedKeys || [];
141+
}
142+
return expandedKeys;
143+
}, [
144+
expandedKeys,
145+
searchExpandedKeys,
146+
treeExpandedKeys,
147+
searchValue,
148+
loadData,
149+
treeDefaultExpandAll,
150+
]);
138151

139152
const onInternalExpand = (keys: Key[]) => {
140153
setExpandedKeys(keys);
@@ -310,8 +323,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
310323
() => (searchValue ? false : true),
311324
// eslint-disable-next-line react-hooks/exhaustive-deps
312325
[searchValue, treeExpandedKeys || expandedKeys],
313-
([preSearchValue], [nextSearchValue, nextExcludeSearchExpandedKeys]) =>
314-
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys),
326+
([preSearchValue], [nextSearchValue]) => preSearchValue !== nextSearchValue,
315327
);
316328

317329
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)