Skip to content

Commit 93afc43

Browse files
bbb169afc163jingzouzou
authored
fix: always change treeProps.expandedKeys to avoid wrong loadling data (#576)
Co-authored-by: afc163 <afc163@gmail.com> Co-authored-by: jingzouzou <jingzouzou@didiglobal.com>
1 parent fcd598c commit 93afc43

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

src/OptionList.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
196196
onKeyUp: () => {},
197197
}));
198198

199+
const loadDataFun = useMemo(
200+
() => searchValue ? null : (loadData as any),
201+
[searchValue, treeExpandedKeys || expandedKeys],
202+
([preSearchValue], [nextSearchValue, nextExcludeSearchExpandedKeys]) =>
203+
preSearchValue !== nextSearchValue && !!(nextSearchValue || nextExcludeSearchExpandedKeys)
204+
);
205+
199206
// ========================== Render ==========================
200207
if (memoTreeData.length === 0) {
201208
return (
@@ -237,7 +244,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
237244
showIcon={showTreeIcon}
238245
switcherIcon={switcherIcon}
239246
showLine={treeLine}
240-
loadData={searchValue ? null : (loadData as any)}
247+
loadData={loadDataFun}
241248
motion={treeMotion}
242249
activeKey={activeKey}
243250
// We handle keys by out instead tree self

tests/Select.SearchInput.spec.js

+63-31
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ describe('TreeSelect.SearchInput', () => {
1919

2020
wrapper.selectNode();
2121
expect(onSearch).not.toHaveBeenCalled();
22-
expect(
23-
wrapper
24-
.find('input')
25-
.first()
26-
.props().value,
27-
).toBeFalsy();
22+
expect(wrapper.find('input').first().props().value).toBeFalsy();
2823
});
2924

3025
it('expandedKeys', () => {
@@ -51,10 +46,7 @@ describe('TreeSelect.SearchInput', () => {
5146
expect(wrapper.find('NodeList').prop('expandedKeys')).toEqual(['bamboo', 'light']);
5247

5348
function search(value) {
54-
wrapper
55-
.find('input')
56-
.first()
57-
.simulate('change', { target: { value } });
49+
wrapper.find('input').first().simulate('change', { target: { value } });
5850
wrapper.update();
5951
}
6052

@@ -85,8 +77,8 @@ describe('TreeSelect.SearchInput', () => {
8577
{ id: 1, pId: 0, value: '1', title: 'Expand to load' },
8678
{ id: 2, pId: 0, value: '2', title: 'Expand to load' },
8779
{ id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
88-
])
89-
}
80+
]);
81+
};
9082

9183
const genTreeNode = (parentId, isLeaf = false) => {
9284
const random = Math.random().toString(36).substring(2, 6);
@@ -100,22 +92,16 @@ describe('TreeSelect.SearchInput', () => {
10092
};
10193

10294
const onLoadData = ({ id, ...rest }) =>
103-
new Promise((resolve) => {
104-
setTimeout(() => {
105-
called += 1;
106-
handleLoadData({ id, ...rest });
107-
setTreeData(
108-
treeData.concat([
109-
genTreeNode(id, false),
110-
genTreeNode(id, true),
111-
genTreeNode(id, true),
112-
])
113-
);
114-
resolve(undefined);
115-
}, 300);
95+
new Promise(resolve => {
96+
called += 1;
97+
handleLoadData({ id, ...rest });
98+
setTreeData(
99+
treeData.concat([genTreeNode(id, false), genTreeNode(id, true), genTreeNode(id, true)]),
100+
);
101+
resolve(undefined);
116102
});
117103

118-
const onChange = (newValue) => {
104+
const onChange = newValue => {
119105
setValue(newValue);
120106
};
121107

@@ -130,7 +116,6 @@ describe('TreeSelect.SearchInput', () => {
130116
treeData={treeData}
131117
treeNodeFilterProp="title"
132118
showSearch
133-
filterTreeNode={false}
134119
/>
135120
<button onClick={addDefaultTreeData}>设置数据</button>
136121
</>
@@ -141,10 +126,7 @@ describe('TreeSelect.SearchInput', () => {
141126
expect(handleLoadData).not.toHaveBeenCalled();
142127

143128
function search(value) {
144-
wrapper
145-
.find('input')
146-
.first()
147-
.simulate('change', { target: { value } });
129+
wrapper.find('input').first().simulate('change', { target: { value } });
148130
wrapper.update();
149131
}
150132
search('Tree Node');
@@ -165,5 +147,55 @@ describe('TreeSelect.SearchInput', () => {
165147
search('');
166148
expect(handleLoadData).not.toHaveBeenCalled();
167149
expect(called).toBe(0);
150+
151+
search('ex');
152+
const nodes = wrapper.find(`[title="${'Expand to load'}"]`).hostNodes();
153+
nodes.first().simulate('click');
154+
expect(called).toBe(0); // should not trrigger all nodes to load data
155+
});
156+
157+
it('should trrigger `loadData` when click node', () => {
158+
let called = 0;
159+
const Demo = () => {
160+
const [value, setValue] = useState();
161+
const onLoadData = ({ id, ...rest }) =>
162+
new Promise(resolve => {
163+
called += 1;
164+
resolve(undefined);
165+
});
166+
167+
const onChange = newValue => {
168+
setValue(newValue);
169+
};
170+
171+
return (
172+
<TreeSelect
173+
treeDataSimpleMode
174+
value={value}
175+
placeholder="Please select"
176+
onChange={onChange}
177+
loadData={onLoadData}
178+
treeData={[
179+
{ id: 1, pId: 0, value: '1', title: 'Expand to load' },
180+
{ id: 2, pId: 0, value: '2', title: 'Expand to load' },
181+
{ id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
182+
]}
183+
treeNodeFilterProp="title"
184+
treeExpandAction="click"
185+
showSearch
186+
/>
187+
);
188+
};
189+
const wrapper = mount(<Demo />);
190+
191+
function search(value) {
192+
wrapper.find('input').first().simulate('change', { target: { value } });
193+
wrapper.update();
194+
}
195+
196+
search('ex');
197+
const nodes = wrapper.find(`[title="${'Expand to load'}"]`).hostNodes();
198+
nodes.first().simulate('click');
199+
expect(called).toBe(1);
168200
});
169201
});

0 commit comments

Comments
 (0)