mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-13 07:39:10 +08:00
16ecdbb58d
* fix(modal): fix the error of `onCancel` parameter of modal returned by `useModal` resolve: 36581 ref: https://github.com/ant-design/ant-design/issues/36581#issuecomment-1189396007 * test(confirm): 补充 modal 测试用例 * test(modal): 添加测试用例 * Revert "fix(modal): fix the error of `onCancel` parameter of modal returned by `useModal`" This reverts commit e4fcb3e62add63193ec9081b2c21e657379bebfb. * fix(modal): fix modal onOk/onCancel method is not a valid function when there is a close parameter closed: #36581 * chore: cancel the introduction of `noop` from third-party library
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import * as React from 'react';
|
|
import { ConfigContext } from '../../config-provider';
|
|
import LocaleReceiver from '../../locale-provider/LocaleReceiver';
|
|
import defaultLocale from '../../locale/default';
|
|
import ConfirmDialog from '../ConfirmDialog';
|
|
import type { ModalFuncProps } from '../Modal';
|
|
|
|
export interface HookModalProps {
|
|
afterClose: () => void;
|
|
config: ModalFuncProps;
|
|
}
|
|
|
|
export interface HookModalRef {
|
|
destroy: () => void;
|
|
update: (config: ModalFuncProps) => void;
|
|
}
|
|
|
|
interface ModalLocale {
|
|
okText: string;
|
|
cancelText: string;
|
|
justOkText: string;
|
|
}
|
|
|
|
const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> = (
|
|
{ afterClose, config },
|
|
ref,
|
|
) => {
|
|
const [visible, setVisible] = React.useState(true);
|
|
const [innerConfig, setInnerConfig] = React.useState(config);
|
|
const { direction, getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('modal');
|
|
const rootPrefixCls = getPrefixCls();
|
|
|
|
const close = (...args: any[]) => {
|
|
setVisible(false);
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
|
if (innerConfig.onCancel && triggerCancel) {
|
|
innerConfig.onCancel(() => {}, ...args.slice(1));
|
|
}
|
|
};
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
destroy: close,
|
|
update: (newConfig: ModalFuncProps) => {
|
|
setInnerConfig(originConfig => ({
|
|
...originConfig,
|
|
...newConfig,
|
|
}));
|
|
},
|
|
}));
|
|
|
|
return (
|
|
<LocaleReceiver componentName="Modal" defaultLocale={defaultLocale.Modal}>
|
|
{(modalLocale: ModalLocale) => (
|
|
<ConfirmDialog
|
|
prefixCls={prefixCls}
|
|
rootPrefixCls={rootPrefixCls}
|
|
{...innerConfig}
|
|
close={close}
|
|
visible={visible}
|
|
afterClose={afterClose}
|
|
okText={
|
|
innerConfig.okText ||
|
|
(innerConfig.okCancel ? modalLocale.okText : modalLocale.justOkText)
|
|
}
|
|
direction={direction}
|
|
cancelText={innerConfig.cancelText || modalLocale.cancelText}
|
|
/>
|
|
)}
|
|
</LocaleReceiver>
|
|
);
|
|
};
|
|
|
|
export default React.forwardRef(HookModal);
|