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;
|
2018-09-29 17:00:01 +08:00
|
|
|
|
autoFocusButton?: null | 'ok' | 'cancel';
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
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) => {
|
2018-09-30 14:50:03 +08:00
|
|
|
|
const { onCancel, onOk, close, zIndex, afterClose, visible, keyboard, centered, getContainer, okButtonProps, cancelButtonProps } = props;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
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;
|
2018-09-29 17:00:01 +08:00
|
|
|
|
const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok';
|
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 && (
|
2018-09-30 14:50:03 +08:00
|
|
|
|
<ActionButton actionFn={onCancel} closeModal={close} autoFocus={autoFocusButton === 'cancel'} buttonProps={cancelButtonProps}>
|
2017-12-02 22:10:27 +08:00
|
|
|
|
{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}
|
2018-08-01 10:50:34 +08:00
|
|
|
|
wrapClassName={classNames({ [`${prefixCls}-centered`]: !!props.centered })}
|
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}
|
2018-02-11 13:44:59 +08:00
|
|
|
|
keyboard={keyboard}
|
2018-08-30 22:27:32 +08:00
|
|
|
|
centered={centered}
|
2018-08-25 21:24:18 +08:00
|
|
|
|
getContainer={getContainer}
|
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}
|
2018-09-30 14:50:03 +08:00
|
|
|
|
<ActionButton type={okType} actionFn={onOk} closeModal={close} autoFocus={autoFocusButton === 'ok'} buttonProps={okButtonProps}>
|
2017-12-02 22:10:27 +08:00
|
|
|
|
{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) {
|
2018-08-25 22:08:19 +08:00
|
|
|
|
const div = document.createElement('div');
|
2017-12-02 22:10:27 +08:00
|
|
|
|
document.body.appendChild(div);
|
2018-08-25 21:51:19 +08:00
|
|
|
|
let currentConfig = { ...config, close, visible: true } as any;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
|
|
|
|
|
function close(...args: any[]) {
|
2018-08-25 21:51:19 +08:00
|
|
|
|
currentConfig = {
|
|
|
|
|
...currentConfig,
|
|
|
|
|
visible: false,
|
|
|
|
|
afterClose: destroy.bind(this, ...args),
|
|
|
|
|
};
|
2017-12-02 22:10:27 +08:00
|
|
|
|
if (IS_REACT_16) {
|
2018-08-25 21:51:19 +08:00
|
|
|
|
render(currentConfig);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
} else {
|
|
|
|
|
destroy(...args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 21:51:19 +08:00
|
|
|
|
function update(newConfig: ModalFuncProps) {
|
|
|
|
|
currentConfig = {
|
|
|
|
|
...currentConfig,
|
|
|
|
|
...newConfig,
|
|
|
|
|
};
|
|
|
|
|
render(currentConfig);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 21:51:19 +08:00
|
|
|
|
render(currentConfig);
|
2016-06-21 18:50:07 +08:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
destroy: close,
|
2018-08-25 21:51:19 +08:00
|
|
|
|
update,
|
2016-06-21 18:50:07 +08:00
|
|
|
|
};
|
2015-07-23 22:23:11 +08:00
|
|
|
|
}
|