Skip to content

Commit

Permalink
feat: add afterOpenChange prop (#349)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
MuxinFeng and afc163 committed Mar 1, 2023
1 parent b666e19 commit 08da1bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/Dialog/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as React from 'react';
import { useRef, useEffect } from 'react';
import classNames from 'classnames';
import KeyCode from 'rc-util/lib/KeyCode';
import useId from 'rc-util/lib/hooks/useId';
import contains from 'rc-util/lib/Dom/contains';
import useId from 'rc-util/lib/hooks/useId';
import KeyCode from 'rc-util/lib/KeyCode';
import pickAttrs from 'rc-util/lib/pickAttrs';
import * as React from 'react';
import { useEffect, useRef } from 'react';
import type { IDialogPropTypes } from '../IDialogPropTypes';
import Mask from './Mask';
import { getMotionName } from '../util';
import Content from './Content';
import type { ContentRef } from './Content/Panel';
import Mask from './Mask';

export default function Dialog(props: IDialogPropTypes) {
const {
Expand All @@ -25,6 +25,7 @@ export default function Dialog(props: IDialogPropTypes) {
wrapClassName,
wrapProps,
onClose,
afterOpenChange,
afterClose,

// Dialog
Expand Down Expand Up @@ -86,6 +87,7 @@ export default function Dialog(props: IDialogPropTypes) {
afterClose?.();
}
}
afterOpenChange?.(newVisible);
}

function onInternalClose(e: React.SyntheticEvent) {
Expand Down
3 changes: 2 additions & 1 deletion src/IDialogPropTypes.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactNode, CSSProperties, SyntheticEvent } from 'react';
import type { GetContainer } from 'rc-util/lib/PortalWrapper';
import type { CSSProperties, ReactNode, SyntheticEvent } from 'react';

export type IDialogPropTypes = {
className?: string;
Expand All @@ -8,6 +8,7 @@ export type IDialogPropTypes = {
mask?: boolean;
children?: any;
afterClose?: () => any;
afterOpenChange?: (open: boolean) => void;
onClose?: (e: SyntheticEvent) => any;
closable?: boolean;
maskClosable?: boolean;
Expand Down
14 changes: 14 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,18 @@ describe('dialog', () => {
expect(afterClose).toHaveBeenCalledTimes(0);
});
});

describe('afterOpenChange', () => {
it('should trigger afterOpenChange when visible changed', () => {
const afterOpenChange = jest.fn();

const wrapper = mount(<Dialog afterOpenChange={afterOpenChange} visible />);
jest.runAllTimers();

wrapper.setProps({ visible: false });
jest.runAllTimers();

expect(afterOpenChange).toHaveBeenCalledTimes(2);
});
});
});

0 comments on commit 08da1bf

Please sign in to comment.