Skip to content

Commit 0e85779

Browse files
authored
fix: Dynamic change isLeaf from false to true should stop loading (#213)
* test: test driven * fix: loading data logic
1 parent 00c05ab commit 0e85779

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/OptionList/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const RefOptionList = React.forwardRef<RefOptionListProps, OptionListProps>((pro
6464
if (loadingKeys.length) {
6565
loadingKeys.forEach(loadingKey => {
6666
const option = flattenOptions.find(opt => opt.value === loadingKey);
67-
if (option.data.children) {
67+
if (option.data.children || option.data.isLeaf === true) {
6868
setLoadingKeys(keys => keys.filter(key => key !== loadingKey));
6969
}
7070
});

tests/index.spec.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable react/jsx-no-bind */
22

33
import React from 'react';
4+
import { act } from 'react-dom/test-utils';
45
import { resetWarned } from 'rc-util/lib/warning';
56
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
67
import { mount } from './enzyme';
@@ -603,6 +604,41 @@ describe('Cascader.Basic', () => {
603604
jest.runAllTimers();
604605
expect(loadData).toHaveBeenCalled();
605606
});
607+
608+
it('change isLeaf back to true should not loop loading', async () => {
609+
const Demo = () => {
610+
const [options, setOptions] = React.useState([
611+
{ value: 'zhejiang', label: 'Zhejiang', isLeaf: false },
612+
]);
613+
614+
const loadData = () => {
615+
Promise.resolve().then(() => {
616+
act(() => {
617+
setOptions([
618+
{
619+
value: 'zhejiang',
620+
label: 'Zhejiang',
621+
isLeaf: true,
622+
},
623+
]);
624+
});
625+
});
626+
};
627+
628+
return <Cascader options={options} loadData={loadData} open />;
629+
};
630+
631+
const wrapper = mount(<Demo />);
632+
wrapper.find('.rc-cascader-menu-item-content').first().simulate('click');
633+
expect(wrapper.exists('.rc-cascader-menu-item-loading')).toBeTruthy();
634+
635+
for (let i = 0; i < 3; i += 1) {
636+
await Promise.resolve();
637+
}
638+
wrapper.update();
639+
640+
expect(wrapper.exists('.rc-cascader-menu-item-loading')).toBeFalsy();
641+
});
606642
});
607643

608644
// https://github.com/ant-design/ant-design/issues/9793

0 commit comments

Comments
 (0)