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';
|
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';
|
2022-06-11 21:39:58 +08:00
|
|
|
import { renderCloseIcon, renderFooter } from './PurePanel';
|
2022-04-26 22:45:39 +08:00
|
|
|
import useStyle from './style';
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
let mousePosition: { x: number; y: number } | null;
|
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 {
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 对话框是否可见 */
|
2022-08-23 16:55:57 +08:00
|
|
|
open?: boolean;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 确定按钮 loading */
|
2016-08-15 12:00:05 +08:00
|
|
|
confirmLoading?: boolean;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 标题 */
|
2021-11-18 14:39:22 +08:00
|
|
|
title?: React.ReactNode;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 是否显示右上角的关闭按钮 */
|
2016-08-15 12:00:05 +08:00
|
|
|
closable?: boolean;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 点击确定回调 */
|
2019-06-16 20:51:47 +08:00
|
|
|
onOk?: (e: React.MouseEvent<HTMLElement>) => void;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 点击模态框右上角叉、取消按钮、Props.maskClosable 值为 true 时的遮罩层或键盘按下 Esc 时的回调 */
|
2019-06-16 20:51:47 +08:00
|
|
|
onCancel?: (e: React.MouseEvent<HTMLElement>) => void;
|
2017-01-05 12:08:43 +08:00
|
|
|
afterClose?: () => void;
|
2018-11-26 19:32:33 +08:00
|
|
|
/** 垂直居中 */
|
2018-07-31 17:05:07 +08:00
|
|
|
centered?: boolean;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 宽度 */
|
2016-08-15 12:00:05 +08:00
|
|
|
width?: string | number;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 底部内容 */
|
2016-08-15 12:00:05 +08:00
|
|
|
footer?: React.ReactNode;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 确认按钮文字 */
|
2019-02-01 00:12:06 +08:00
|
|
|
okText?: React.ReactNode;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 确认按钮类型 */
|
2020-05-05 12:03:16 +08:00
|
|
|
okType?: LegacyButtonType;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 取消按钮文字 */
|
2019-02-01 00:12:06 +08:00
|
|
|
cancelText?: React.ReactNode;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 点击蒙层是否允许关闭 */
|
2016-08-15 12:00:05 +08:00
|
|
|
maskClosable?: boolean;
|
2019-08-05 18:38:10 +08:00
|
|
|
/** 强制渲染 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;
|
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;
|
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;
|
2022-08-23 16:55:57 +08:00
|
|
|
open?: 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;
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:22:42 +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 17:48:00 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
2022-08-24 11:15:25 +08:00
|
|
|
!('visible' in props),
|
2022-08-23 17:48:00 +08:00
|
|
|
'Modal',
|
|
|
|
`\`visible\` is removed in v5, please use \`open\` instead.`,
|
2017-10-20 14:54:38 +08:00
|
|
|
);
|
2022-08-23 17:48:00 +08:00
|
|
|
}
|
2016-03-03 17:43:38 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2022-06-11 21:39:58 +08:00
|
|
|
className,
|
2022-08-23 16:55:57 +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,
|
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
|
|
|
|
|
|
|
warning(
|
|
|
|
!('visible' in props),
|
|
|
|
'Modal',
|
|
|
|
`\`visible\` is deprecated, please use \`open\` instead.`,
|
|
|
|
);
|
|
|
|
|
2022-04-26 22:45:39 +08:00
|
|
|
return wrapSSR(
|
2022-06-06 23:39:00 +08:00
|
|
|
<NoFormStyle status override>
|
|
|
|
<Dialog
|
|
|
|
{...restProps}
|
|
|
|
getContainer={
|
|
|
|
getContainer === undefined ? (getContextPopupContainer as getContainerFunc) : getContainer
|
|
|
|
}
|
|
|
|
prefixCls={prefixCls}
|
2022-06-07 21:13:52 +08:00
|
|
|
rootClassName={hashId}
|
2022-06-06 23:39:00 +08:00
|
|
|
wrapClassName={wrapClassNameExtended}
|
2022-06-11 21:39:58 +08:00
|
|
|
footer={renderFooter({
|
|
|
|
...props,
|
|
|
|
onOk: handleOk,
|
|
|
|
onCancel: handleCancel,
|
|
|
|
})}
|
2022-08-23 17:48:00 +08:00
|
|
|
visible={open}
|
2022-06-06 23:39:00 +08:00
|
|
|
mousePosition={mousePosition}
|
|
|
|
onClose={handleCancel}
|
2022-06-11 21:39:58 +08:00
|
|
|
closeIcon={renderCloseIcon(prefixCls, closeIcon)}
|
2022-06-06 23:39:00 +08:00
|
|
|
focusTriggerAfterClose={focusTriggerAfterClose}
|
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'zoom', props.transitionName)}
|
|
|
|
maskTransitionName={getTransitionName(rootPrefixCls, 'fade', props.maskTransitionName)}
|
2022-06-11 21:39:58 +08:00
|
|
|
className={classNames(hashId, className)}
|
2022-06-06 23:39:00 +08:00
|
|
|
/>
|
2022-06-07 21:13:52 +08:00
|
|
|
</NoFormStyle>,
|
2020-05-19 20:15:51 +08:00
|
|
|
);
|
|
|
|
};
|
2016-08-29 20:48:47 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
Modal.defaultProps = {
|
|
|
|
width: 520,
|
|
|
|
confirmLoading: false,
|
2022-08-23 16:55:57 +08:00
|
|
|
open: false,
|
2020-05-19 20:15:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Modal;
|