Skip to content

Commit e230c2d

Browse files
committed
fix: cache of loading
1 parent e6822f7 commit e230c2d

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/OptionList.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,15 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
243243

244244
const loadDataFun = useMemo(
245245
() => (searchValue ? null : (loadData as any)),
246-
[searchValue, treeExpandedKeys || expandedKeys],
247-
([preSearchValue], [nextSearchValue, nextExcludeSearchExpandedKeys]) =>
248-
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys),
246+
[loadData, searchValue, treeExpandedKeys || expandedKeys],
247+
(
248+
[prevLoadData, preSearchValue],
249+
[nextLoadData, nextSearchValue, nextExcludeSearchExpandedKeys],
250+
) =>
251+
// `loadData` changed
252+
prevLoadData !== nextLoadData ||
253+
// `searchValue` changed and not in search mode
254+
(preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys)),
249255
);
250256

251257
// ========================== Render ==========================

tests/Select.loadData.spec.tsx

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
/* eslint-disable no-undef, react/no-multi-comp, no-console */
2-
import Tree, { TreeNode } from 'rc-tree';
32
import React from 'react';
43
import { render, fireEvent, act } from '@testing-library/react';
54

6-
import TreeSelect, { SHOW_ALL, SHOW_CHILD, SHOW_PARENT, TreeNode as SelectNode } from '../src';
7-
import { selectNode } from './util';
5+
import TreeSelect from '../src';
86

97
describe('TreeSelect.loadData', () => {
108
it('keep sync', async () => {
11-
let renderTimes = 0;
12-
let calledLastId: number | null = null;
13-
149
const Demo = () => {
1510
const [treeData, setTreeData] = React.useState([
1611
{
@@ -20,17 +15,15 @@ describe('TreeSelect.loadData', () => {
2015
},
2116
]);
2217

23-
renderTimes += 1;
24-
const renderId = renderTimes;
25-
2618
const loadData = async () => {
27-
calledLastId = renderId;
19+
const nextId = treeData.length;
20+
2821
setTreeData([
2922
...treeData,
3023
{
31-
title: `${renderId}`,
32-
value: renderId,
33-
isLeaf: true,
24+
title: `${nextId}`,
25+
value: nextId,
26+
isLeaf: false,
3427
},
3528
]);
3629
};
@@ -40,18 +33,14 @@ describe('TreeSelect.loadData', () => {
4033

4134
render(<Demo />);
4235

43-
console.log(document.body.innerHTML);
44-
45-
fireEvent.click(document.querySelector('.rc-tree-select-tree-switcher_close'));
46-
await act(async () => {
47-
await Promise.resolve();
48-
});
49-
expect(calledLastId).toBe(renderTimes);
50-
51-
fireEvent.click(document.querySelector('.rc-tree-select-tree-switcher_close'));
52-
await act(async () => {
53-
await Promise.resolve();
54-
});
55-
expect(calledLastId).toBe(renderTimes);
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+
}
5645
});
5746
});

0 commit comments

Comments
 (0)