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';
|
2023-02-22 18:18:26 +08:00
|
|
|
|
import useLocale from '../locale/useLocale';
|
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,
|
2022-12-07 21:33:44 +08:00
|
|
|
|
footer,
|
2022-07-15 20:51:07 +08:00
|
|
|
|
// 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';
|
|
|
|
|
|
2023-02-24 10:51:59 +08:00
|
|
|
|
const [locale] = useLocale('Modal');
|
2023-02-22 18:18:26 +08:00
|
|
|
|
|
|
|
|
|
const mergedLocale = staticLocale || locale;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
|
2023-02-22 18:18:26 +08:00
|
|
|
|
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>
|
|
|
|
|
{footer !== undefined ? (
|
|
|
|
|
footer
|
|
|
|
|
) : (
|
|
|
|
|
<div className={`${confirmPrefixCls}-btns`}>
|
|
|
|
|
{cancelButton}
|
2022-07-15 20:51:07 +08:00
|
|
|
|
<ActionButton
|
2023-02-22 18:18:26 +08:00
|
|
|
|
type={okType}
|
|
|
|
|
actionFn={onOk}
|
2022-07-15 20:51:07 +08:00
|
|
|
|
close={close}
|
2023-02-22 18:18:26 +08:00
|
|
|
|
autoFocus={autoFocusButton === 'ok'}
|
|
|
|
|
buttonProps={okButtonProps}
|
2022-07-15 20:51:07 +08:00
|
|
|
|
prefixCls={`${rootPrefixCls}-btn`}
|
|
|
|
|
>
|
2023-02-22 18:18:26 +08:00
|
|
|
|
{okText || (mergedOkCancel ? mergedLocale?.okText : mergedLocale?.justOkText)}
|
2022-07-15 20:51:07 +08:00
|
|
|
|
</ActionButton>
|
2023-02-22 18:18:26 +08:00
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2022-07-15 20:51:07 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 18:18:26 +08:00
|
|
|
|
const ConfirmDialog: React.FC<ConfirmDialogProps> = (props) => {
|
2022-07-15 20:51:07 +08:00
|
|
|
|
const {
|
2020-02-03 13:00:35 +08:00
|
|
|
|
close,
|
|
|
|
|
zIndex,
|
|
|
|
|
afterClose,
|
|
|
|
|
visible,
|
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-09-18 11:22:05 +08:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
warning(
|
|
|
|
|
visible === undefined,
|
|
|
|
|
'Modal',
|
|
|
|
|
`\`visible\` is deprecated, please use \`open\` instead.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
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;
|