Skip to content

Commit 08da1bf

Browse files
MuxinFengafc163
andauthored
feat: add afterOpenChange prop (#349)
* feat: add onOpenChanged prop * Update src/Dialog/index.tsx Co-authored-by: afc163 <afc163@gmail.com> * fix: fix onOpenChanged to onOpenChange * fix: replace onOpenChange with afterOpenChange --------- Co-authored-by: afc163 <afc163@gmail.com>
1 parent b666e19 commit 08da1bf

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/Dialog/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import * as React from 'react';
2-
import { useRef, useEffect } from 'react';
31
import classNames from 'classnames';
4-
import KeyCode from 'rc-util/lib/KeyCode';
5-
import useId from 'rc-util/lib/hooks/useId';
62
import contains from 'rc-util/lib/Dom/contains';
3+
import useId from 'rc-util/lib/hooks/useId';
4+
import KeyCode from 'rc-util/lib/KeyCode';
75
import pickAttrs from 'rc-util/lib/pickAttrs';
6+
import * as React from 'react';
7+
import { useEffect, useRef } from 'react';
88
import type { IDialogPropTypes } from '../IDialogPropTypes';
9-
import Mask from './Mask';
109
import { getMotionName } from '../util';
1110
import Content from './Content';
1211
import type { ContentRef } from './Content/Panel';
12+
import Mask from './Mask';
1313

1414
export default function Dialog(props: IDialogPropTypes) {
1515
const {
@@ -25,6 +25,7 @@ export default function Dialog(props: IDialogPropTypes) {
2525
wrapClassName,
2626
wrapProps,
2727
onClose,
28+
afterOpenChange,
2829
afterClose,
2930

3031
// Dialog
@@ -86,6 +87,7 @@ export default function Dialog(props: IDialogPropTypes) {
8687
afterClose?.();
8788
}
8889
}
90+
afterOpenChange?.(newVisible);
8991
}
9092

9193
function onInternalClose(e: React.SyntheticEvent) {

src/IDialogPropTypes.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ReactNode, CSSProperties, SyntheticEvent } from 'react';
21
import type { GetContainer } from 'rc-util/lib/PortalWrapper';
2+
import type { CSSProperties, ReactNode, SyntheticEvent } from 'react';
33

44
export type IDialogPropTypes = {
55
className?: string;
@@ -8,6 +8,7 @@ export type IDialogPropTypes = {
88
mask?: boolean;
99
children?: any;
1010
afterClose?: () => any;
11+
afterOpenChange?: (open: boolean) => void;
1112
onClose?: (e: SyntheticEvent) => any;
1213
closable?: boolean;
1314
maskClosable?: boolean;

tests/index.spec.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,18 @@ describe('dialog', () => {
504504
expect(afterClose).toHaveBeenCalledTimes(0);
505505
});
506506
});
507+
508+
describe('afterOpenChange', () => {
509+
it('should trigger afterOpenChange when visible changed', () => {
510+
const afterOpenChange = jest.fn();
511+
512+
const wrapper = mount(<Dialog afterOpenChange={afterOpenChange} visible />);
513+
jest.runAllTimers();
514+
515+
wrapper.setProps({ visible: false });
516+
jest.runAllTimers();
517+
518+
expect(afterOpenChange).toHaveBeenCalledTimes(2);
519+
});
520+
});
507521
});

0 commit comments

Comments
 (0)