import type { ExtractPropTypes, HTMLAttributes, PropType } from 'vue'; import { computed, ref, toRef, defineComponent } from 'vue'; import Popover from '../popover'; import abstractTooltipProps from '../tooltip/abstractTooltipProps'; import { initDefaultProps } from '../_util/props-util'; import type { ButtonProps, LegacyButtonType } from '../button/buttonTypes'; import { convertLegacyProps } from '../button/buttonTypes'; import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled'; import Button from '../button'; import { useLocaleReceiver } from '../locale-provider/LocaleReceiver'; import defaultLocale from '../locale/en_US'; import { anyType, objectType, stringType, withInstall } from '../_util/type'; import useMergedState from '../_util/hooks/useMergedState'; import KeyCode from '../_util/KeyCode'; import useConfigInject from '../config-provider/hooks/useConfigInject'; import classNames from '../_util/classNames'; import { getTransitionName } from '../_util/transition'; import { cloneVNodes } from '../_util/vnode'; import omit from '../_util/omit'; import { tooltipDefaultProps } from '../tooltip/Tooltip'; import ActionButton from '../_util/ActionButton'; import usePopconfirmStyle from './style'; export const popconfirmProps = () => ({ ...abstractTooltipProps(), prefixCls: String, content: anyType(), title: anyType(), description: anyType(), okType: stringType('primary'), disabled: { type: Boolean, default: false }, okText: anyType(), cancelText: anyType(), icon: anyType(), okButtonProps: objectType(), cancelButtonProps: objectType(), showCancel: { type: Boolean, default: true }, onConfirm: Function as PropType<(e: MouseEvent) => void>, onCancel: Function as PropType<(e: MouseEvent) => void>, }); export type PopconfirmProps = Partial>>; export interface PopconfirmLocale { okText: string; cancelText: string; } const Popconfirm = defineComponent({ compatConfig: { MODE: 3 }, name: 'APopconfirm', inheritAttrs: false, props: initDefaultProps(popconfirmProps(), { ...tooltipDefaultProps(), trigger: 'click', transitionName: 'zoom-big', placement: 'top', mouseEnterDelay: 0.1, mouseLeaveDelay: 0.1, arrowPointAtCenter: false, autoAdjustOverflow: true, okType: 'primary', disabled: false, }), slots: ['title', 'content', 'okText', 'icon', 'cancelText', 'cancelButton', 'okButton'], // emits: ['update:open', 'visibleChange'], setup(props: PopconfirmProps, { slots, emit, expose, attrs }) { const rootRef = ref(); expose({ getPopupDomNode: () => { return rootRef.value?.getPopupDomNode?.(); }, }); const [open, setOpen] = useMergedState(false, { value: toRef(props, 'open'), }); const settingOpen = (value: boolean, e?: MouseEvent | KeyboardEvent) => { if (props.open === undefined) { setOpen(value); } emit('update:open', value); emit('openChange', value, e); }; const close = (e: MouseEvent) => { settingOpen(false, e); }; const onConfirm = (e: MouseEvent) => { return props.onConfirm?.(e); }; const onCancel = (e: MouseEvent) => { settingOpen(false, e); props.onCancel?.(e); }; const onKeyDown = (e: KeyboardEvent) => { if (e.keyCode === KeyCode.ESC && open) { settingOpen(false, e); } }; const onOpenChange = (value: boolean) => { const { disabled } = props; if (disabled) { return; } settingOpen(value); }; const { prefixCls: prefixClsConfirm, getPrefixCls } = useConfigInject('popconfirm', props); const rootPrefixCls = computed(() => getPrefixCls()); const btnPrefixCls = computed(() => getPrefixCls('btn')); const [wrapSSR] = usePopconfirmStyle(prefixClsConfirm); const [popconfirmLocale] = useLocaleReceiver('Popconfirm', defaultLocale.Popconfirm); const renderOverlay = () => { const { okButtonProps, cancelButtonProps, title = slots.title?.(), description = slots.description?.(), cancelText = slots.cancel?.(), okText = slots.okText?.(), okType, icon = slots.icon?.() || , showCancel = true, } = props; const { cancelButton, okButton } = slots; const cancelProps: ButtonProps = { onClick: onCancel, size: 'small', ...cancelButtonProps, }; const okProps: ButtonProps = { onClick: onConfirm, ...convertLegacyProps(okType), size: 'small', ...okButtonProps, }; return (
{icon && {icon}}
{title}
{description &&
{description}
}
{showCancel ? ( cancelButton ? ( cancelButton(cancelProps) ) : ( ) ) : null} {okButton ? ( okButton(okProps) ) : ( {okText || popconfirmLocale.value.okText} )}
); }; return () => { const { placement, overlayClassName, trigger = 'click', ...restProps } = props; const otherProps = omit(restProps, [ 'title', 'content', 'cancelText', 'okText', 'onUpdate:open', 'onConfirm', 'onCancel', 'prefixCls', ]); const overlayClassNames = classNames(prefixClsConfirm.value, overlayClassName); return wrapSSR( {cloneVNodes( slots.default?.() || [], { onKeydown: (e: KeyboardEvent) => { onKeyDown(e); }, }, false, )} , ); }; }, }); export default withInstall(Popconfirm);