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-10-26 14:38:54 +08:00
|
|
|
import defaultLocale from '../../locale/en_US';
|
2023-02-22 18:18:26 +08:00
|
|
|
import useLocale from '../../locale/useLocale';
|
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';
|
|
|
|
|
2023-02-24 10:51:59 +08:00
|
|
|
const [contextLocale] = useLocale('Modal', defaultLocale.Modal);
|
2023-02-22 18:18:26 +08:00
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
return (
|
2023-02-22 18:18:26 +08:00
|
|
|
<ConfirmDialog
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
rootPrefixCls={rootPrefixCls}
|
|
|
|
{...innerConfig}
|
|
|
|
close={close}
|
|
|
|
open={open}
|
|
|
|
afterClose={afterClose}
|
|
|
|
okText={
|
|
|
|
innerConfig.okText || (mergedOkCancel ? contextLocale?.okText : contextLocale?.justOkText)
|
|
|
|
}
|
|
|
|
direction={direction}
|
|
|
|
cancelText={innerConfig.cancelText || contextLocale?.cancelText}
|
|
|
|
/>
|
2020-02-03 13:00:35 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.forwardRef(HookModal);
|