2017-11-17 14:38:54 +08:00
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2016-04-11 14:57:29 +08:00
|
|
|
|
import classNames from 'classnames';
|
2016-12-19 14:01:52 +08:00
|
|
|
|
import Icon from '../icon';
|
2017-11-21 17:16:48 +08:00
|
|
|
|
import Dialog, { ModalFuncProps } from './Modal';
|
2016-12-19 14:01:52 +08:00
|
|
|
|
import ActionButton from './ActionButton';
|
|
|
|
|
import { getConfirmLocale } from './locale';
|
2016-08-14 15:19:47 +08:00
|
|
|
|
|
2017-11-21 17:16:48 +08:00
|
|
|
|
export default function confirm(config: ModalFuncProps) {
|
|
|
|
|
const props: ModalFuncProps = {
|
2017-07-03 16:57:11 +08:00
|
|
|
|
iconType: 'question-circle',
|
2017-07-16 17:42:02 +08:00
|
|
|
|
okType: 'primary',
|
2017-07-03 16:57:11 +08:00
|
|
|
|
...config,
|
|
|
|
|
};
|
2016-09-14 16:18:33 +08:00
|
|
|
|
const prefixCls = props.prefixCls || 'ant-confirm';
|
2015-09-18 16:00:12 +08:00
|
|
|
|
let div = document.createElement('div');
|
|
|
|
|
document.body.appendChild(div);
|
|
|
|
|
|
2015-09-07 11:48:57 +08:00
|
|
|
|
let width = props.width || 416;
|
2016-05-05 16:52:36 +08:00
|
|
|
|
let style = props.style || {};
|
2015-09-07 11:48:57 +08:00
|
|
|
|
|
2017-01-07 15:17:27 +08:00
|
|
|
|
// 默认为 false,保持旧版默认行为
|
|
|
|
|
const maskClosable = props.maskClosable === undefined ? false : props.maskClosable;
|
|
|
|
|
|
2015-09-07 11:48:57 +08:00
|
|
|
|
// 默认为 true,保持向下兼容
|
|
|
|
|
if (!('okCancel' in props)) {
|
|
|
|
|
props.okCancel = true;
|
|
|
|
|
}
|
2015-06-10 22:02:13 +08:00
|
|
|
|
|
2016-05-12 13:25:00 +08:00
|
|
|
|
const runtimeLocale = getConfirmLocale();
|
|
|
|
|
|
2016-03-05 17:14:45 +08:00
|
|
|
|
props.okText = props.okText ||
|
|
|
|
|
(props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
|
|
|
|
|
props.cancelText = props.cancelText || runtimeLocale.cancelText;
|
2015-11-24 16:48:21 +08:00
|
|
|
|
|
2017-11-21 17:16:48 +08:00
|
|
|
|
function close(...args: any[]) {
|
2016-08-15 17:49:41 +08:00
|
|
|
|
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
2016-12-19 14:01:52 +08:00
|
|
|
|
if (unmountResult && div.parentNode) {
|
2016-08-15 17:49:41 +08:00
|
|
|
|
div.parentNode.removeChild(div);
|
|
|
|
|
}
|
2017-03-17 11:47:36 +08:00
|
|
|
|
const triggerCancel = args && args.length &&
|
|
|
|
|
args.some(param => param && param.triggerCancel);
|
|
|
|
|
if (props.onCancel && triggerCancel) {
|
|
|
|
|
props.onCancel(...args);
|
|
|
|
|
}
|
2015-06-10 22:02:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 14:21:29 +08:00
|
|
|
|
let body = (
|
2016-09-14 16:18:33 +08:00
|
|
|
|
<div className={`${prefixCls}-body`}>
|
2017-11-21 17:16:48 +08:00
|
|
|
|
<Icon type={props.iconType!} />
|
2016-09-14 16:18:33 +08:00
|
|
|
|
<span className={`${prefixCls}-title`}>{props.title}</span>
|
|
|
|
|
<div className={`${prefixCls}-content`}>{props.content}</div>
|
2016-01-07 14:21:29 +08:00
|
|
|
|
</div>
|
|
|
|
|
);
|
2015-06-10 22:02:13 +08:00
|
|
|
|
|
2016-10-24 12:04:26 +08:00
|
|
|
|
let footer: React.ReactElement<any> | null = null;
|
2015-09-07 11:48:57 +08:00
|
|
|
|
if (props.okCancel) {
|
2016-01-07 14:21:29 +08:00
|
|
|
|
footer = (
|
2016-09-14 16:18:33 +08:00
|
|
|
|
<div className={`${prefixCls}-btns`}>
|
2017-07-17 10:50:58 +08:00
|
|
|
|
<ActionButton actionFn={props.onCancel} closeModal={close}>
|
2016-01-07 14:21:29 +08:00
|
|
|
|
{props.cancelText}
|
2016-08-14 15:19:47 +08:00
|
|
|
|
</ActionButton>
|
2017-07-16 17:42:02 +08:00
|
|
|
|
<ActionButton type={props.okType} actionFn={props.onOk} closeModal={close} autoFocus>
|
2016-01-07 14:21:29 +08:00
|
|
|
|
{props.okText}
|
2016-08-14 15:19:47 +08:00
|
|
|
|
</ActionButton>
|
2016-01-07 14:21:29 +08:00
|
|
|
|
</div>
|
|
|
|
|
);
|
2015-09-07 11:48:57 +08:00
|
|
|
|
} else {
|
2016-01-07 14:21:29 +08:00
|
|
|
|
footer = (
|
2016-09-14 16:18:33 +08:00
|
|
|
|
<div className={`${prefixCls}-btns`}>
|
2017-07-16 17:42:02 +08:00
|
|
|
|
<ActionButton type={props.okType} actionFn={props.onOk} closeModal={close} autoFocus>
|
2016-01-07 14:21:29 +08:00
|
|
|
|
{props.okText}
|
2016-08-14 15:19:47 +08:00
|
|
|
|
</ActionButton>
|
2016-01-07 14:21:29 +08:00
|
|
|
|
</div>
|
|
|
|
|
);
|
2015-09-07 11:48:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 10:20:23 +08:00
|
|
|
|
const classString = classNames(prefixCls, {
|
2016-09-14 16:18:33 +08:00
|
|
|
|
[`${prefixCls}-${props.type}`]: true,
|
2016-11-30 10:20:23 +08:00
|
|
|
|
}, props.className);
|
2016-04-11 14:57:29 +08:00
|
|
|
|
|
2016-05-05 16:52:36 +08:00
|
|
|
|
ReactDOM.render(
|
|
|
|
|
<Dialog
|
|
|
|
|
className={classString}
|
2017-03-17 11:47:36 +08:00
|
|
|
|
onCancel={close.bind(this, { triggerCancel: true })}
|
2017-05-31 15:48:11 +08:00
|
|
|
|
visible
|
2016-05-05 16:52:36 +08:00
|
|
|
|
title=""
|
|
|
|
|
transitionName="zoom"
|
|
|
|
|
footer=""
|
|
|
|
|
maskTransitionName="fade"
|
2017-01-07 15:17:27 +08:00
|
|
|
|
maskClosable={maskClosable}
|
2016-05-05 16:52:36 +08:00
|
|
|
|
style={style}
|
2016-06-06 13:54:10 +08:00
|
|
|
|
width={width}
|
2017-07-19 09:21:20 +08:00
|
|
|
|
zIndex={props.zIndex}
|
2016-06-06 13:54:10 +08:00
|
|
|
|
>
|
2016-10-07 17:59:00 +08:00
|
|
|
|
<div className={`${prefixCls}-body-wrapper`}>
|
|
|
|
|
{body} {footer}
|
|
|
|
|
</div>
|
2016-05-05 16:52:36 +08:00
|
|
|
|
</Dialog>
|
2016-08-15 17:49:41 +08:00
|
|
|
|
, div);
|
2016-06-21 18:50:07 +08:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
destroy: close,
|
|
|
|
|
};
|
2015-07-23 22:23:11 +08:00
|
|
|
|
}
|