ant-design/components/modal/confirm.tsx

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2016-09-21 11:54:53 +08:00
import React from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
2016-12-19 14:01:52 +08:00
import Icon from '../icon';
import Dialog from './Modal';
import ActionButton from './ActionButton';
import { getConfirmLocale } from './locale';
export default function confirm(config) {
const props = {
iconType: 'question-circle',
...config,
};
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;
let style = props.style || {};
2015-09-07 11:48:57 +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
const runtimeLocale = getConfirmLocale();
props.okText = props.okText ||
(props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
props.cancelText = props.cancelText || runtimeLocale.cancelText;
function close(...args) {
const unmountResult = ReactDOM.unmountComponentAtNode(div);
2016-12-19 14:01:52 +08:00
if (unmountResult && div.parentNode) {
div.parentNode.removeChild(div);
}
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
}
let body = (
<div className={`${prefixCls}-body`}>
<Icon type={props.iconType} />
<span className={`${prefixCls}-title`}>{props.title}</span>
<div className={`${prefixCls}-content`}>{props.content}</div>
</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) {
footer = (
<div className={`${prefixCls}-btns`}>
2017-02-04 22:35:33 +08:00
<ActionButton actionFn={props.onCancel} closeModal={close}>
{props.cancelText}
</ActionButton>
2017-05-31 15:48:11 +08:00
<ActionButton type="primary" actionFn={props.onOk} closeModal={close} autoFocus>
{props.okText}
</ActionButton>
</div>
);
2015-09-07 11:48:57 +08:00
} else {
footer = (
<div className={`${prefixCls}-btns`}>
2017-05-31 15:48:11 +08:00
<ActionButton type="primary" actionFn={props.onOk} closeModal={close} autoFocus>
{props.okText}
</ActionButton>
</div>
);
2015-09-07 11:48:57 +08:00
}
const classString = classNames(prefixCls, {
[`${prefixCls}-${props.type}`]: true,
}, props.className);
ReactDOM.render(
<Dialog
className={classString}
onCancel={close.bind(this, { triggerCancel: true })}
2017-05-31 15:48:11 +08:00
visible
title=""
transitionName="zoom"
footer=""
maskTransitionName="fade"
maskClosable={maskClosable}
style={style}
width={width}
>
2016-10-07 17:59:00 +08:00
<div className={`${prefixCls}-body-wrapper`}>
{body} {footer}
</div>
</Dialog>
, div);
return {
destroy: close,
};
}