2022-06-06 23:39:00 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import Dialog from 'rc-dialog';
|
|
|
|
import * as React from 'react';
|
|
|
|
import type { ButtonProps, LegacyButtonType } from '../button/button';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { DirectionType } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-06 23:39:00 +08:00
|
|
|
import { NoFormStyle } from '../form/context';
|
2022-11-22 22:52:40 +08:00
|
|
|
import { NoCompactStyle } from '../space/Compact';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
2022-06-06 23:39:00 +08:00
|
|
|
import { canUseDocElement } from '../_util/styleChecker';
|
2022-08-23 16:55:57 +08:00
|
|
|
import warning from '../_util/warning';
|
2023-02-22 18:18:26 +08:00
|
|
|
import { Footer, renderCloseIcon } from './PurePanel';
|
2022-04-26 22:45:39 +08:00
|
|
|
import useStyle from './style';
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2022-11-15 22:20:02 +08:00
|
|
|
type MousePosition = { x: number; y: number } | null;
|
|
|
|
|
|
|
|
let mousePosition: MousePosition;
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2019-04-18 23:20:13 +08:00
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/15795
|
|
|
|
const getClickPosition = (e: MouseEvent) => {
|
|
|
|
mousePosition = {
|
|
|
|
x: e.pageX,
|
|
|
|
y: e.pageY,
|
|
|
|
};
|
|
|
|
// 100ms 内发生过点击事件,则从点击位置动画展示
|
|
|
|
// 否则直接 zoom 展示
|
|
|
|
// 这样可以兼容非点击方式展开
|
2020-06-08 18:01:50 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
mousePosition = null;
|
|
|
|
}, 100);
|
2019-04-18 23:20:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 只有点击事件支持从鼠标位置动画展开
|
2021-01-27 14:03:05 +08:00
|
|
|
if (canUseDocElement()) {
|
2020-11-27 15:53:51 +08:00
|
|
|
document.documentElement.addEventListener('click', getClickPosition, true);
|
2019-04-18 23:20:13 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 12:00:05 +08:00
|
|
|
export interface ModalProps {
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Whether the modal dialog is visible or not */
|
2022-08-23 16:55:57 +08:00
|
|
|
open?: boolean;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Whether to apply loading visual effect for OK button or not */
|
2016-08-15 12:00:05 +08:00
|
|
|
confirmLoading?: boolean;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** The modal dialog's title */
|
2021-11-18 14:39:22 +08:00
|
|
|
title?: React.ReactNode;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Whether a close (x) button is visible on top right of the modal dialog or not */
|
2016-08-15 12:00:05 +08:00
|
|
|
closable?: boolean;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Specify a function that will be called when a user clicks the OK button */
|
2022-12-15 17:48:29 +08:00
|
|
|
onOk?: (e: React.MouseEvent<HTMLButtonElement>) => void;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Specify a function that will be called when a user clicks mask, close button on top right or Cancel button */
|
2022-12-15 17:48:29 +08:00
|
|
|
onCancel?: (e: React.MouseEvent<HTMLButtonElement>) => void;
|
2017-01-05 12:08:43 +08:00
|
|
|
afterClose?: () => void;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Callback when the animation ends when Modal is turned on and off */
|
|
|
|
afterOpenChange?: (open: boolean) => void;
|
|
|
|
/** Centered Modal */
|
2018-07-31 17:05:07 +08:00
|
|
|
centered?: boolean;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Width of the modal dialog */
|
2016-08-15 12:00:05 +08:00
|
|
|
width?: string | number;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Footer content */
|
2016-08-15 12:00:05 +08:00
|
|
|
footer?: React.ReactNode;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Text of the OK button */
|
2019-02-01 00:12:06 +08:00
|
|
|
okText?: React.ReactNode;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Button `type` of the OK button */
|
2020-05-05 12:03:16 +08:00
|
|
|
okType?: LegacyButtonType;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Text of the Cancel button */
|
2019-02-01 00:12:06 +08:00
|
|
|
cancelText?: React.ReactNode;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Whether to close the modal dialog when the mask (area outside the modal) is clicked */
|
2016-08-15 12:00:05 +08:00
|
|
|
maskClosable?: boolean;
|
2023-04-12 10:11:31 +08:00
|
|
|
/** Force render Modal */
|
2019-01-12 13:14:06 +08:00
|
|
|
forceRender?: boolean;
|
2020-04-06 13:00:41 +08:00
|
|
|
okButtonProps?: ButtonProps;
|
|
|
|
cancelButtonProps?: ButtonProps;
|
2017-12-28 15:13:06 +08:00
|
|
|
destroyOnClose?: boolean;
|
2016-08-15 12:00:05 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
wrapClassName?: string;
|
|
|
|
maskTransitionName?: string;
|
|
|
|
transitionName?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2021-07-26 16:47:55 +08:00
|
|
|
getContainer?: string | HTMLElement | getContainerFunc | false;
|
2017-09-15 21:41:32 +08:00
|
|
|
zIndex?: number;
|
2017-12-01 10:29:48 +08:00
|
|
|
bodyStyle?: React.CSSProperties;
|
|
|
|
maskStyle?: React.CSSProperties;
|
|
|
|
mask?: boolean;
|
2018-02-11 13:44:59 +08:00
|
|
|
keyboard?: boolean;
|
2019-06-24 11:29:58 +08:00
|
|
|
wrapProps?: any;
|
2018-08-02 18:02:43 +08:00
|
|
|
prefixCls?: string;
|
2019-08-17 20:55:59 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2020-10-30 19:52:12 +08:00
|
|
|
modalRender?: (node: React.ReactNode) => React.ReactNode;
|
2020-11-30 16:57:33 +08:00
|
|
|
focusTriggerAfterClose?: boolean;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2022-11-15 22:20:02 +08:00
|
|
|
mousePosition?: MousePosition;
|
2022-09-06 21:46:49 +08:00
|
|
|
|
|
|
|
// Legacy
|
|
|
|
/** @deprecated Please use `open` instead. */
|
|
|
|
visible?: boolean;
|
2016-08-15 12:00:05 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 21:22:27 +08:00
|
|
|
type getContainerFunc = () => HTMLElement;
|
|
|
|
|
2017-03-03 11:40:54 +08:00
|
|
|
export interface ModalFuncProps {
|
2017-11-21 17:16:48 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2022-08-23 16:55:57 +08:00
|
|
|
open?: boolean;
|
2022-09-14 14:41:20 +08:00
|
|
|
/** @deprecated Please use `open` instead. */
|
|
|
|
visible?: boolean;
|
2017-11-21 17:16:48 +08:00
|
|
|
title?: React.ReactNode;
|
2020-11-21 21:13:28 +08:00
|
|
|
closable?: boolean;
|
2017-11-21 17:16:48 +08:00
|
|
|
content?: React.ReactNode;
|
2019-06-16 20:51:47 +08:00
|
|
|
// TODO: find out exact types
|
2019-06-24 11:29:58 +08:00
|
|
|
onOk?: (...args: any[]) => any;
|
|
|
|
onCancel?: (...args: any[]) => any;
|
2020-12-07 13:44:06 +08:00
|
|
|
afterClose?: () => void;
|
2020-04-06 13:00:41 +08:00
|
|
|
okButtonProps?: ButtonProps;
|
|
|
|
cancelButtonProps?: ButtonProps;
|
2018-07-31 17:05:07 +08:00
|
|
|
centered?: boolean;
|
2017-03-03 11:40:54 +08:00
|
|
|
width?: string | number;
|
2019-02-01 00:08:30 +08:00
|
|
|
okText?: React.ReactNode;
|
2020-05-05 12:03:16 +08:00
|
|
|
okType?: LegacyButtonType;
|
2019-02-01 00:08:30 +08:00
|
|
|
cancelText?: React.ReactNode;
|
2018-12-26 21:36:36 +08:00
|
|
|
icon?: React.ReactNode;
|
2019-01-10 01:21:51 +08:00
|
|
|
mask?: boolean;
|
2017-07-17 15:26:37 +08:00
|
|
|
maskClosable?: boolean;
|
2017-09-15 22:51:03 +08:00
|
|
|
zIndex?: number;
|
2017-11-21 17:16:48 +08:00
|
|
|
okCancel?: boolean;
|
|
|
|
style?: React.CSSProperties;
|
2021-10-28 18:41:30 +08:00
|
|
|
wrapClassName?: string;
|
2018-10-27 18:54:52 +08:00
|
|
|
maskStyle?: React.CSSProperties;
|
2021-01-24 17:40:30 +08:00
|
|
|
type?: 'info' | 'success' | 'error' | 'warn' | 'warning' | 'confirm';
|
2018-02-11 13:44:59 +08:00
|
|
|
keyboard?: boolean;
|
2021-07-26 16:47:55 +08:00
|
|
|
getContainer?: string | HTMLElement | getContainerFunc | false;
|
2018-09-29 17:00:01 +08:00
|
|
|
autoFocusButton?: null | 'ok' | 'cancel';
|
2019-01-11 09:43:44 +08:00
|
|
|
transitionName?: string;
|
|
|
|
maskTransitionName?: string;
|
2020-11-04 14:12:45 +08:00
|
|
|
direction?: DirectionType;
|
2020-10-22 11:24:49 +08:00
|
|
|
bodyStyle?: React.CSSProperties;
|
2020-11-21 21:13:28 +08:00
|
|
|
closeIcon?: React.ReactNode;
|
2022-12-07 21:33:44 +08:00
|
|
|
footer?: React.ReactNode;
|
2020-10-30 19:52:12 +08:00
|
|
|
modalRender?: (node: React.ReactNode) => React.ReactNode;
|
2020-11-30 16:57:33 +08:00
|
|
|
focusTriggerAfterClose?: boolean;
|
2017-03-03 11:40:54 +08:00
|
|
|
}
|
2017-11-21 17:16:48 +08:00
|
|
|
|
|
|
|
export interface ModalLocale {
|
|
|
|
okText: string;
|
|
|
|
cancelText: string;
|
|
|
|
justOkText: string;
|
|
|
|
}
|
|
|
|
|
2022-11-17 14:04:31 +08:00
|
|
|
const Modal: React.FC<ModalProps> = (props) => {
|
2021-07-26 16:47:55 +08:00
|
|
|
const {
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
direction,
|
|
|
|
} = React.useContext(ConfigContext);
|
2016-03-03 17:43:38 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const handleCancel = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
const { onCancel } = props;
|
2021-02-19 18:26:53 +08:00
|
|
|
onCancel?.(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const handleOk = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
const { onOk } = props;
|
2021-02-19 18:26:53 +08:00
|
|
|
onOk?.(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2022-08-23 16:55:57 +08:00
|
|
|
warning(
|
|
|
|
!('visible' in props),
|
|
|
|
'Modal',
|
|
|
|
`\`visible\` will be removed in next major version, please use \`open\` instead.`,
|
|
|
|
);
|
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2022-06-11 21:39:58 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-10-11 21:33:29 +08:00
|
|
|
open,
|
2020-05-19 20:15:51 +08:00
|
|
|
wrapClassName,
|
|
|
|
centered,
|
|
|
|
getContainer,
|
|
|
|
closeIcon,
|
2020-11-30 16:57:33 +08:00
|
|
|
focusTriggerAfterClose = true,
|
2022-09-06 21:46:49 +08:00
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
visible,
|
|
|
|
|
2022-10-02 16:50:34 +08:00
|
|
|
width = 520,
|
2023-03-13 19:55:59 +08:00
|
|
|
footer,
|
2020-05-19 20:15:51 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('modal', customizePrefixCls);
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2022-04-26 22:45:39 +08:00
|
|
|
// Style
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2021-02-08 17:09:13 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const wrapClassNameExtended = classNames(wrapClassName, {
|
|
|
|
[`${prefixCls}-centered`]: !!centered,
|
|
|
|
[`${prefixCls}-wrap-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2022-08-24 11:15:25 +08:00
|
|
|
|
2022-09-06 21:46:49 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(!('visible' in props), 'Modal', '`visible` is deprecated, please use `open` instead.');
|
|
|
|
}
|
2022-08-24 11:15:25 +08:00
|
|
|
|
2023-03-14 15:14:40 +08:00
|
|
|
const dialogFooter =
|
|
|
|
footer === undefined ? <Footer {...props} onOk={handleOk} onCancel={handleCancel} /> : footer;
|
|
|
|
|
2022-04-26 22:45:39 +08:00
|
|
|
return wrapSSR(
|
2022-11-22 22:52:40 +08:00
|
|
|
<NoCompactStyle>
|
|
|
|
<NoFormStyle status override>
|
|
|
|
<Dialog
|
|
|
|
width={width}
|
|
|
|
{...restProps}
|
2022-12-15 17:48:29 +08:00
|
|
|
getContainer={getContainer === undefined ? getContextPopupContainer : getContainer}
|
2022-11-22 22:52:40 +08:00
|
|
|
prefixCls={prefixCls}
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName={classNames(hashId, rootClassName)}
|
2022-11-22 22:52:40 +08:00
|
|
|
wrapClassName={wrapClassNameExtended}
|
2023-03-14 15:14:40 +08:00
|
|
|
footer={dialogFooter}
|
2022-11-22 22:52:40 +08:00
|
|
|
visible={open ?? visible}
|
|
|
|
mousePosition={restProps.mousePosition ?? mousePosition}
|
|
|
|
onClose={handleCancel}
|
|
|
|
closeIcon={renderCloseIcon(prefixCls, closeIcon)}
|
|
|
|
focusTriggerAfterClose={focusTriggerAfterClose}
|
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'zoom', props.transitionName)}
|
|
|
|
maskTransitionName={getTransitionName(rootPrefixCls, 'fade', props.maskTransitionName)}
|
|
|
|
className={classNames(hashId, className)}
|
|
|
|
/>
|
|
|
|
</NoFormStyle>
|
|
|
|
</NoCompactStyle>,
|
2020-05-19 20:15:51 +08:00
|
|
|
);
|
|
|
|
};
|
2016-08-29 20:48:47 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
export default Modal;
|