From dc306a20f9fde03ce1b71e7ebd10dd4480ecae80 Mon Sep 17 00:00:00 2001 From: xrkffgg Date: Fri, 14 Aug 2020 17:53:34 +0800 Subject: [PATCH] style: optimize cascader clear animation (#26186) * style: optimize cascader clear animation * fix test * style: optimize cascader clear animation * fix test * add clearTimeout * add test * add clearTimeout --- components/cascader/__tests__/index.test.js | 10 +++++++++- components/cascader/index.tsx | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/components/cascader/__tests__/index.test.js b/components/cascader/__tests__/index.test.js index 607dbdce99..6640b4252d 100644 --- a/components/cascader/__tests__/index.test.js +++ b/components/cascader/__tests__/index.test.js @@ -6,6 +6,7 @@ import ConfigProvider from '../../config-provider'; import focusTest from '../../../tests/shared/focusTest'; import mountTest from '../../../tests/shared/mountTest'; import rtlTest from '../../../tests/shared/rtlTest'; +import { sleep } from '../../../tests/utils'; const options = [ { @@ -186,11 +187,18 @@ describe('Cascader', () => { expect(popupWrapper).toMatchSnapshot(); }); - it('should support to clear selection', () => { + it('should support to clear selection', async () => { const wrapper = mount(); + const willUnmount = jest.spyOn(wrapper.instance(), 'componentWillUnmount'); + const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout'); expect(wrapper.find('.ant-cascader-picker-label').text()).toBe('Zhejiang / Hangzhou'); wrapper.find('.ant-cascader-picker-clear').at(0).simulate('click'); + await sleep(300); expect(wrapper.find('.ant-cascader-picker-label').text()).toBe(''); + wrapper.unmount(); + expect(willUnmount).toHaveBeenCalled(); + expect(clearTimeoutSpy).toHaveBeenCalled(); + clearTimeoutSpy.mockRestore(); }); it('should close popup when clear selection', () => { diff --git a/components/cascader/index.tsx b/components/cascader/index.tsx index 59ea5ae5cb..df19cfefd9 100644 --- a/components/cascader/index.tsx +++ b/components/cascader/index.tsx @@ -109,7 +109,7 @@ export interface CascaderProps { /** use this after antd@3.7.0 */ fieldNames?: FieldNamesType; suffixIcon?: React.ReactNode; - dropdownRender?: (menus: React.ReactNode) => React.ReactNode + dropdownRender?: (menus: React.ReactNode) => React.ReactNode; } export interface CascaderState { @@ -255,6 +255,8 @@ class Cascader extends React.Component { cachedOptions: CascaderOptionType[] = []; + clearSelectionTimeout: any; + private input: Input; constructor(props: CascaderProps) { @@ -269,6 +271,12 @@ class Cascader extends React.Component { }; } + componentWillUnmount() { + if (this.clearSelectionTimeout) { + clearTimeout(this.clearSelectionTimeout); + } + } + setValue = (value: CascaderValueType, selectedOptions: CascaderOptionType[] = []) => { if (!('value' in this.props)) { this.setState({ value }); @@ -354,8 +362,10 @@ class Cascader extends React.Component { e.preventDefault(); e.stopPropagation(); if (!inputValue) { - this.setValue([]); this.handlePopupVisibleChange(false); + this.clearSelectionTimeout = setTimeout(() => { + this.setValue([]); + }, 200); } else { this.setState({ inputValue: '' }); }