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-06-22 14:57:09 +08:00
|
|
|
import LocaleReceiver from '../../locale-provider/LocaleReceiver';
|
|
|
|
import defaultLocale from '../../locale/default';
|
|
|
|
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);
|
2020-11-30 15:25:36 +08:00
|
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
|
|
|
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) => {
|
|
|
|
setInnerConfig(originConfig => ({
|
|
|
|
...originConfig,
|
|
|
|
...newConfig,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<LocaleReceiver componentName="Modal" defaultLocale={defaultLocale.Modal}>
|
2022-10-01 22:17:14 +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={
|
|
|
|
innerConfig.okText ||
|
2022-10-01 22:17:14 +08:00
|
|
|
(innerConfig.okCancel ? 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);
|