Skip to content

Commit 35d98f5

Browse files
authored
fix: loadData not keep fresh (#599)
* test: test driven * fix: cache of loading * fix: off topic * fix: sync load logic * chore: fix lint
1 parent 48076ee commit 35d98f5

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/OptionList.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,15 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
241241
onKeyUp: () => {},
242242
}));
243243

244-
const loadDataFun = useMemo(
245-
() => (searchValue ? null : (loadData as any)),
244+
const hasLoadDataFn = useMemo(
245+
() => (searchValue ? false : true),
246246
[searchValue, treeExpandedKeys || expandedKeys],
247247
([preSearchValue], [nextSearchValue, nextExcludeSearchExpandedKeys]) =>
248248
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys),
249249
);
250250

251+
const syncLoadData = hasLoadDataFn ? loadData : null;
252+
251253
// ========================== Render ==========================
252254
if (memoTreeData.length === 0) {
253255
return (
@@ -289,7 +291,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
289291
showIcon={showTreeIcon}
290292
switcherIcon={switcherIcon}
291293
showLine={treeLine}
292-
loadData={loadDataFun}
294+
loadData={syncLoadData}
293295
motion={treeMotion}
294296
activeKey={activeKey}
295297
// We handle keys by out instead tree self

tests/Select.loadData.spec.tsx

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* eslint-disable no-undef, react/no-multi-comp, no-console */
2+
import React from 'react';
3+
import { render, fireEvent, act } from '@testing-library/react';
4+
5+
import TreeSelect from '../src';
6+
7+
describe('TreeSelect.loadData', () => {
8+
it('keep sync', async () => {
9+
const Demo = () => {
10+
const [treeData, setTreeData] = React.useState([
11+
{
12+
title: '0',
13+
value: 0,
14+
isLeaf: false,
15+
},
16+
]);
17+
18+
const loadData = async () => {
19+
const nextId = treeData.length;
20+
21+
setTreeData([
22+
...treeData,
23+
{
24+
title: `${nextId}`,
25+
value: nextId,
26+
isLeaf: false,
27+
},
28+
]);
29+
};
30+
31+
return <TreeSelect open treeData={treeData} loadData={loadData} />;
32+
};
33+
34+
render(<Demo />);
35+
36+
for (let i = 0; i < 5; i += 1) {
37+
fireEvent.click(document.querySelector('.rc-tree-select-tree-switcher_close'));
38+
await act(async () => {
39+
await Promise.resolve();
40+
});
41+
expect(
42+
document.querySelectorAll('.rc-tree-select-tree-list .rc-tree-select-tree-treenode'),
43+
).toHaveLength(2 + i);
44+
}
45+
});
46+
});

0 commit comments

Comments
 (0)