2022-09-08 09:51:32 +08:00
|
|
|
|
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
|
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
|
|
|
|
|
import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
|
2020-02-03 13:00:35 +08:00
|
|
|
|
import classNames from 'classnames';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import * as React from 'react';
|
2020-10-28 12:07:46 +08:00
|
|
|
|
import ConfigProvider from '../config-provider';
|
2022-07-15 20:51:07 +08:00
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import ActionButton from '../_util/ActionButton';
|
2021-02-08 17:09:13 +08:00
|
|
|
|
import { getTransitionName } from '../_util/motion';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import warning from '../_util/warning';
|
2022-07-15 20:51:07 +08:00
|
|
|
|
import type { ModalFuncProps, ModalLocale } from './Modal';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import Dialog from './Modal';
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
|
|
interface ConfirmDialogProps extends ModalFuncProps {
|
|
|
|
|
afterClose?: () => void;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
close?: (...args: any[]) => void;
|
2020-02-03 13:00:35 +08:00
|
|
|
|
autoFocusButton?: null | 'ok' | 'cancel';
|
2021-02-08 17:09:13 +08:00
|
|
|
|
rootPrefixCls: string;
|
2021-06-09 15:36:59 +08:00
|
|
|
|
iconPrefixCls?: string;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
|
|
|
|
|
/** @private Internal Usage. Do not override this */
|
|
|
|
|
locale?: ModalLocale;
|
2020-02-03 13:00:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 20:51:07 +08:00
|
|
|
|
export function ConfirmContent(
|
|
|
|
|
props: ConfirmDialogProps & {
|
|
|
|
|
confirmPrefixCls: string;
|
|
|
|
|
},
|
|
|
|
|
) {
|
2020-02-03 13:00:35 +08:00
|
|
|
|
const {
|
|
|
|
|
icon,
|
|
|
|
|
onCancel,
|
|
|
|
|
onOk,
|
2022-07-15 20:51:07 +08:00
|
|
|
|
close,
|
|
|
|
|
okText,
|
|
|
|
|
okButtonProps,
|
|
|
|
|
cancelText,
|
|
|
|
|
cancelButtonProps,
|
|
|
|
|
confirmPrefixCls,
|
|
|
|
|
rootPrefixCls,
|
|
|
|
|
type,
|
|
|
|
|
okCancel,
|
|
|
|
|
|
|
|
|
|
// Legacy for static function usage
|
|
|
|
|
locale: staticLocale,
|
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
|
|
warning(
|
|
|
|
|
!(typeof icon === 'string' && icon.length > 2),
|
|
|
|
|
'Modal',
|
|
|
|
|
`\`icon\` is using ReactNode instead of string naming in v4. Please check \`${icon}\` at https://ant.design/components/icon`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Icon
|
|
|
|
|
let mergedIcon: React.ReactNode = icon;
|
2022-09-13 18:09:58 +08:00
|
|
|
|
|
|
|
|
|
// 支持传入{ icon: null }来隐藏`Modal.confirm`默认的Icon
|
|
|
|
|
if (!icon && icon !== null) {
|
2022-07-15 20:51:07 +08:00
|
|
|
|
switch (type) {
|
|
|
|
|
case 'info':
|
2022-09-08 09:51:32 +08:00
|
|
|
|
mergedIcon = <InfoCircleFilled />;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'success':
|
2022-09-08 09:51:32 +08:00
|
|
|
|
mergedIcon = <CheckCircleFilled />;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'error':
|
2022-09-08 09:51:32 +08:00
|
|
|
|
mergedIcon = <CloseCircleFilled />;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2022-09-08 09:51:32 +08:00
|
|
|
|
mergedIcon = <ExclamationCircleFilled />;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const okType = props.okType || 'primary';
|
|
|
|
|
// 默认为 true,保持向下兼容
|
|
|
|
|
const mergedOkCancel = okCancel ?? type === 'confirm';
|
|
|
|
|
|
|
|
|
|
const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok';
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<LocaleReceiver componentName="Modal">
|
|
|
|
|
{locale => {
|
|
|
|
|
const mergedLocale = staticLocale || locale;
|
|
|
|
|
|
|
|
|
|
const cancelButton = mergedOkCancel && (
|
|
|
|
|
<ActionButton
|
|
|
|
|
actionFn={onCancel}
|
|
|
|
|
close={close}
|
|
|
|
|
autoFocus={autoFocusButton === 'cancel'}
|
|
|
|
|
buttonProps={cancelButtonProps}
|
|
|
|
|
prefixCls={`${rootPrefixCls}-btn`}
|
|
|
|
|
>
|
|
|
|
|
{cancelText || mergedLocale?.cancelText}
|
|
|
|
|
</ActionButton>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={`${confirmPrefixCls}-body-wrapper`}>
|
|
|
|
|
<div className={`${confirmPrefixCls}-body`}>
|
|
|
|
|
{mergedIcon}
|
|
|
|
|
{props.title === undefined ? null : (
|
|
|
|
|
<span className={`${confirmPrefixCls}-title`}>{props.title}</span>
|
|
|
|
|
)}
|
|
|
|
|
<div className={`${confirmPrefixCls}-content`}>{props.content}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`${confirmPrefixCls}-btns`}>
|
|
|
|
|
{cancelButton}
|
|
|
|
|
<ActionButton
|
|
|
|
|
type={okType}
|
|
|
|
|
actionFn={onOk}
|
|
|
|
|
close={close}
|
|
|
|
|
autoFocus={autoFocusButton === 'ok'}
|
|
|
|
|
buttonProps={okButtonProps}
|
|
|
|
|
prefixCls={`${rootPrefixCls}-btn`}
|
|
|
|
|
>
|
|
|
|
|
{okText || (mergedOkCancel ? mergedLocale?.okText : mergedLocale?.justOkText)}
|
|
|
|
|
</ActionButton>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
</LocaleReceiver>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ConfirmDialog = (props: ConfirmDialogProps) => {
|
|
|
|
|
const {
|
2020-02-03 13:00:35 +08:00
|
|
|
|
close,
|
|
|
|
|
zIndex,
|
|
|
|
|
afterClose,
|
2022-08-23 16:55:57 +08:00
|
|
|
|
open,
|
2020-02-03 13:00:35 +08:00
|
|
|
|
keyboard,
|
|
|
|
|
centered,
|
|
|
|
|
getContainer,
|
|
|
|
|
maskStyle,
|
2020-06-03 17:20:04 +08:00
|
|
|
|
direction,
|
2020-07-14 11:14:46 +08:00
|
|
|
|
prefixCls,
|
2021-10-28 18:41:30 +08:00
|
|
|
|
wrapClassName,
|
2020-07-14 18:43:55 +08:00
|
|
|
|
rootPrefixCls,
|
2021-06-09 15:36:59 +08:00
|
|
|
|
iconPrefixCls,
|
2020-10-22 11:24:49 +08:00
|
|
|
|
bodyStyle,
|
2020-11-21 21:13:28 +08:00
|
|
|
|
closable = false,
|
|
|
|
|
closeIcon,
|
2020-10-30 19:52:12 +08:00
|
|
|
|
modalRender,
|
2020-11-30 16:57:33 +08:00
|
|
|
|
focusTriggerAfterClose,
|
2020-02-03 13:00:35 +08:00
|
|
|
|
} = props;
|
|
|
|
|
|
2022-07-15 20:51:07 +08:00
|
|
|
|
const confirmPrefixCls = `${prefixCls}-confirm`;
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
|
|
const width = props.width || 416;
|
|
|
|
|
const style = props.style || {};
|
|
|
|
|
const mask = props.mask === undefined ? true : props.mask;
|
|
|
|
|
// 默认为 false,保持旧版默认行为
|
|
|
|
|
const maskClosable = props.maskClosable === undefined ? false : props.maskClosable;
|
|
|
|
|
|
|
|
|
|
const classString = classNames(
|
2022-07-15 20:51:07 +08:00
|
|
|
|
confirmPrefixCls,
|
|
|
|
|
`${confirmPrefixCls}-${props.type}`,
|
|
|
|
|
{ [`${confirmPrefixCls}-rtl`]: direction === 'rtl' },
|
2020-02-03 13:00:35 +08:00
|
|
|
|
props.className,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
2021-08-23 17:48:23 +08:00
|
|
|
|
<ConfigProvider prefixCls={rootPrefixCls} iconPrefixCls={iconPrefixCls} direction={direction}>
|
2021-06-09 15:36:59 +08:00
|
|
|
|
<Dialog
|
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
className={classString}
|
2021-10-28 18:41:30 +08:00
|
|
|
|
wrapClassName={classNames(
|
2022-07-15 20:51:07 +08:00
|
|
|
|
{ [`${confirmPrefixCls}-centered`]: !!props.centered },
|
2021-10-28 18:41:30 +08:00
|
|
|
|
wrapClassName,
|
|
|
|
|
)}
|
2022-07-15 20:51:07 +08:00
|
|
|
|
onCancel={() => close?.({ triggerCancel: true })}
|
2022-08-23 17:48:00 +08:00
|
|
|
|
open={open}
|
2021-06-09 15:36:59 +08:00
|
|
|
|
title=""
|
|
|
|
|
footer=""
|
|
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'zoom', props.transitionName)}
|
|
|
|
|
maskTransitionName={getTransitionName(rootPrefixCls, 'fade', props.maskTransitionName)}
|
|
|
|
|
mask={mask}
|
|
|
|
|
maskClosable={maskClosable}
|
|
|
|
|
maskStyle={maskStyle}
|
|
|
|
|
style={style}
|
2021-11-18 18:29:29 +08:00
|
|
|
|
bodyStyle={bodyStyle}
|
2021-06-09 15:36:59 +08:00
|
|
|
|
width={width}
|
|
|
|
|
zIndex={zIndex}
|
|
|
|
|
afterClose={afterClose}
|
|
|
|
|
keyboard={keyboard}
|
|
|
|
|
centered={centered}
|
|
|
|
|
getContainer={getContainer}
|
|
|
|
|
closable={closable}
|
|
|
|
|
closeIcon={closeIcon}
|
|
|
|
|
modalRender={modalRender}
|
|
|
|
|
focusTriggerAfterClose={focusTriggerAfterClose}
|
|
|
|
|
>
|
2022-07-15 20:51:07 +08:00
|
|
|
|
<ConfirmContent {...props} confirmPrefixCls={confirmPrefixCls} />
|
2021-06-09 15:36:59 +08:00
|
|
|
|
</Dialog>
|
|
|
|
|
</ConfigProvider>
|
2020-02-03 13:00:35 +08:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-15 20:51:07 +08:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
ConfirmDialog.displayName = 'ConfirmDialog';
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
|
export default ConfirmDialog;
|