ant-design/components/modal/confirm.tsx

136 lines
3.3 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import * as React from 'react';
import * as 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';
import classNames from 'classnames';
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
class ActionButton extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: false,
};
}
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>
);
}
}
export default function confirm(config) {
const props = assign({ iconType: 'question-circle' }, config);
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
// 默认为 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;
2015-06-10 22:02:13 +08:00
function close() {
const unmountResult = ReactDOM.unmountComponentAtNode(div);
if (unmountResult) {
div.parentNode.removeChild(div);
}
2015-06-10 22:02:13 +08:00
}
let body = (
<div className="ant-confirm-body">
<Icon type={props.iconType} />
<span className="ant-confirm-title">{props.title}</span>
<div className="ant-confirm-content">{props.content}</div>
</div>
);
2015-06-10 22:02:13 +08:00
let footer = null;
2015-09-07 11:48:57 +08:00
if (props.okCancel) {
footer = (
<div className="ant-confirm-btns">
<ActionButton type="ghost" actionFn={props.onCancel} closeModal={close}>
{props.cancelText}
</ActionButton>
<ActionButton type="primary" actionFn={props.onOk} closeModal={close}>
{props.okText}
</ActionButton>
</div>
);
2015-09-07 11:48:57 +08:00
} else {
footer = (
<div className="ant-confirm-btns">
<ActionButton type="primary" actionFn={props.onOk} closeModal={close}>
{props.okText}
</ActionButton>
</div>
);
2015-09-07 11:48:57 +08:00
}
const classString = classNames({
'ant-confirm': true,
[`ant-confirm-${props.type}`]: true,
[props.className]: !!props.className,
});
ReactDOM.render(
<Dialog
className={classString}
visible
closable={false}
title=""
transitionName="zoom"
footer=""
maskTransitionName="fade"
style={style}
width={width}
>
<div style={{ zoom: 1, overflow: 'hidden' }}>{body} {footer}</div>
</Dialog>
, div);
return {
destroy: close,
};
}