ant-design/components/modal/Modal.tsx

234 lines
7.1 KiB
TypeScript
Raw Normal View History

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