import { ElButton, ElPopover, ElPopconfirm } from 'element-plus' import CipButtonText from '../cip-button-text' import CipButton from '../cip-button' import { computed, defineComponent, ref } from 'vue' import { useConfig } from '@xdp/config' import { useButtonPop, type PopSlotScope } from './use-button-pop' import PopConfirmContent from './pop-confirm-content' export default defineComponent({ name: 'CipButtonPop', props: { needPop: { type: Boolean, default: true }, popTitle: String, popMessage: String, popPlacement: String, buttonComp: { type: String, default: 'text' }, popProps: { type: Object, default: () => ({}) }, popLegacyCancel: { type: Boolean, default: true } }, inheritAttrs: false, setup (props, { attrs, slots }) { const xdpConfig = useConfig() const popoverRef = ref>() const buttonPop = computed(() => useButtonPop(props, attrs, slots, xdpConfig)) const ButtonComponent = computed(() => props.buttonComp === 'button' ? CipButton : CipButtonText ) const renderButton = (onClickHandler?: (e?: MouseEvent) => void) => { const Comp = ButtonComponent.value const { buttonProps } = buttonPop.value return ( {slots.reference?.() ?? slots.button?.() ?? slots.default?.()} ) } const renderPopContent = (scope: PopSlotScope) => { const customContent = slots.pop?.(scope) ?? (buttonPop.value.hasReferenceSlot ? slots.default?.(scope) : undefined) if (customContent) return customContent const { icon, iconColor, hideIcon } = buttonPop.value.popProps return ( {{ title: slots['pop-title'], message: slots['pop-message'] }} ) } const hidePopover = () => { popoverRef.value?.hide?.() } const renderRichPopconfirm = ( onClick?: (e?: MouseEvent, confirmed?: boolean) => void, onCancelHandler?: (e?: MouseEvent) => void ) => { const { popProps, popoverProps, popLegacyCancel } = buttonPop.value const confirmButtonText = popProps.confirmButtonText ?? '确定' const cancelButtonText = popProps.cancelButtonText ?? '取消' const confirmButtonType = popProps.confirmButtonType ?? 'primary' const cancelButtonType = popProps.cancelButtonType ?? 'default' const { popperClass, trigger, teleported, ...restPopoverProps } = popoverProps const handleConfirm = (e?: MouseEvent) => { onClick?.(e, true) hidePopover() } const handleCancel = (e?: MouseEvent) => { onCancelHandler?.(e) if (!popLegacyCancel) { onClick?.(e, false) } hidePopover() } const scope: PopSlotScope = { title: buttonPop.value.resolvedTitle, message: buttonPop.value.resolvedMessage, confirm: handleConfirm, cancel: handleCancel } return ( {{ // 触发区仅负责打开确认层,业务 onClick 只在 confirm/cancel 阶段触发 reference: () => renderButton(() => {}), default: () => (
{renderPopContent(scope)}
) }}
) } return () => { const { needPop, buttonProps, popProps, hasRichPop, onClick, onCancel, popLegacyCancel } = buttonPop.value if (!needPop || buttonProps.disabled) { return renderButton(onClick) } if (hasRichPop) { return renderRichPopconfirm(onClick, onCancel) } return ( onClick?.(e, true)} onCancel={(e) => { onCancel?.(e) if (!popLegacyCancel) { onClick?.(e, false) } }} > {{ reference: () => renderButton(() => {}) }} ) } } })