diff --git a/src/OptionList/index.tsx b/src/OptionList/index.tsx index 6a9cc8ad..11df8ba6 100644 --- a/src/OptionList/index.tsx +++ b/src/OptionList/index.tsx @@ -64,7 +64,7 @@ const RefOptionList = React.forwardRef((pro if (loadingKeys.length) { loadingKeys.forEach(loadingKey => { const option = flattenOptions.find(opt => opt.value === loadingKey); - if (option.data.children) { + if (option.data.children || option.data.isLeaf === true) { setLoadingKeys(keys => keys.filter(key => key !== loadingKey)); } }); diff --git a/tests/index.spec.tsx b/tests/index.spec.tsx index 65b59acf..54b2a66b 100644 --- a/tests/index.spec.tsx +++ b/tests/index.spec.tsx @@ -1,6 +1,7 @@ /* eslint-disable react/jsx-no-bind */ import React from 'react'; +import { act } from 'react-dom/test-utils'; import { resetWarned } from 'rc-util/lib/warning'; import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; import { mount } from './enzyme'; @@ -603,6 +604,41 @@ describe('Cascader.Basic', () => { jest.runAllTimers(); expect(loadData).toHaveBeenCalled(); }); + + it('change isLeaf back to true should not loop loading', async () => { + const Demo = () => { + const [options, setOptions] = React.useState([ + { value: 'zhejiang', label: 'Zhejiang', isLeaf: false }, + ]); + + const loadData = () => { + Promise.resolve().then(() => { + act(() => { + setOptions([ + { + value: 'zhejiang', + label: 'Zhejiang', + isLeaf: true, + }, + ]); + }); + }); + }; + + return ; + }; + + const wrapper = mount(); + wrapper.find('.rc-cascader-menu-item-content').first().simulate('click'); + expect(wrapper.exists('.rc-cascader-menu-item-loading')).toBeTruthy(); + + for (let i = 0; i < 3; i += 1) { + await Promise.resolve(); + } + wrapper.update(); + + expect(wrapper.exists('.rc-cascader-menu-item-loading')).toBeFalsy(); + }); }); // https://github.com/ant-design/ant-design/issues/9793