2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2020-02-03 13:00:35 +08:00
|
|
|
import {
|
|
|
|
InfoCircleOutlined,
|
|
|
|
CheckCircleOutlined,
|
|
|
|
CloseCircleOutlined,
|
|
|
|
ExclamationCircleOutlined,
|
|
|
|
} from '@ant-design/icons';
|
2016-12-19 14:01:52 +08:00
|
|
|
import { getConfirmLocale } from './locale';
|
2020-02-03 13:00:35 +08:00
|
|
|
import { ModalFuncProps, destroyFns } from './Modal';
|
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
|
|
|
|
|
|
|
export type ModalFunc = (
|
|
|
|
props: ModalFuncProps,
|
|
|
|
) => {
|
|
|
|
destroy: () => void;
|
|
|
|
update: (newConfig: ModalFuncProps) => void;
|
|
|
|
};
|
2016-08-14 15:19:47 +08:00
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
export interface ModalStaticFunctions {
|
|
|
|
info: ModalFunc;
|
|
|
|
success: ModalFunc;
|
|
|
|
error: ModalFunc;
|
|
|
|
warn: ModalFunc;
|
|
|
|
warning: ModalFunc;
|
|
|
|
confirm: ModalFunc;
|
2017-12-02 22:10:27 +08:00
|
|
|
}
|
2015-09-18 16:00:12 +08:00
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
export default function confirm(config: ModalFuncProps) {
|
2018-08-25 22:08:19 +08:00
|
|
|
const div = document.createElement('div');
|
2017-12-02 22:10:27 +08:00
|
|
|
document.body.appendChild(div);
|
2019-08-06 15:02:05 +08:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
2018-08-25 21:51:19 +08:00
|
|
|
let currentConfig = { ...config, close, visible: true } as any;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
|
|
|
function destroy(...args: any[]) {
|
|
|
|
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
|
|
|
if (unmountResult && div.parentNode) {
|
|
|
|
div.parentNode.removeChild(div);
|
|
|
|
}
|
2018-12-12 03:51:31 +08:00
|
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
2017-12-02 22:10:27 +08:00
|
|
|
if (config.onCancel && triggerCancel) {
|
|
|
|
config.onCancel(...args);
|
|
|
|
}
|
2018-12-20 17:43:22 +08:00
|
|
|
for (let i = 0; i < destroyFns.length; i++) {
|
2018-12-26 21:36:36 +08:00
|
|
|
const fn = destroyFns[i];
|
2019-08-06 15:09:07 +08:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
2019-01-30 10:46:30 +08:00
|
|
|
if (fn === close) {
|
2018-12-26 21:36:36 +08:00
|
|
|
destroyFns.splice(i, 1);
|
|
|
|
break;
|
2018-12-03 17:08:48 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-02 22:10:27 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
function render({ okText, cancelText, ...props }: any) {
|
|
|
|
const runtimeLocale = getConfirmLocale();
|
|
|
|
ReactDOM.render(
|
|
|
|
<ConfirmDialog
|
|
|
|
{...props}
|
|
|
|
okText={okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText)}
|
|
|
|
cancelText={cancelText || runtimeLocale.cancelText}
|
|
|
|
/>,
|
|
|
|
div,
|
|
|
|
);
|
2017-12-02 22:10:27 +08:00
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
function close(...args: any[]) {
|
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
|
|
|
visible: false,
|
|
|
|
afterClose: destroy.bind(this, ...args),
|
|
|
|
};
|
2020-02-03 13:00:35 +08:00
|
|
|
render(currentConfig);
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function update(newConfig: ModalFuncProps) {
|
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
|
|
|
...newConfig,
|
|
|
|
};
|
|
|
|
render(currentConfig);
|
2017-12-02 22:10:27 +08:00
|
|
|
}
|
|
|
|
|
2018-08-25 21:51:19 +08:00
|
|
|
render(currentConfig);
|
2016-06-21 18:50:07 +08:00
|
|
|
|
2018-12-26 21:36:36 +08:00
|
|
|
destroyFns.push(close);
|
2018-12-03 17:08:48 +08:00
|
|
|
|
2016-06-21 18:50:07 +08:00
|
|
|
return {
|
|
|
|
destroy: close,
|
2018-08-25 21:51:19 +08:00
|
|
|
update,
|
2016-06-21 18:50:07 +08:00
|
|
|
};
|
2015-07-23 22:23:11 +08:00
|
|
|
}
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
export function withWarn(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
return {
|
|
|
|
type: 'warning',
|
|
|
|
icon: <ExclamationCircleOutlined />,
|
|
|
|
okCancel: false,
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withInfo(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
return {
|
|
|
|
type: 'info',
|
|
|
|
icon: <InfoCircleOutlined />,
|
|
|
|
okCancel: false,
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withSuccess(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
return {
|
|
|
|
type: 'success',
|
|
|
|
icon: <CheckCircleOutlined />,
|
|
|
|
okCancel: false,
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withError(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
icon: <CloseCircleOutlined />,
|
|
|
|
okCancel: false,
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withConfirm(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
return {
|
|
|
|
type: 'confirm',
|
|
|
|
okCancel: true,
|
|
|
|
...props,
|
|
|
|
};
|
|
|
|
}
|