2020-02-03 13:00:35 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { ModalFuncProps } from '../Modal';
|
|
|
|
import ConfirmDialog from '../ConfirmDialog';
|
|
|
|
import defaultLocale from '../../locale/default';
|
|
|
|
import LocaleReceiver from '../../locale-provider/LocaleReceiver';
|
2020-06-03 17:20:04 +08:00
|
|
|
import { ConfigContext } from '../../config-provider';
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
export interface HookModalProps {
|
|
|
|
afterClose: () => void;
|
|
|
|
config: ModalFuncProps;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface HookModalRef {
|
|
|
|
destroy: () => void;
|
|
|
|
update: (config: ModalFuncProps) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ModalLocale {
|
|
|
|
okText: string;
|
|
|
|
cancelText: string;
|
|
|
|
justOkText: string;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
) => {
|
|
|
|
const [visible, setVisible] = React.useState(true);
|
|
|
|
const [innerConfig, setInnerConfig] = React.useState(config);
|
2020-06-03 17:20:04 +08:00
|
|
|
const { direction } = React.useContext(ConfigContext);
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
function close() {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
|
|
destroy: close,
|
|
|
|
update: (newConfig: ModalFuncProps) => {
|
|
|
|
setInnerConfig(originConfig => ({
|
|
|
|
...originConfig,
|
|
|
|
...newConfig,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<LocaleReceiver componentName="Modal" defaultLocale={defaultLocale.Modal}>
|
|
|
|
{(modalLocale: ModalLocale) => (
|
|
|
|
<ConfirmDialog
|
|
|
|
{...innerConfig}
|
|
|
|
close={close}
|
|
|
|
visible={visible}
|
|
|
|
afterClose={afterClose}
|
|
|
|
okText={
|
|
|
|
innerConfig.okText ||
|
|
|
|
(innerConfig.okCancel ? modalLocale.okText : modalLocale.justOkText)
|
|
|
|
}
|
2020-06-03 17:20:04 +08:00
|
|
|
direction={direction}
|
2020-02-03 13:00:35 +08:00
|
|
|
cancelText={innerConfig.cancelText || modalLocale.cancelText}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.forwardRef(HookModal);
|