ant-design-vue/components/modal/ConfirmDialog.tsx

117 lines
3.4 KiB
Vue
Raw Normal View History

2020-08-31 16:53:19 +08:00
import classNames from '../_util/classNames';
2020-10-19 23:09:36 +08:00
import Dialog, { ModalFuncProps } from './Modal';
2019-01-12 11:33:27 +08:00
import ActionButton from './ActionButton';
import { getConfirmLocale } from './locale';
2020-10-19 23:09:36 +08:00
import { FunctionalComponent } from 'vue';
2018-03-06 19:14:41 +08:00
2020-10-19 23:09:36 +08:00
interface ConfirmDialogProps extends ModalFuncProps {
afterClose?: () => void;
close: (...args: any[]) => void;
autoFocusButton?: null | 'ok' | 'cancel';
}
const ConfirmDialog: FunctionalComponent<ConfirmDialogProps> = props => {
2020-06-15 23:47:49 +08:00
const {
icon,
onCancel,
onOk,
close,
closable = false,
2020-06-15 23:47:49 +08:00
zIndex,
afterClose,
visible,
keyboard,
centered,
getContainer,
maskStyle,
okButtonProps,
cancelButtonProps,
2020-10-19 23:09:36 +08:00
// closable = false,
} = props;
const okType = props.okType || 'primary';
const prefixCls = props.prefixCls || 'ant-modal';
2020-06-15 23:47:49 +08:00
const contentPrefixCls = `${prefixCls}-confirm`;
// 默认为 true保持向下兼容
2020-10-19 23:09:36 +08:00
const okCancel = 'okCancel' in props ? props.okCancel : true;
const width = props.width || 416;
const style = props.style || {};
const mask = props.mask === undefined ? true : props.mask;
2020-06-15 23:47:49 +08:00
// 默认为 false保持旧版默认行为
2020-10-19 23:09:36 +08:00
const maskClosable = props.maskClosable === undefined ? false : props.maskClosable;
2020-06-15 23:47:49 +08:00
const runtimeLocale = getConfirmLocale();
2020-10-19 23:09:36 +08:00
const okText = props.okText || (okCancel ? runtimeLocale.okText : runtimeLocale.justOkText);
const cancelText = props.cancelText || runtimeLocale.cancelText;
const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok';
const transitionName = props.transitionName || 'zoom';
const maskTransitionName = props.maskTransitionName || 'fade';
2018-03-06 19:14:41 +08:00
2020-06-15 23:47:49 +08:00
const classString = classNames(
contentPrefixCls,
2020-10-19 23:09:36 +08:00
`${contentPrefixCls}-${props.type}`,
`${prefixCls}-${props.type}`,
props.class,
2020-06-15 23:47:49 +08:00
);
2018-03-06 19:14:41 +08:00
2020-06-15 23:47:49 +08:00
const cancelButton = okCancel && (
<ActionButton
actionFn={onCancel}
closeModal={close}
2020-07-17 17:36:58 +08:00
autofocus={autoFocusButton === 'cancel'}
2020-06-15 23:47:49 +08:00
buttonProps={cancelButtonProps}
>
{cancelText}
</ActionButton>
);
2018-03-06 19:14:41 +08:00
2020-06-15 23:47:49 +08:00
return (
<Dialog
prefixCls={prefixCls}
class={classString}
wrapClassName={classNames({ [`${contentPrefixCls}-centered`]: !!centered })}
onCancel={e => close({ triggerCancel: true }, e)}
visible={visible}
title=""
transitionName={transitionName}
footer=""
maskTransitionName={maskTransitionName}
mask={mask}
maskClosable={maskClosable}
maskStyle={maskStyle}
style={style}
width={width}
zIndex={zIndex}
afterClose={afterClose}
keyboard={keyboard}
centered={centered}
getContainer={getContainer}
closable={closable}
2020-06-15 23:47:49 +08:00
>
<div class={`${contentPrefixCls}-body-wrapper`}>
<div class={`${contentPrefixCls}-body`}>
2020-10-19 23:09:36 +08:00
{icon}
{props.title === undefined ? null : (
<span class={`${contentPrefixCls}-title`}>{props.title}</span>
2020-06-15 23:47:49 +08:00
)}
2020-10-19 23:09:36 +08:00
<div class={`${contentPrefixCls}-content`}>{props.content}</div>
2018-03-06 19:14:41 +08:00
</div>
2020-06-15 23:47:49 +08:00
<div class={`${contentPrefixCls}-btns`}>
{cancelButton}
<ActionButton
type={okType}
actionFn={onOk}
closeModal={close}
2020-07-17 17:36:58 +08:00
autofocus={autoFocusButton === 'ok'}
2020-06-15 23:47:49 +08:00
buttonProps={okButtonProps}
>
{okText}
</ActionButton>
</div>
</div>
</Dialog>
);
2019-01-12 11:33:27 +08:00
};
2020-10-19 23:09:36 +08:00
2020-06-15 23:47:49 +08:00
ConfirmDialog.inheritAttrs = false;
2020-10-19 23:09:36 +08:00
2020-06-15 23:47:49 +08:00
export default ConfirmDialog;