Skip to content

Commit 119d525

Browse files
authored
fix: afterClose unexpected call when mount dialog (#244)
close ant-design/ant-design#29009
1 parent cb7c94f commit 119d525

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

src/Dialog/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type IDialogChildProps = {
1515
// TODO: refactor to remove this.
1616
getOpenCount: () => number;
1717
scrollLocker?: ScollLocker;
18-
} & IDialogPropTypes
18+
} & IDialogPropTypes;
1919

2020
export default function Dialog(props: IDialogChildProps) {
2121
const {
@@ -81,7 +81,10 @@ export default function Dialog(props: IDialogChildProps) {
8181
lastOutSideActiveElementRef.current = null;
8282
}
8383

84-
afterClose?.();
84+
// Trigger afterClose only when change visible from true to false
85+
if (animatedVisible) {
86+
afterClose?.();
87+
}
8588
}
8689
}
8790

@@ -91,7 +94,7 @@ export default function Dialog(props: IDialogChildProps) {
9194

9295
// >>> Content
9396
const contentClickRef = useRef(false);
94-
const contentTimeoutRef = useRef<number>();
97+
const contentTimeoutRef = useRef<NodeJS.Timeout>();
9598

9699
// We need record content click incase content popup out of dialog
97100
const onContentMouseDown: React.MouseEventHandler = () => {

tests/index.spec.tsx

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
22
import React, { cloneElement } from 'react';
33
import { act } from 'react-dom/test-utils';
4-
import { mount, ReactWrapper } from 'enzyme';
4+
import type { ReactWrapper } from 'enzyme';
5+
import { mount } from 'enzyme';
56
import Portal from 'rc-util/lib/Portal';
67
import KeyCode from 'rc-util/lib/KeyCode';
7-
import Dialog, { DialogProps } from '../src';
8+
import type { DialogProps } from '../src';
9+
import Dialog from '../src';
810

911
describe('dialog', () => {
1012
beforeEach(() => {
@@ -250,10 +252,7 @@ describe('dialog', () => {
250252
});
251253

252254
expect(
253-
(wrapper
254-
.find('.rc-dialog')
255-
.at(0)
256-
.getDOMNode() as HTMLDivElement).style['transform-origin'],
255+
(wrapper.find('.rc-dialog').at(0).getDOMNode() as HTMLDivElement).style['transform-origin'],
257256
).toBeTruthy();
258257
});
259258

@@ -295,18 +294,6 @@ describe('dialog', () => {
295294
expect(onClose).not.toHaveBeenCalled();
296295
});
297296

298-
it('afterClose', () => {
299-
const afterClose = jest.fn();
300-
301-
const wrapper = mount(<Dialog afterClose={afterClose} visible />);
302-
jest.runAllTimers();
303-
304-
wrapper.setProps({ visible: false });
305-
jest.runAllTimers();
306-
307-
expect(afterClose).toHaveBeenCalledTimes(1);
308-
});
309-
310297
it('zIndex', () => {
311298
const wrapper = mount(<Dialog visible zIndex={903} />);
312299
expect(wrapper.find('.rc-dialog-wrap').props().style.zIndex).toBe(903);
@@ -397,4 +384,42 @@ describe('dialog', () => {
397384
expect(getRenderTimes()).toEqual(2);
398385
});
399386
});
387+
388+
describe('afterClose', () => {
389+
it('should trigger afterClose when set visible to false', () => {
390+
const afterClose = jest.fn();
391+
392+
const wrapper = mount(<Dialog afterClose={afterClose} visible />);
393+
jest.runAllTimers();
394+
395+
wrapper.setProps({ visible: false });
396+
jest.runAllTimers();
397+
398+
expect(afterClose).toHaveBeenCalledTimes(1);
399+
});
400+
401+
it('should not trigger afterClose when mount dialog of getContainer={false}', () => {
402+
const afterClose = jest.fn();
403+
404+
const wrapper = mount(<Dialog afterClose={afterClose} getContainer={false} />);
405+
jest.runAllTimers();
406+
407+
wrapper.setProps({ visible: false });
408+
jest.runAllTimers();
409+
410+
expect(afterClose).toHaveBeenCalledTimes(0);
411+
});
412+
413+
it('should not trigger afterClose when mount dialog of forceRender={true}', () => {
414+
const afterClose = jest.fn();
415+
416+
const wrapper = mount(<Dialog afterClose={afterClose} forceRender />);
417+
jest.runAllTimers();
418+
419+
wrapper.setProps({ visible: false });
420+
jest.runAllTimers();
421+
422+
expect(afterClose).toHaveBeenCalledTimes(0);
423+
});
424+
});
400425
});

0 commit comments

Comments
 (0)