From 4f819ea4ef8badce96e3e8f328600120aa5c8291 Mon Sep 17 00:00:00 2001 From: luo3house <96865086+luo3house@users.noreply.github.com> Date: Fri, 24 Mar 2023 17:23:39 +0800 Subject: [PATCH] fix(useModal): forward modal config (#41422) --- components/modal/__tests__/hook.test.tsx | 17 +++++++++++++++++ components/modal/useModal/HookModal.tsx | 9 +++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/components/modal/__tests__/hook.test.tsx b/components/modal/__tests__/hook.test.tsx index c4b5ff77da..0867fe06ad 100644 --- a/components/modal/__tests__/hook.test.tsx +++ b/components/modal/__tests__/hook.test.tsx @@ -350,4 +350,21 @@ describe('Modal.hook', () => { expect(document.body.querySelector('.bamboo')?.textContent).toEqual('好的'); }); + + it('it should call forwarded afterClose', () => { + const afterClose = jest.fn(); + const Demo = () => { + const [modal, contextHolder] = Modal.useModal(); + React.useEffect(() => { + modal.confirm({ title: 'Confirm', afterClose }); + }, []); + return contextHolder; + }; + + render(); + const btns = document.body.querySelectorAll('.ant-btn'); + fireEvent.click(btns[btns.length - 1]); + + expect(afterClose).toHaveBeenCalledTimes(1); + }); }); diff --git a/components/modal/useModal/HookModal.tsx b/components/modal/useModal/HookModal.tsx index f2528b58d6..d1e699fdc5 100644 --- a/components/modal/useModal/HookModal.tsx +++ b/components/modal/useModal/HookModal.tsx @@ -16,7 +16,7 @@ export interface HookModalRef { } const HookModal: React.ForwardRefRenderFunction = ( - { afterClose, config }, + { afterClose: hookAfterClose, config }, ref, ) => { const [open, setOpen] = React.useState(true); @@ -26,6 +26,11 @@ const HookModal: React.ForwardRefRenderFunction = const prefixCls = getPrefixCls('modal'); const rootPrefixCls = getPrefixCls(); + const afterClose = () => { + hookAfterClose(); + innerConfig.afterClose?.(); + }; + const close = (...args: any[]) => { setOpen(false); const triggerCancel = args.some((param) => param && param.triggerCancel); @@ -59,7 +64,7 @@ const HookModal: React.ForwardRefRenderFunction = okText={ innerConfig.okText || (mergedOkCancel ? contextLocale?.okText : contextLocale?.justOkText) } - direction={direction} + direction={innerConfig.direction || direction} cancelText={innerConfig.cancelText || contextLocale?.cancelText} /> );