2020-02-03 13:00:35 +08:00
|
|
|
import * as React from 'react';
|
2020-06-03 17:20:04 +08:00
|
|
|
import { ConfigContext } from '../../config-provider';
|
2022-12-09 15:04:08 +08:00
|
|
|
import LocaleReceiver from '../../locale/LocaleReceiver';
|
2022-10-26 14:38:54 +08:00
|
|
|
import defaultLocale from '../../locale/en_US';
|
2022-06-22 14:57:09 +08:00
|
|
|
import ConfirmDialog from '../ConfirmDialog';
|
|
|
|
import type { ModalFuncProps } from '../Modal';
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
export interface HookModalProps {
|
|
|
|
afterClose: () => void;
|
|
|
|
config: ModalFuncProps;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface HookModalRef {
|
|
|
|
destroy: () => void;
|
|
|
|
update: (config: ModalFuncProps) => void;
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:12:31 +08:00
|
|
|
const HookModal: React.ForwardRefRenderFunction<HookModalRef, HookModalProps> = (
|
2020-02-03 13:00:35 +08:00
|
|
|
{ afterClose, config },
|
|
|
|
ref,
|
|
|
|
) => {
|
2022-08-23 16:55:57 +08:00
|
|
|
const [open, setOpen] = React.useState(true);
|
2020-02-03 13:00:35 +08:00
|
|
|
const [innerConfig, setInnerConfig] = React.useState(config);
|
2020-08-03 11:41:08 +08:00
|
|
|
const { direction, getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('modal');
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-02-03 13:00:35 +08:00
|
|
|
|
2021-08-30 13:04:21 +08:00
|
|
|
const close = (...args: any[]) => {
|
2022-08-23 16:55:57 +08:00
|
|
|
setOpen(false);
|
2022-11-19 13:47:33 +08:00
|
|
|
const triggerCancel = args.some((param) => param && param.triggerCancel);
|
2020-11-30 15:25:36 +08:00
|
|
|
if (innerConfig.onCancel && triggerCancel) {
|
2022-07-23 16:04:23 +08:00
|
|
|
innerConfig.onCancel(() => {}, ...args.slice(1));
|
2020-11-30 15:25:36 +08:00
|
|
|
}
|
2021-08-30 13:04:21 +08:00
|
|
|
};
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
|
|
destroy: close,
|
|
|
|
update: (newConfig: ModalFuncProps) => {
|
2022-11-19 13:47:33 +08:00
|
|
|
setInnerConfig((originConfig) => ({
|
2020-02-03 13:00:35 +08:00
|
|
|
...originConfig,
|
|
|
|
...newConfig,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2022-12-29 17:05:50 +08:00
|
|
|
const mergedOkCancel = innerConfig.okCancel ?? innerConfig.type === 'confirm';
|
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
return (
|
|
|
|
<LocaleReceiver componentName="Modal" defaultLocale={defaultLocale.Modal}>
|
2022-11-19 13:47:33 +08:00
|
|
|
{(contextLocale) => (
|
2020-02-03 13:00:35 +08:00
|
|
|
<ConfirmDialog
|
2020-08-03 11:41:08 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
rootPrefixCls={rootPrefixCls}
|
2020-02-03 13:00:35 +08:00
|
|
|
{...innerConfig}
|
|
|
|
close={close}
|
2022-08-23 16:55:57 +08:00
|
|
|
open={open}
|
2020-02-03 13:00:35 +08:00
|
|
|
afterClose={afterClose}
|
|
|
|
okText={
|
2022-12-29 17:05:50 +08:00
|
|
|
innerConfig.okText || (mergedOkCancel ? contextLocale.okText : contextLocale.justOkText)
|
2020-02-03 13:00:35 +08:00
|
|
|
}
|
2020-06-03 17:20:04 +08:00
|
|
|
direction={direction}
|
2022-10-01 22:17:14 +08:00
|
|
|
cancelText={innerConfig.cancelText || contextLocale.cancelText}
|
2020-02-03 13:00:35 +08:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.forwardRef(HookModal);
|