Skip to content

Commit e276c90

Browse files
EmilyyyLiu刘欢
andauthored
feat: Merge 'afterClose' to 'closable' (#500)
* feat: Merge 'AfterClose' to 'Closable' * docs: Add a through line to afterClose * feat: change closable AfterClose ts * fix: lint error * feat: add default value to closableAfterClose * feat: Call separately * fix: closable !== null * feat: change ts * feat: closable null * feat: null * feat: useEffect dependencies * fix: lint error * fix: lint error (Initializer provides no value) * fix: Delete non current modifications --------- Co-authored-by: 刘欢 <[email protected]>
1 parent a6af28e commit e276c90

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ ReactDOM.render(
6464
| maskTransitionName | String | | mask animation css class name | |
6565
| title | String\|React.Element | | Title of the dialog | |
6666
| footer | React.Element | | footer of the dialog | |
67-
| closable | Boolean \| ({ closeIcon?: React.ReactNode; disabled?: boolean } & React.AriaAttributes | true | whether show close button | |
67+
| closable | Boolean \| ({ closeIcon?: React.ReactNode; disabled?: boolean, afterClose:function } & React.AriaAttributes) | true | whether show close button | |
6868
| mask | Boolean | true | whether show mask | |
6969
| maskClosable | Boolean | true | whether click mask to close | |
7070
| keyboard | Boolean | true | whether support press esc to close | |

src/DialogWrap.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
2020
forceRender,
2121
destroyOnHidden = false,
2222
afterClose,
23+
closable,
2324
panelRef,
2425
} = props;
2526
const [animatedVisible, setAnimatedVisible] = React.useState<boolean>(visible);
@@ -49,6 +50,9 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
4950
{...props}
5051
destroyOnHidden={destroyOnHidden}
5152
afterClose={() => {
53+
const closableObj = closable && typeof closable === 'object' ? closable : {};
54+
const { afterClose: closableAfterClose } = closableObj || {};
55+
closableAfterClose?.();
5256
afterClose?.();
5357
setAnimatedVisible(false);
5458
}}

src/IDialogPropTypes.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export type ModalClassNames = Partial<Record<SemanticName, string>>;
77

88
export type ModalStyles = Partial<Record<SemanticName, CSSProperties>>;
99

10+
export type ClosableType = {
11+
closeIcon?: React.ReactNode;
12+
disabled?: boolean;
13+
afterClose?: () => any;
14+
};
15+
1016
export type IDialogPropTypes = {
1117
className?: string;
1218
keyboard?: boolean;
@@ -17,7 +23,7 @@ export type IDialogPropTypes = {
1723
afterClose?: () => any;
1824
afterOpenChange?: (open: boolean) => void;
1925
onClose?: (e: SyntheticEvent) => any;
20-
closable?: boolean | ({ closeIcon?: React.ReactNode; disabled?: boolean } & React.AriaAttributes);
26+
closable?: boolean | (ClosableType & React.AriaAttributes);
2127
maskClosable?: boolean;
2228
visible?: boolean;
2329
destroyOnHidden?: boolean;

tests/index.spec.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,25 @@ describe('dialog', () => {
552552

553553
expect(afterClose).toHaveBeenCalledTimes(0);
554554
});
555+
556+
it('should prioritize closable.afterClose when both exist', () => {
557+
const afterClose = jest.fn();
558+
const legacyAfterClose = jest.fn();
559+
560+
const { rerender } = render(
561+
<Dialog closable={{ afterClose }} afterClose={legacyAfterClose} visible />,
562+
);
563+
act(() => {
564+
jest.runAllTimers();
565+
});
566+
567+
rerender(<Dialog closable={{ afterClose }} afterClose={legacyAfterClose} visible={false} />);
568+
act(() => {
569+
jest.runAllTimers();
570+
});
571+
expect(afterClose).toHaveBeenCalledTimes(1);
572+
expect(legacyAfterClose).toHaveBeenCalledTimes(1);
573+
});
555574
});
556575

557576
describe('afterOpenChange', () => {

0 commit comments

Comments
 (0)