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

115 lines
3.4 KiB
TypeScript
Raw Normal View History

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