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-12-02 22:10:27 +08:00
|
|
|
|
interface ConfirmDialogProps extends ModalFuncProps {
|
|
|
|
|
afterClose?: () => void;
|
|
|
|
|
close: (...args: any[]) => void;
|
|
|
|
|
}
|
2015-09-18 16:00:12 +08:00
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const IS_REACT_16 = !!ReactDOM.createPortal;
|
2015-09-07 11:48:57 +08:00
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const ConfirmDialog = (props: ConfirmDialogProps) => {
|
|
|
|
|
const { onCancel, onOk, close, zIndex, afterClose, visible } = props;
|
|
|
|
|
const iconType = props.iconType || 'question-circle';
|
|
|
|
|
const okType = props.okType || 'primary';
|
|
|
|
|
const prefixCls = props.prefixCls || 'ant-confirm';
|
|
|
|
|
// 默认为 true,保持向下兼容
|
|
|
|
|
const okCancel = ('okCancel' in props) ? props.okCancel! : true;
|
|
|
|
|
const width = props.width || 416;
|
|
|
|
|
const style = props.style || {};
|
2017-01-07 15:17:27 +08:00
|
|
|
|
// 默认为 false,保持旧版默认行为
|
|
|
|
|
const maskClosable = props.maskClosable === undefined ? false : props.maskClosable;
|
2016-05-12 13:25:00 +08:00
|
|
|
|
const runtimeLocale = getConfirmLocale();
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const okText = props.okText ||
|
|
|
|
|
(okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
|
|
|
|
|
const cancelText = props.cancelText || runtimeLocale.cancelText;
|
2016-05-12 13:25:00 +08:00
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const classString = classNames(
|
|
|
|
|
prefixCls,
|
|
|
|
|
`${prefixCls}-${props.type}`,
|
|
|
|
|
props.className,
|
2016-01-07 14:21:29 +08:00
|
|
|
|
);
|
2015-06-10 22:02:13 +08:00
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const cancelButton = okCancel && (
|
|
|
|
|
<ActionButton actionFn={onCancel} closeModal={close}>
|
|
|
|
|
{cancelText}
|
|
|
|
|
</ActionButton>
|
|
|
|
|
);
|
2016-04-11 14:57:29 +08:00
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
return (
|
2016-05-05 16:52:36 +08:00
|
|
|
|
<Dialog
|
|
|
|
|
className={classString}
|
2017-03-17 11:47:36 +08:00
|
|
|
|
onCancel={close.bind(this, { triggerCancel: true })}
|
2017-12-02 22:10:27 +08:00
|
|
|
|
visible={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-12-02 22:10:27 +08:00
|
|
|
|
zIndex={zIndex}
|
|
|
|
|
afterClose={afterClose}
|
2016-06-06 13:54:10 +08:00
|
|
|
|
>
|
2016-10-07 17:59:00 +08:00
|
|
|
|
<div className={`${prefixCls}-body-wrapper`}>
|
2017-12-02 22:10:27 +08:00
|
|
|
|
<div className={`${prefixCls}-body`}>
|
|
|
|
|
<Icon type={iconType!} />
|
|
|
|
|
<span className={`${prefixCls}-title`}>{props.title}</span>
|
|
|
|
|
<div className={`${prefixCls}-content`}>{props.content}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`${prefixCls}-btns`}>
|
|
|
|
|
{cancelButton}
|
|
|
|
|
<ActionButton type={okType} actionFn={onOk} closeModal={close} autoFocus>
|
|
|
|
|
{okText}
|
|
|
|
|
</ActionButton>
|
|
|
|
|
</div>
|
2016-10-07 17:59:00 +08:00
|
|
|
|
</div>
|
2016-05-05 16:52:36 +08:00
|
|
|
|
</Dialog>
|
2017-12-02 22:10:27 +08:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function confirm(config: ModalFuncProps) {
|
|
|
|
|
let div = document.createElement('div');
|
|
|
|
|
document.body.appendChild(div);
|
|
|
|
|
|
|
|
|
|
function close(...args: any[]) {
|
|
|
|
|
if (IS_REACT_16) {
|
|
|
|
|
render({ ...config, close, visible: false, afterClose: destroy.bind(this, ...args) });
|
|
|
|
|
} else {
|
|
|
|
|
destroy(...args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function destroy(...args: any[]) {
|
|
|
|
|
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
|
|
|
|
if (unmountResult && div.parentNode) {
|
|
|
|
|
div.parentNode.removeChild(div);
|
|
|
|
|
}
|
|
|
|
|
const triggerCancel = args && args.length &&
|
|
|
|
|
args.some(param => param && param.triggerCancel);
|
|
|
|
|
if (config.onCancel && triggerCancel) {
|
|
|
|
|
config.onCancel(...args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function render(props: any) {
|
|
|
|
|
ReactDOM.render(<ConfirmDialog {...props} />, div);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render({ ...config, visible: true, close });
|
2016-06-21 18:50:07 +08:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
destroy: close,
|
|
|
|
|
};
|
2015-07-23 22:23:11 +08:00
|
|
|
|
}
|