diff --git a/components/tag/__tests__/__snapshots__/index.test.js.snap b/components/tag/__tests__/__snapshots__/index.test.js.snap index d81d326407..84c169db4b 100644 --- a/components/tag/__tests__/__snapshots__/index.test.js.snap +++ b/components/tag/__tests__/__snapshots__/index.test.js.snap @@ -1,17 +1,37 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Tag can be controlled by visible 1`] = ` +exports[`Tag visibility can be controlled by visible with hidden as initial value 1`] = `
`; + +exports[`Tag visibility can be controlled by visible with hidden as initial value 2`] = ` +
+`; + +exports[`Tag visibility can be controlled by visible with hidden as initial value 3`] = ` +
+`; + +exports[`Tag visibility can be controlled by visible with visible as initial value 1`] = `
`; -exports[`Tag can be controlled by visible 2`] = `null`; +exports[`Tag visibility can be controlled by visible with visible as initial value 2`] = ` +
+`; -exports[`Tag can be controlled by visible 3`] = ` +exports[`Tag visibility can be controlled by visible with visible as initial value 3`] = `
`; diff --git a/components/tag/__tests__/index.test.js b/components/tag/__tests__/index.test.js index 7626f497cf..46dd7b310b 100644 --- a/components/tag/__tests__/index.test.js +++ b/components/tag/__tests__/index.test.js @@ -39,16 +39,31 @@ describe('Tag', () => { expect(wrapper.find('.ant-tag').length).toBe(1); }); - it('can be controlled by visible', () => { - const wrapper = mount( - - ); - expect(wrapper.render()).toMatchSnapshot(); - wrapper.setProps({ visible: false }); - jest.runAllTimers(); - expect(wrapper.render()).toMatchSnapshot(); - wrapper.setProps({ visible: true }); - jest.runAllTimers(); - expect(wrapper.render()).toMatchSnapshot(); + describe('visibility', () => { + it('can be controlled by visible with visible as initial value', () => { + const wrapper = mount( + + ); + expect(wrapper.render()).toMatchSnapshot(); + wrapper.setProps({ visible: false }); + jest.runAllTimers(); + expect(wrapper.render()).toMatchSnapshot(); + wrapper.setProps({ visible: true }); + jest.runAllTimers(); + expect(wrapper.render()).toMatchSnapshot(); + }); + + it('can be controlled by visible with hidden as initial value', () => { + const wrapper = mount( + + ); + expect(wrapper.render()).toMatchSnapshot(); + wrapper.setProps({ visible: true }); + jest.runAllTimers(); + expect(wrapper.render()).toMatchSnapshot(); + wrapper.setProps({ visible: false }); + jest.runAllTimers(); + expect(wrapper.render()).toMatchSnapshot(); + }); }); }); diff --git a/components/tag/index.tsx b/components/tag/index.tsx index 6cd8b6fa6b..167605f33e 100644 --- a/components/tag/index.tsx +++ b/components/tag/index.tsx @@ -28,6 +28,7 @@ export interface TagState { closing: boolean; closed: boolean; visible: boolean; + mounted: boolean; } class Tag extends React.Component { @@ -37,14 +38,30 @@ class Tag extends React.Component { closable: false, }; - static getDerivedStateFromProps(nextProps: TagProps) { - return ('visible' in nextProps) ? { visible: nextProps.visible } : null; + static getDerivedStateFromProps(nextProps: TagProps, state: TagState) { + if ('visible' in nextProps) { + let newState: Partial = { + visible: nextProps.visible, + mounted: true, + }; + + if (!state.mounted) { + newState = { + ...newState, + closed: !nextProps.visible, + }; + } + + return newState; + } + return null; } state = { closing: false, closed: false, visible: true, + mounted: false, }; componentDidUpdate(_prevProps: TagProps, prevState: TagState) { @@ -130,7 +147,7 @@ class Tag extends React.Component { backgroundColor: (color && !isPresetColor) ? color : null, ...style, }; - const tag = this.state.closed ? null : ( + const tag = this.state.closed ?
: (
{ {closeIcon}
); + return (