Browse Source

fix(useModal): forward modal config (#41422)

pull/41442/head
luo3house 2 years ago
committed by GitHub
parent
commit
4f819ea4ef
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      components/modal/__tests__/hook.test.tsx
  2. 9
      components/modal/useModal/HookModal.tsx

17
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(<Demo />);
const btns = document.body.querySelectorAll('.ant-btn');
fireEvent.click(btns[btns.length - 1]);
expect(afterClose).toHaveBeenCalledTimes(1);
});
});

9
components/modal/useModal/HookModal.tsx

@ -16,7 +16,7 @@ export interface HookModalRef {
}
const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> = (
{ afterClose, config },
{ afterClose: hookAfterClose, config },
ref,
) => {
const [open, setOpen] = React.useState(true);
@ -26,6 +26,11 @@ const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> =
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<HookModalRef, HookModalProps> =
okText={
innerConfig.okText || (mergedOkCancel ? contextLocale?.okText : contextLocale?.justOkText)
}
direction={direction}
direction={innerConfig.direction || direction}
cancelText={innerConfig.cancelText || contextLocale?.cancelText}
/>
);

Loading…
Cancel
Save