2016-09-21 11:54:53 +08:00
|
|
|
|
import React from 'react';
|
|
|
|
|
import ReactDOM from 'react-dom';
|
2016-01-13 16:46:38 +08:00
|
|
|
|
import Dialog from './Modal';
|
2015-11-20 11:05:34 +08:00
|
|
|
|
import Icon from '../icon';
|
2015-11-03 20:06:44 +08:00
|
|
|
|
import Button from '../button';
|
2016-04-11 14:57:29 +08:00
|
|
|
|
import classNames from 'classnames';
|
2016-05-12 13:25:00 +08:00
|
|
|
|
import { getConfirmLocale } from './locale';
|
2016-06-22 13:18:43 +08:00
|
|
|
|
import assign from 'object-assign';
|
2016-07-15 12:37:53 +08:00
|
|
|
|
|
2016-08-22 17:26:14 +08:00
|
|
|
|
export interface ActionButtonProps {
|
|
|
|
|
type: 'primary' | 'ghost' | 'dashed';
|
|
|
|
|
actionFn: Function;
|
|
|
|
|
closeModal: Function;
|
2016-09-20 08:58:20 +08:00
|
|
|
|
autoFocus?: Boolean;
|
2016-08-22 17:26:14 +08:00
|
|
|
|
}
|
|
|
|
|
class ActionButton extends React.Component<ActionButtonProps, any> {
|
2016-09-20 08:58:20 +08:00
|
|
|
|
timeoutId: number;
|
2016-08-14 15:19:47 +08:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
loading: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-09-20 08:58:20 +08:00
|
|
|
|
componentDidMount () {
|
|
|
|
|
if (this.props.autoFocus) {
|
|
|
|
|
const $this = ReactDOM.findDOMNode(this) as HTMLInputElement;
|
|
|
|
|
this.timeoutId = setTimeout(() => $this.focus());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
|
clearTimeout(this.timeoutId);
|
|
|
|
|
}
|
2016-08-14 15:19:47 +08:00
|
|
|
|
onClick = () => {
|
|
|
|
|
const { actionFn, closeModal } = this.props;
|
|
|
|
|
if (actionFn) {
|
|
|
|
|
let ret;
|
|
|
|
|
if (actionFn.length) {
|
|
|
|
|
ret = actionFn(closeModal);
|
|
|
|
|
} else {
|
|
|
|
|
ret = actionFn();
|
|
|
|
|
if (!ret) {
|
|
|
|
|
closeModal();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ret && ret.then) {
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
ret.then((...args) => {
|
|
|
|
|
// It's unnecessary to set loading=false, for the Modal will be unmounted after close.
|
|
|
|
|
// this.setState({ loading: false });
|
|
|
|
|
closeModal(...args);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
closeModal();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { type, children } = this.props;
|
|
|
|
|
const loading = this.state.loading;
|
|
|
|
|
return (
|
|
|
|
|
<Button type={type} size="large" onClick={this.onClick} loading={loading}>
|
|
|
|
|
{children}
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-05 17:14:45 +08:00
|
|
|
|
export default function confirm(config) {
|
2016-08-14 15:19:47 +08:00
|
|
|
|
const props = assign({ iconType: 'question-circle' }, 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
|
|
|
|
|
|
|
|
|
// 默认为 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
|
|
|
|
|
2015-06-10 22:02:13 +08:00
|
|
|
|
function close() {
|
2016-08-15 17:49:41 +08:00
|
|
|
|
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
|
|
|
|
if (unmountResult) {
|
|
|
|
|
div.parentNode.removeChild(div);
|
|
|
|
|
}
|
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`}>
|
2016-04-11 14:57:29 +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
|
|
|
|
|
2015-11-24 11:01:36 +08:00
|
|
|
|
let footer = 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`}>
|
2016-08-14 15:19:47 +08:00
|
|
|
|
<ActionButton type="ghost" actionFn={props.onCancel} closeModal={close}>
|
2016-01-07 14:21:29 +08:00
|
|
|
|
{props.cancelText}
|
2016-08-14 15:19:47 +08:00
|
|
|
|
</ActionButton>
|
2016-09-20 08:58:20 +08:00
|
|
|
|
<ActionButton type="primary" 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`}>
|
2016-09-20 08:58:20 +08:00
|
|
|
|
<ActionButton type="primary" 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-04-11 14:57:29 +08:00
|
|
|
|
const classString = classNames({
|
2016-09-14 16:18:33 +08:00
|
|
|
|
[prefixCls]: true,
|
|
|
|
|
[`${prefixCls}-${props.type}`]: true,
|
2016-04-11 14:57:29 +08:00
|
|
|
|
[props.className]: !!props.className,
|
|
|
|
|
});
|
|
|
|
|
|
2016-05-05 16:52:36 +08:00
|
|
|
|
ReactDOM.render(
|
|
|
|
|
<Dialog
|
|
|
|
|
className={classString}
|
2016-09-20 08:58:20 +08:00
|
|
|
|
onCancel={close}
|
2016-05-05 16:52:36 +08:00
|
|
|
|
visible
|
|
|
|
|
title=""
|
|
|
|
|
transitionName="zoom"
|
|
|
|
|
footer=""
|
|
|
|
|
maskTransitionName="fade"
|
2016-09-20 08:58:20 +08:00
|
|
|
|
maskClosable={false}
|
2016-05-05 16:52:36 +08:00
|
|
|
|
style={style}
|
2016-06-06 13:54:10 +08:00
|
|
|
|
width={width}
|
|
|
|
|
>
|
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
|
|
|
|
}
|