2023-08-28 11:54:43 +08:00
|
|
|
|
import * as React from 'react';
|
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';
|
2023-08-28 11:54:43 +08:00
|
|
|
|
|
2023-11-17 10:54:19 +08:00
|
|
|
|
import { CONTAINER_MAX_OFFSET } from '../_util/hooks/useZIndex';
|
2021-02-08 17:09:13 +08:00
|
|
|
|
import { getTransitionName } from '../_util/motion';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-05-18 23:53:34 +08:00
|
|
|
|
import type { ThemeConfig } from '../config-provider';
|
|
|
|
|
import ConfigProvider from '../config-provider';
|
|
|
|
|
import { useLocale } from '../locale';
|
2023-11-17 10:54:19 +08:00
|
|
|
|
import useToken from '../theme/useToken';
|
2023-08-28 11:54:43 +08:00
|
|
|
|
import CancelBtn from './components/ConfirmCancelBtn';
|
|
|
|
|
import OkBtn from './components/ConfirmOkBtn';
|
|
|
|
|
import type { ModalContextProps } from './context';
|
|
|
|
|
import { ModalContextProvider } from './context';
|
2023-06-05 09:57:26 +08:00
|
|
|
|
import type { ModalFuncProps, ModalLocale } from './interface';
|
2023-11-22 18:02:32 +08:00
|
|
|
|
import Modal from './Modal';
|
2023-12-29 22:20:28 +08:00
|
|
|
|
import Confirm from './style/confirm';
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
2023-08-28 11:54:43 +08:00
|
|
|
|
export interface ConfirmDialogProps extends ModalFuncProps {
|
2023-08-31 22:21:32 +08:00
|
|
|
|
prefixCls: string;
|
2020-02-03 13:00:35 +08:00
|
|
|
|
afterClose?: () => void;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
close?: (...args: any[]) => void;
|
2023-07-11 09:58:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* `close` prop support `...args` that pass to the developer
|
|
|
|
|
* that we can not break this.
|
|
|
|
|
* Provider `onClose` for internal usage
|
|
|
|
|
*/
|
|
|
|
|
onConfirm?: (confirmed: boolean) => void;
|
2020-02-03 13:00:35 +08:00
|
|
|
|
autoFocusButton?: null | 'ok' | 'cancel';
|
2023-08-28 11:54:43 +08:00
|
|
|
|
rootPrefixCls?: string;
|
2021-06-09 15:36:59 +08:00
|
|
|
|
iconPrefixCls?: string;
|
2023-11-17 10:54:19 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only passed by static method
|
|
|
|
|
*/
|
2023-05-18 23:53:34 +08:00
|
|
|
|
theme?: ThemeConfig;
|
2022-07-15 20:51:07 +08:00
|
|
|
|
|
|
|
|
|
/** @private Internal Usage. Do not override this */
|
|
|
|
|
locale?: ModalLocale;
|
2023-07-11 09:58:25 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do not throw if is await mode
|
|
|
|
|
*/
|
|
|
|
|
isSilent?: () => boolean;
|
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 {
|
2023-08-31 22:21:32 +08:00
|
|
|
|
prefixCls,
|
2020-02-03 13:00:35 +08:00
|
|
|
|
icon,
|
2022-07-15 20:51:07 +08:00
|
|
|
|
okText,
|
|
|
|
|
cancelText,
|
|
|
|
|
confirmPrefixCls,
|
|
|
|
|
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,
|
2023-08-28 11:54:43 +08:00
|
|
|
|
...resetProps
|
2022-07-15 20:51:07 +08:00
|
|
|
|
} = props;
|
|
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
|
const warning = devUseWarning('Modal');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
|
|
warning(
|
|
|
|
|
!(typeof icon === 'string' && icon.length > 2),
|
|
|
|
|
'breaking',
|
|
|
|
|
`\`icon\` is using ReactNode instead of string naming in v4. Please check \`${icon}\` at https://ant.design/components/icon`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-07-15 20:51:07 +08:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认为 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-08-28 11:54:43 +08:00
|
|
|
|
// ================== Locale Text ==================
|
|
|
|
|
const okTextLocale = okText || (mergedOkCancel ? mergedLocale?.okText : mergedLocale?.justOkText);
|
|
|
|
|
const cancelTextLocale = cancelText || mergedLocale?.cancelText;
|
|
|
|
|
|
|
|
|
|
// ================= Context Value =================
|
|
|
|
|
const btnCtxValue: ModalContextProps = {
|
|
|
|
|
autoFocusButton,
|
|
|
|
|
cancelTextLocale,
|
|
|
|
|
okTextLocale,
|
|
|
|
|
mergedOkCancel,
|
|
|
|
|
...resetProps,
|
|
|
|
|
};
|
|
|
|
|
const btnCtxValueMemo = React.useMemo(() => btnCtxValue, [...Object.values(btnCtxValue)]);
|
|
|
|
|
|
|
|
|
|
// ====================== Footer Origin Node ======================
|
|
|
|
|
const footerOriginNode = (
|
|
|
|
|
<>
|
|
|
|
|
<CancelBtn />
|
|
|
|
|
<OkBtn />
|
|
|
|
|
</>
|
2023-02-22 18:18:26 +08:00
|
|
|
|
);
|
|
|
|
|
|
2023-08-31 22:21:32 +08:00
|
|
|
|
const hasTitle = props.title !== undefined && props.title !== null;
|
|
|
|
|
|
|
|
|
|
const bodyCls = `${confirmPrefixCls}-body`;
|
|
|
|
|
|
2023-02-22 18:18:26 +08:00
|
|
|
|
return (
|
|
|
|
|
<div className={`${confirmPrefixCls}-body-wrapper`}>
|
2023-08-31 22:21:32 +08:00
|
|
|
|
<div
|
|
|
|
|
className={classNames(bodyCls, {
|
|
|
|
|
[`${bodyCls}-has-title`]: hasTitle,
|
|
|
|
|
})}
|
|
|
|
|
>
|
2023-02-22 18:18:26 +08:00
|
|
|
|
{mergedIcon}
|
2023-08-31 22:21:32 +08:00
|
|
|
|
<div className={`${confirmPrefixCls}-paragraph`}>
|
|
|
|
|
{hasTitle && <span className={`${confirmPrefixCls}-title`}>{props.title}</span>}
|
|
|
|
|
<div className={`${confirmPrefixCls}-content`}>{props.content}</div>
|
|
|
|
|
</div>
|
2023-02-22 18:18:26 +08:00
|
|
|
|
</div>
|
2023-08-28 11:54:43 +08:00
|
|
|
|
|
|
|
|
|
{footer === undefined || typeof footer === 'function' ? (
|
|
|
|
|
<ModalContextProvider value={btnCtxValueMemo}>
|
|
|
|
|
<div className={`${confirmPrefixCls}-btns`}>
|
|
|
|
|
{typeof footer === 'function'
|
|
|
|
|
? footer(footerOriginNode, {
|
|
|
|
|
OkBtn,
|
|
|
|
|
CancelBtn,
|
|
|
|
|
})
|
|
|
|
|
: footerOriginNode}
|
|
|
|
|
</div>
|
|
|
|
|
</ModalContextProvider>
|
2023-03-13 19:55:59 +08:00
|
|
|
|
) : (
|
|
|
|
|
footer
|
2023-02-22 18:18:26 +08:00
|
|
|
|
)}
|
2023-08-31 22:21:32 +08:00
|
|
|
|
|
2023-12-29 22:20:28 +08:00
|
|
|
|
<Confirm prefixCls={prefixCls} />
|
2023-02-22 18:18:26 +08:00
|
|
|
|
</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,
|
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,
|
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,
|
2023-09-05 21:36:21 +08:00
|
|
|
|
onConfirm,
|
2023-11-02 10:12:41 +08:00
|
|
|
|
styles,
|
2020-02-03 13:00:35 +08:00
|
|
|
|
} = props;
|
|
|
|
|
|
2022-09-18 11:22:05 +08:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
|
const warning = devUseWarning('Modal');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
2023-09-25 14:27:41 +08:00
|
|
|
|
[
|
|
|
|
|
['visible', 'open'],
|
|
|
|
|
['bodyStyle', 'styles.body'],
|
|
|
|
|
['maskStyle', 'styles.mask'],
|
|
|
|
|
].forEach(([deprecatedName, newName]) => {
|
|
|
|
|
warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
|
|
|
|
|
});
|
2022-09-18 11:22:05 +08:00
|
|
|
|
}
|
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,
|
|
|
|
|
);
|
|
|
|
|
|
2023-11-17 10:54:19 +08:00
|
|
|
|
// ========================= zIndex =========================
|
|
|
|
|
const [, token] = useToken();
|
|
|
|
|
|
|
|
|
|
const mergedZIndex = React.useMemo(() => {
|
|
|
|
|
if (zIndex !== undefined) {
|
|
|
|
|
return zIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Static always use max zIndex
|
|
|
|
|
return token.zIndexPopupBase + CONTAINER_MAX_OFFSET;
|
|
|
|
|
}, [zIndex, token]);
|
|
|
|
|
|
|
|
|
|
// ========================= Render =========================
|
|
|
|
|
return (
|
2023-11-22 18:02:32 +08:00
|
|
|
|
<Modal
|
2023-11-17 10:54:19 +08:00
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
className={classString}
|
|
|
|
|
wrapClassName={classNames(
|
|
|
|
|
{ [`${confirmPrefixCls}-centered`]: !!props.centered },
|
|
|
|
|
wrapClassName,
|
|
|
|
|
)}
|
|
|
|
|
onCancel={() => {
|
|
|
|
|
close?.({ triggerCancel: true });
|
|
|
|
|
onConfirm?.(false);
|
|
|
|
|
}}
|
|
|
|
|
open={open}
|
|
|
|
|
title=""
|
|
|
|
|
footer={null}
|
|
|
|
|
transitionName={getTransitionName(rootPrefixCls || '', 'zoom', props.transitionName)}
|
|
|
|
|
maskTransitionName={getTransitionName(rootPrefixCls || '', 'fade', props.maskTransitionName)}
|
|
|
|
|
mask={mask}
|
|
|
|
|
maskClosable={maskClosable}
|
|
|
|
|
style={style}
|
2024-01-30 14:42:46 +08:00
|
|
|
|
styles={{ body: bodyStyle, mask: maskStyle, ...styles }}
|
2023-11-17 10:54:19 +08:00
|
|
|
|
width={width}
|
|
|
|
|
zIndex={mergedZIndex}
|
|
|
|
|
afterClose={afterClose}
|
|
|
|
|
keyboard={keyboard}
|
|
|
|
|
centered={centered}
|
|
|
|
|
getContainer={getContainer}
|
|
|
|
|
closable={closable}
|
|
|
|
|
closeIcon={closeIcon}
|
|
|
|
|
modalRender={modalRender}
|
|
|
|
|
focusTriggerAfterClose={focusTriggerAfterClose}
|
|
|
|
|
>
|
|
|
|
|
<ConfirmContent {...props} confirmPrefixCls={confirmPrefixCls} />
|
2023-11-22 18:02:32 +08:00
|
|
|
|
</Modal>
|
2023-11-17 10:54:19 +08:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ConfirmDialogWrapper: React.FC<ConfirmDialogProps> = (props) => {
|
|
|
|
|
const { rootPrefixCls, iconPrefixCls, direction, theme } = props;
|
|
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
|
return (
|
2023-05-18 23:53:34 +08:00
|
|
|
|
<ConfigProvider
|
|
|
|
|
prefixCls={rootPrefixCls}
|
|
|
|
|
iconPrefixCls={iconPrefixCls}
|
|
|
|
|
direction={direction}
|
|
|
|
|
theme={theme}
|
|
|
|
|
>
|
2023-11-17 10:54:19 +08:00
|
|
|
|
<ConfirmDialog {...props} />
|
2021-06-09 15:36:59 +08:00
|
|
|
|
</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';
|
2023-11-17 10:54:19 +08:00
|
|
|
|
ConfirmDialogWrapper.displayName = 'ConfirmDialogWrapper';
|
2022-07-15 20:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 10:54:19 +08:00
|
|
|
|
export default ConfirmDialogWrapper;
|