2021-09-25 08:51:32 +00:00
|
|
|
import type { ExtractPropTypes, CSSProperties, PropType } from 'vue';
|
2022-01-06 08:08:53 +00:00
|
|
|
import { defineComponent } from 'vue';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Dialog from '../vc-dialog';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-03-07 11:45:13 +00:00
|
|
|
import addEventListener from '../vc-util/Dom/addEventListener';
|
2020-03-24 08:58:53 +00:00
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Button from '../button';
|
2021-06-30 06:45:05 +00:00
|
|
|
import type { ButtonProps as ButtonPropsType, LegacyButtonType } from '../button/buttonTypes';
|
2022-01-06 08:08:53 +00:00
|
|
|
import { convertLegacyProps } from '../button/buttonTypes';
|
2021-10-26 08:12:56 +00:00
|
|
|
import { useLocaleReceiver } from '../locale-provider/LocaleReceiver';
|
2020-10-19 15:09:36 +00:00
|
|
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
2022-01-06 08:08:53 +00:00
|
|
|
import type { Direction } from '../config-provider';
|
2021-09-25 08:51:32 +00:00
|
|
|
import type { VueNode } from '../_util/type';
|
2022-01-06 08:08:53 +00:00
|
|
|
import { canUseDocElement } from '../_util/styleChecker';
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import { getTransitionName } from '../_util/transition';
|
2018-03-06 11:14:41 +00:00
|
|
|
|
2020-10-19 15:09:36 +00:00
|
|
|
let mousePosition: { x: number; y: number } | null = null;
|
2020-03-07 11:45:13 +00:00
|
|
|
// ref: https://github.com/ant-design/ant-design/issues/15795
|
2020-10-19 15:09:36 +00:00
|
|
|
const getClickPosition = (e: MouseEvent) => {
|
2020-03-07 11:45:13 +00:00
|
|
|
mousePosition = {
|
|
|
|
x: e.pageX,
|
|
|
|
y: e.pageY,
|
|
|
|
};
|
|
|
|
// 100ms 内发生过点击事件,则从点击位置动画展示
|
|
|
|
// 否则直接 zoom 展示
|
|
|
|
// 这样可以兼容非点击方式展开
|
|
|
|
setTimeout(() => (mousePosition = null), 100);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 只有点击事件支持从鼠标位置动画展开
|
2022-01-06 08:08:53 +00:00
|
|
|
if (canUseDocElement()) {
|
2020-03-15 05:40:58 +00:00
|
|
|
addEventListener(document.documentElement, 'click', getClickPosition, true);
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 08:08:53 +00:00
|
|
|
const modalProps = () => ({
|
|
|
|
prefixCls: String,
|
|
|
|
visible: { type: Boolean, default: undefined },
|
|
|
|
confirmLoading: { type: Boolean, default: undefined },
|
2020-10-19 15:09:36 +00:00
|
|
|
title: PropTypes.any,
|
2022-01-06 08:08:53 +00:00
|
|
|
closable: { type: Boolean, default: undefined },
|
2020-10-19 15:09:36 +00:00
|
|
|
closeIcon: PropTypes.any,
|
2022-01-06 08:08:53 +00:00
|
|
|
onOk: Function as PropType<(e: MouseEvent) => void>,
|
|
|
|
onCancel: Function as PropType<(e: MouseEvent) => void>,
|
|
|
|
'onUpdate:visible': Function as PropType<(visible: boolean) => void>,
|
|
|
|
onChange: Function as PropType<(visible: boolean) => void>,
|
|
|
|
afterClose: Function as PropType<() => void>,
|
|
|
|
centered: { type: Boolean, default: undefined },
|
|
|
|
width: [String, Number],
|
2020-10-19 15:09:36 +00:00
|
|
|
footer: PropTypes.any,
|
|
|
|
okText: PropTypes.any,
|
2022-01-06 08:08:53 +00:00
|
|
|
okType: String as PropType<LegacyButtonType>,
|
2020-10-19 15:09:36 +00:00
|
|
|
cancelText: PropTypes.any,
|
|
|
|
icon: PropTypes.any,
|
2022-01-06 08:08:53 +00:00
|
|
|
maskClosable: { type: Boolean, default: undefined },
|
|
|
|
forceRender: { type: Boolean, default: undefined },
|
|
|
|
okButtonProps: Object as PropType<ButtonPropsType>,
|
|
|
|
cancelButtonProps: Object as PropType<ButtonPropsType>,
|
|
|
|
destroyOnClose: { type: Boolean, default: undefined },
|
|
|
|
wrapClassName: String,
|
|
|
|
maskTransitionName: String,
|
|
|
|
transitionName: String,
|
2022-01-12 15:50:02 +00:00
|
|
|
getContainer: {
|
|
|
|
type: [String, Function, Boolean, Object] as PropType<
|
|
|
|
string | HTMLElement | getContainerFunc | false
|
|
|
|
>,
|
|
|
|
default: undefined,
|
|
|
|
},
|
2022-01-06 08:08:53 +00:00
|
|
|
zIndex: Number,
|
|
|
|
bodyStyle: Object as PropType<CSSProperties>,
|
|
|
|
maskStyle: Object as PropType<CSSProperties>,
|
|
|
|
mask: { type: Boolean, default: undefined },
|
|
|
|
keyboard: { type: Boolean, default: undefined },
|
|
|
|
wrapProps: Object,
|
|
|
|
focusTriggerAfterClose: { type: Boolean, default: undefined },
|
|
|
|
modalRender: Function as PropType<(arg: { originVNode: VueNode }) => VueNode>,
|
|
|
|
});
|
2020-10-19 15:09:36 +00:00
|
|
|
|
2022-01-06 08:08:53 +00:00
|
|
|
export type ModalProps = Partial<ExtractPropTypes<ReturnType<typeof modalProps>>>;
|
2020-10-19 15:09:36 +00:00
|
|
|
|
|
|
|
export interface ModalFuncProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
class?: string;
|
|
|
|
visible?: boolean;
|
2021-09-25 08:51:32 +00:00
|
|
|
title?: string | (() => VueNode) | VueNode;
|
2021-03-27 14:03:23 +00:00
|
|
|
closable?: boolean;
|
2021-09-25 08:51:32 +00:00
|
|
|
content?: string | (() => VueNode) | VueNode;
|
2020-10-19 15:09:36 +00:00
|
|
|
// TODO: find out exact types
|
|
|
|
onOk?: (...args: any[]) => any;
|
|
|
|
onCancel?: (...args: any[]) => any;
|
2022-01-06 08:08:53 +00:00
|
|
|
afterClose?: () => void;
|
2021-04-09 03:00:41 +00:00
|
|
|
okButtonProps?: ButtonPropsType;
|
|
|
|
cancelButtonProps?: ButtonPropsType;
|
2020-10-19 15:09:36 +00:00
|
|
|
centered?: boolean;
|
|
|
|
width?: string | number;
|
2021-09-25 08:51:32 +00:00
|
|
|
okText?: string | (() => VueNode) | VueNode;
|
2021-06-30 02:03:17 +00:00
|
|
|
okType?: LegacyButtonType;
|
2021-09-25 08:51:32 +00:00
|
|
|
cancelText?: string | (() => VueNode) | VueNode;
|
|
|
|
icon?: (() => VueNode) | VueNode;
|
2022-03-12 01:56:32 +00:00
|
|
|
wrapClassName?: String;
|
2020-10-19 15:09:36 +00:00
|
|
|
/* Deprecated */
|
|
|
|
iconType?: string;
|
|
|
|
mask?: boolean;
|
|
|
|
maskClosable?: boolean;
|
|
|
|
zIndex?: number;
|
|
|
|
okCancel?: boolean;
|
|
|
|
style?: CSSProperties | string;
|
|
|
|
maskStyle?: CSSProperties;
|
2022-01-06 08:08:53 +00:00
|
|
|
type?: 'info' | 'success' | 'error' | 'warn' | 'warning' | 'confirm';
|
2020-10-19 15:09:36 +00:00
|
|
|
keyboard?: boolean;
|
2022-03-12 01:56:32 +00:00
|
|
|
getContainer?: string | HTMLElement | getContainerFunc | false | null;
|
2020-10-19 15:09:36 +00:00
|
|
|
autoFocusButton?: null | 'ok' | 'cancel';
|
|
|
|
transitionName?: string;
|
|
|
|
maskTransitionName?: string;
|
2022-01-06 08:08:53 +00:00
|
|
|
direction?: Direction;
|
|
|
|
bodyStyle?: CSSProperties;
|
|
|
|
closeIcon?: string | (() => VueNode) | VueNode;
|
|
|
|
modalRender?: (arg: { originVNode: VueNode }) => VueNode;
|
|
|
|
focusTriggerAfterClose?: boolean;
|
2021-09-08 09:12:11 +00:00
|
|
|
|
|
|
|
/** @deprecated please use `appContext` instead */
|
2021-07-01 13:45:35 +00:00
|
|
|
parentContext?: any;
|
2021-09-08 09:12:11 +00:00
|
|
|
appContext?: any;
|
2020-10-19 15:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type getContainerFunc = () => HTMLElement;
|
|
|
|
|
2021-06-23 15:08:16 +00:00
|
|
|
export type ModalFunc = (props: ModalFuncProps) => {
|
2020-10-19 15:09:36 +00:00
|
|
|
destroy: () => void;
|
|
|
|
update: (newConfig: ModalFuncProps) => void;
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-06 11:14:41 +00:00
|
|
|
|
2020-10-19 15:09:36 +00:00
|
|
|
export interface ModalLocale {
|
|
|
|
okText: string;
|
|
|
|
cancelText: string;
|
|
|
|
justOkText: string;
|
|
|
|
}
|
|
|
|
|
2019-04-17 02:21:28 +00:00
|
|
|
export const destroyFns = [];
|
|
|
|
|
2020-10-19 15:09:36 +00:00
|
|
|
export default defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AModal',
|
2020-01-18 09:06:09 +00:00
|
|
|
inheritAttrs: false,
|
2022-01-06 08:08:53 +00:00
|
|
|
props: initDefaultProps(modalProps(), {
|
2018-03-06 11:14:41 +00:00
|
|
|
width: 520,
|
|
|
|
transitionName: 'zoom',
|
|
|
|
maskTransitionName: 'fade',
|
|
|
|
confirmLoading: false,
|
|
|
|
visible: false,
|
|
|
|
okType: 'primary',
|
|
|
|
}),
|
2022-01-06 08:08:53 +00:00
|
|
|
setup(props, { emit, slots, attrs }) {
|
2021-10-26 08:12:56 +00:00
|
|
|
const [locale] = useLocaleReceiver('Modal');
|
2022-01-06 08:08:53 +00:00
|
|
|
const { prefixCls, rootPrefixCls, direction, getPopupContainer } = useConfigInject(
|
|
|
|
'modal',
|
|
|
|
props,
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleCancel = (e: MouseEvent) => {
|
|
|
|
emit('update:visible', false);
|
|
|
|
emit('cancel', e);
|
|
|
|
emit('change', false);
|
2020-03-07 11:45:13 +00:00
|
|
|
};
|
2018-03-06 11:14:41 +00:00
|
|
|
|
2022-01-06 08:08:53 +00:00
|
|
|
const handleOk = (e: MouseEvent) => {
|
|
|
|
emit('ok', e);
|
|
|
|
};
|
2021-04-09 03:00:41 +00:00
|
|
|
|
2022-01-06 08:08:53 +00:00
|
|
|
const renderFooter = () => {
|
|
|
|
const {
|
|
|
|
okText = slots.okText?.(),
|
|
|
|
okType,
|
|
|
|
cancelText = slots.cancelText?.(),
|
|
|
|
confirmLoading,
|
|
|
|
} = props;
|
2018-03-06 11:14:41 +00:00
|
|
|
return (
|
2022-01-06 08:08:53 +00:00
|
|
|
<>
|
|
|
|
<Button onClick={handleCancel} {...props.cancelButtonProps}>
|
|
|
|
{cancelText || locale.value.cancelText}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
{...convertLegacyProps(okType)}
|
|
|
|
loading={confirmLoading}
|
|
|
|
onClick={handleOk}
|
|
|
|
{...props.okButtonProps}
|
|
|
|
>
|
|
|
|
{okText || locale.value.okText}
|
2018-03-06 11:14:41 +00:00
|
|
|
</Button>
|
2022-01-06 08:08:53 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
return () => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
visible,
|
|
|
|
wrapClassName,
|
|
|
|
centered,
|
|
|
|
getContainer,
|
|
|
|
closeIcon = slots.closeIcon?.(),
|
|
|
|
focusTriggerAfterClose = true,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const wrapClassNameExtended = classNames(wrapClassName, {
|
|
|
|
[`${prefixCls.value}-centered`]: !!centered,
|
|
|
|
[`${prefixCls.value}-wrap-rtl`]: direction.value === 'rtl',
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
{...restProps}
|
|
|
|
{...attrs}
|
2022-01-12 15:50:02 +00:00
|
|
|
getContainer={getContainer || getPopupContainer.value}
|
2022-01-06 08:08:53 +00:00
|
|
|
prefixCls={prefixCls.value}
|
|
|
|
wrapClassName={wrapClassNameExtended}
|
|
|
|
visible={visible}
|
|
|
|
mousePosition={mousePosition}
|
|
|
|
onClose={handleCancel}
|
|
|
|
focusTriggerAfterClose={focusTriggerAfterClose}
|
|
|
|
transitionName={getTransitionName(rootPrefixCls.value, 'zoom', props.transitionName)}
|
|
|
|
maskTransitionName={getTransitionName(
|
|
|
|
rootPrefixCls.value,
|
|
|
|
'fade',
|
|
|
|
props.maskTransitionName,
|
|
|
|
)}
|
|
|
|
v-slots={{
|
|
|
|
...slots,
|
|
|
|
footer: slots.footer || renderFooter,
|
|
|
|
closeIcon: () => {
|
|
|
|
return (
|
|
|
|
<span class={`${prefixCls.value}-close-x`}>
|
|
|
|
{closeIcon || <CloseOutlined class={`${prefixCls.value}-close-icon`} />}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
></Dialog>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
|
|
|
};
|
2018-03-06 11:14:41 +00:00
|
|
|
},
|
2020-10-19 15:09:36 +00:00
|
|
|
});
|