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';
|
2018-12-03 17:08:48 +08:00
|
|
|
|
import Dialog, { ModalFuncProps, destroyFns } from './Modal';
|
2016-12-19 14:01:52 +08:00
|
|
|
|
import ActionButton from './ActionButton';
|
|
|
|
|
import { getConfirmLocale } from './locale';
|
2018-12-26 21:36:36 +08:00
|
|
|
|
import warning from '../_util/warning';
|
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-12-07 20:02:01 +08:00
|
|
|
|
const {
|
|
|
|
|
onCancel,
|
|
|
|
|
onOk,
|
|
|
|
|
close,
|
|
|
|
|
zIndex,
|
|
|
|
|
afterClose,
|
|
|
|
|
visible,
|
|
|
|
|
keyboard,
|
|
|
|
|
centered,
|
|
|
|
|
getContainer,
|
|
|
|
|
maskStyle,
|
|
|
|
|
okButtonProps,
|
|
|
|
|
cancelButtonProps,
|
2018-12-26 21:36:36 +08:00
|
|
|
|
iconType = 'question-circle',
|
2018-12-07 20:02:01 +08:00
|
|
|
|
} = props;
|
2018-12-26 21:36:36 +08:00
|
|
|
|
warning(
|
|
|
|
|
!('iconType' in props),
|
2019-02-27 15:32:29 +08:00
|
|
|
|
'Modal',
|
2018-12-26 21:36:36 +08:00
|
|
|
|
`The property 'iconType' is deprecated. Use the property 'icon' instead.`,
|
|
|
|
|
);
|
2019-03-12 09:45:55 +08:00
|
|
|
|
|
|
|
|
|
// 支持传入{ icon: null }来隐藏`Modal.confirm`默认的Icon
|
|
|
|
|
const icon = props.icon === undefined ? iconType : props.icon;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const okType = props.okType || 'primary';
|
2018-10-12 14:04:26 +08:00
|
|
|
|
const prefixCls = props.prefixCls || 'ant-modal';
|
|
|
|
|
const contentPrefixCls = `${prefixCls}-confirm`;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
// 默认为 true,保持向下兼容
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const okCancel = 'okCancel' in props ? props.okCancel! : true;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const width = props.width || 416;
|
|
|
|
|
const style = props.style || {};
|
2019-01-10 01:21:51 +08:00
|
|
|
|
const mask = props.mask === undefined ? true : props.mask;
|
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();
|
2018-12-07 20:02:01 +08:00
|
|
|
|
const okText = props.okText || (okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const cancelText = props.cancelText || runtimeLocale.cancelText;
|
2018-09-29 17:00:01 +08:00
|
|
|
|
const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok';
|
2019-01-11 09:43:44 +08:00
|
|
|
|
const transitionName = props.transitionName || 'zoom';
|
|
|
|
|
const maskTransitionName = props.maskTransitionName || 'fade';
|
2016-05-12 13:25:00 +08:00
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
const classString = classNames(
|
2018-10-12 14:04:26 +08:00
|
|
|
|
contentPrefixCls,
|
|
|
|
|
`${contentPrefixCls}-${props.type}`,
|
2017-12-02 22:10:27 +08:00
|
|
|
|
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-12-07 20:02:01 +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
|
|
|
|
|
2018-12-26 21:36:36 +08:00
|
|
|
|
const iconNode = typeof icon === 'string' ? <Icon type={icon} /> : icon;
|
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
return (
|
2016-05-05 16:52:36 +08:00
|
|
|
|
<Dialog
|
2018-10-12 14:04:26 +08:00
|
|
|
|
prefixCls={prefixCls}
|
2016-05-05 16:52:36 +08:00
|
|
|
|
className={classString}
|
2018-10-12 14:04:26 +08:00
|
|
|
|
wrapClassName={classNames({ [`${contentPrefixCls}-centered`]: !!props.centered })}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
onCancel={() => close({ triggerCancel: true })}
|
2017-12-02 22:10:27 +08:00
|
|
|
|
visible={visible}
|
2016-05-05 16:52:36 +08:00
|
|
|
|
title=""
|
2019-01-11 09:43:44 +08:00
|
|
|
|
transitionName={transitionName}
|
2016-05-05 16:52:36 +08:00
|
|
|
|
footer=""
|
2019-01-11 09:43:44 +08:00
|
|
|
|
maskTransitionName={maskTransitionName}
|
2019-01-10 01:21:51 +08:00
|
|
|
|
mask={mask}
|
2017-01-07 15:17:27 +08:00
|
|
|
|
maskClosable={maskClosable}
|
2018-10-27 18:54:52 +08:00
|
|
|
|
maskStyle={maskStyle}
|
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
|
|
|
|
>
|
2018-10-12 14:04:26 +08:00
|
|
|
|
<div className={`${contentPrefixCls}-body-wrapper`}>
|
|
|
|
|
<div className={`${contentPrefixCls}-body`}>
|
2018-12-26 21:36:36 +08:00
|
|
|
|
{iconNode}
|
2019-10-26 10:31:58 +08:00
|
|
|
|
{props.title === undefined ? null : (
|
|
|
|
|
<span className={`${contentPrefixCls}-title`}>{props.title}</span>
|
|
|
|
|
)}
|
2018-10-12 14:04:26 +08:00
|
|
|
|
<div className={`${contentPrefixCls}-content`}>{props.content}</div>
|
2017-12-02 22:10:27 +08:00
|
|
|
|
</div>
|
2018-10-12 14:04:26 +08:00
|
|
|
|
<div className={`${contentPrefixCls}-btns`}>
|
2017-12-02 22:10:27 +08:00
|
|
|
|
{cancelButton}
|
2018-12-07 20:02:01 +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);
|
2019-08-06 15:02:05 +08:00
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
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 destroy(...args: any[]) {
|
|
|
|
|
const unmountResult = ReactDOM.unmountComponentAtNode(div);
|
|
|
|
|
if (unmountResult && div.parentNode) {
|
|
|
|
|
div.parentNode.removeChild(div);
|
|
|
|
|
}
|
2018-12-12 03:51:31 +08:00
|
|
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
if (config.onCancel && triggerCancel) {
|
|
|
|
|
config.onCancel(...args);
|
|
|
|
|
}
|
2018-12-20 17:43:22 +08:00
|
|
|
|
for (let i = 0; i < destroyFns.length; i++) {
|
2018-12-26 21:36:36 +08:00
|
|
|
|
const fn = destroyFns[i];
|
2019-08-06 15:09:07 +08:00
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
2019-01-30 10:46:30 +08:00
|
|
|
|
if (fn === close) {
|
2018-12-26 21:36:36 +08:00
|
|
|
|
destroyFns.splice(i, 1);
|
|
|
|
|
break;
|
2018-12-03 17:08:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function render(props: any) {
|
2019-08-09 14:38:54 +08:00
|
|
|
|
ReactDOM.render(<ConfirmDialog getContainer={false} {...props} />, div);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function close(...args: any[]) {
|
|
|
|
|
currentConfig = {
|
|
|
|
|
...currentConfig,
|
|
|
|
|
visible: false,
|
|
|
|
|
afterClose: destroy.bind(this, ...args),
|
|
|
|
|
};
|
|
|
|
|
if (IS_REACT_16) {
|
|
|
|
|
render(currentConfig);
|
|
|
|
|
} else {
|
|
|
|
|
destroy(...args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function update(newConfig: ModalFuncProps) {
|
|
|
|
|
currentConfig = {
|
|
|
|
|
...currentConfig,
|
|
|
|
|
...newConfig,
|
|
|
|
|
};
|
|
|
|
|
render(currentConfig);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 21:51:19 +08:00
|
|
|
|
render(currentConfig);
|
2016-06-21 18:50:07 +08:00
|
|
|
|
|
2018-12-26 21:36:36 +08:00
|
|
|
|
destroyFns.push(close);
|
2018-12-03 17:08:48 +08:00
|
|
|
|
|
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
|
|
|
|
}
|