ant-design/components/modal/confirm.tsx

155 lines
3.8 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
2016-12-19 14:01:52 +08:00
import { getConfirmLocale } from './locale';
import { ModalFuncProps, destroyFns } from './Modal';
import ConfirmDialog from './ConfirmDialog';
let defaultRootPrefixCls = 'ant';
function getRootPrefixCls() {
return defaultRootPrefixCls;
}
export type ModalFunc = (
props: ModalFuncProps,
) => {
destroy: () => void;
update: (newConfig: ModalFuncProps) => void;
};
export interface ModalStaticFunctions {
info: ModalFunc;
success: ModalFunc;
error: ModalFunc;
warn: ModalFunc;
warning: ModalFunc;
confirm: ModalFunc;
}
2015-09-18 16:00:12 +08:00
export default function confirm(config: ModalFuncProps) {
2018-08-25 22:08:19 +08:00
const div = document.createElement('div');
document.body.appendChild(div);
// eslint-disable-next-line no-use-before-define
let currentConfig = { ...config, close, visible: true } as any;
function destroy(...args: any[]) {
const unmountResult = ReactDOM.unmountComponentAtNode(div);
if (unmountResult && div.parentNode) {
div.parentNode.removeChild(div);
}
const triggerCancel = args.some(param => param && param.triggerCancel);
if (config.onCancel && triggerCancel) {
config.onCancel(...args);
}
2018-12-20 17:43:22 +08:00
for (let i = 0; i < destroyFns.length; i++) {
const fn = destroyFns[i];
2019-08-06 15:09:07 +08:00
// eslint-disable-next-line no-use-before-define
if (fn === close) {
destroyFns.splice(i, 1);
break;
}
}
}
function render({ okText, cancelText, prefixCls, ...props }: any) {
/**
* https://github.com/ant-design/ant-design/issues/23623
* Sync render blocks React event. Let's make this async.
*/
setTimeout(() => {
const runtimeLocale = getConfirmLocale();
ReactDOM.render(
<ConfirmDialog
{...props}
prefixCls={prefixCls || `${getRootPrefixCls()}-modal`}
rootPrefixCls={getRootPrefixCls()}
okText={okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText)}
cancelText={cancelText || runtimeLocale.cancelText}
/>,
div,
);
});
}
2019-08-05 18:38:10 +08:00
function close(...args: any[]) {
currentConfig = {
...currentConfig,
visible: false,
afterClose: destroy.bind(this, ...args),
};
render(currentConfig);
2019-08-05 18:38:10 +08:00
}
function update(newConfig: ModalFuncProps) {
currentConfig = {
...currentConfig,
...newConfig,
};
render(currentConfig);
}
render(currentConfig);
destroyFns.push(close);
return {
destroy: close,
update,
};
}
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',
icon: <ExclamationCircleOutlined />,
okCancel: true,
...props,
};
}
export function globalConfig({ rootPrefixCls }: { rootPrefixCls?: string }) {
if (rootPrefixCls) {
defaultRootPrefixCls = rootPrefixCls;
}
}