2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-01-12 17:07:24 +08:00
|
|
|
import Dialog from 'rc-dialog';
|
2018-07-31 17:05:07 +08:00
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
import useModal from './useModal';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { getConfirmLocale } from './locale';
|
2017-07-17 09:40:55 +08:00
|
|
|
import Button from '../button';
|
2020-05-05 12:03:16 +08:00
|
|
|
import { LegacyButtonType, ButtonProps, convertLegacyProps } from '../button/button';
|
2017-10-20 14:54:38 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2020-11-04 14:12:45 +08:00
|
|
|
import { ConfigContext, DirectionType } from '../config-provider';
|
2016-01-12 17:07:24 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
let mousePosition: { x: number; y: number } | null;
|
2018-12-03 17:08:48 +08:00
|
|
|
export const destroyFns: Array<() => void> = [];
|
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
|
|
|
};
|
|
|
|
|
|
|
|
// 只有点击事件支持从鼠标位置动画展开
|
|
|
|
if (typeof window !== 'undefined' && window.document && window.document.documentElement) {
|
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
|
|
|
/** 对话框是否可见 */
|
2016-08-15 12:00:05 +08:00
|
|
|
visible?: 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
|
|
|
/** 标题 */
|
2016-09-19 10:17:07 +08:00
|
|
|
title?: React.ReactNode | string;
|
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;
|
2019-07-11 21:22:27 +08:00
|
|
|
getContainer?: string | HTMLElement | getContainerFunc | false | null;
|
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;
|
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;
|
2017-03-03 11:40:54 +08:00
|
|
|
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-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;
|
2018-10-27 18:54:52 +08:00
|
|
|
maskStyle?: React.CSSProperties;
|
2017-11-21 17:16:48 +08:00
|
|
|
type?: string;
|
2018-02-11 13:44:59 +08:00
|
|
|
keyboard?: boolean;
|
2019-07-11 21:22:27 +08:00
|
|
|
getContainer?: string | HTMLElement | getContainerFunc | false | null;
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
interface ModalInterface extends React.FC<ModalProps> {
|
|
|
|
useModal: typeof useModal;
|
|
|
|
}
|
2020-02-03 13:00:35 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const Modal: ModalInterface = props => {
|
|
|
|
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;
|
2016-10-24 12:04:26 +08:00
|
|
|
if (onCancel) {
|
|
|
|
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;
|
2016-10-24 12:04:26 +08:00
|
|
|
if (onOk) {
|
2017-02-24 16:07:39 +08:00
|
|
|
onOk(e);
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
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 renderFooter = (locale: ModalLocale) => {
|
|
|
|
const { okText, okType, cancelText, confirmLoading } = props;
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
2020-05-11 19:12:04 +08:00
|
|
|
<>
|
2020-05-19 20:15:51 +08:00
|
|
|
<Button onClick={handleCancel} {...props.cancelButtonProps}>
|
2017-10-20 14:54:38 +08:00
|
|
|
{cancelText || locale.cancelText}
|
|
|
|
</Button>
|
|
|
|
<Button
|
2020-05-05 12:03:16 +08:00
|
|
|
{...convertLegacyProps(okType)}
|
2017-10-20 14:54:38 +08:00
|
|
|
loading={confirmLoading}
|
2020-05-19 20:15:51 +08:00
|
|
|
onClick={handleOk}
|
|
|
|
{...props.okButtonProps}
|
2017-10-20 14:54:38 +08:00
|
|
|
>
|
|
|
|
{okText || locale.okText}
|
|
|
|
</Button>
|
2020-05-11 19:12:04 +08:00
|
|
|
</>
|
2017-10-20 14:54:38 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-03 17:43:38 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
footer,
|
|
|
|
visible,
|
|
|
|
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);
|
|
|
|
const defaultFooter = (
|
|
|
|
<LocaleReceiver componentName="Modal" defaultLocale={getConfirmLocale()}>
|
|
|
|
{renderFooter}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
|
|
|
|
const closeIconToRender = (
|
|
|
|
<span className={`${prefixCls}-close-x`}>
|
|
|
|
{closeIcon || <CloseOutlined className={`${prefixCls}-close-icon`} />}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
|
|
|
const wrapClassNameExtended = classNames(wrapClassName, {
|
|
|
|
[`${prefixCls}-centered`]: !!centered,
|
|
|
|
[`${prefixCls}-wrap-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
{...restProps}
|
|
|
|
getContainer={getContainer === undefined ? getContextPopupContainer : getContainer}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
wrapClassName={wrapClassNameExtended}
|
|
|
|
footer={footer === undefined ? defaultFooter : footer}
|
|
|
|
visible={visible}
|
|
|
|
mousePosition={mousePosition}
|
|
|
|
onClose={handleCancel}
|
|
|
|
closeIcon={closeIconToRender}
|
2020-11-30 16:57:33 +08:00
|
|
|
focusTriggerAfterClose={focusTriggerAfterClose}
|
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.useModal = useModal;
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-19 20:15:51 +08:00
|
|
|
Modal.defaultProps = {
|
|
|
|
width: 520,
|
|
|
|
transitionName: 'zoom',
|
|
|
|
maskTransitionName: 'fade',
|
|
|
|
confirmLoading: false,
|
|
|
|
visible: false,
|
|
|
|
okType: 'primary' as LegacyButtonType,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Modal;
|