1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import { createVNode, render, h, nextTick } from 'vue' import type { Component, VNode } from 'vue' import type { PopupProps } from 'vant' import Popup from './popup.vue'
interface Options extends PopupProps { title?: string showConfirmButton?: boolean showCancelButton?: boolean onBeforeConfirm?: () => Promise<void> }
type CloseType = 'close' | 'cancel' | 'confirm'
interface ShowPopup { (comp: Component, props: Record<string, any> | null, options: Partial<Options>): Promise<void> }
export const showPopup: ShowPopup = (comp, props = {}, options = {}) => { return new Promise((resolve, reject) => { const container = document.createElement('div') document.body.appendChild(container)
const closePopup = (type: CloseType) => { render(null, container) document.body.removeChild(container) type === 'confirm' ? resolve() : reject(type) }
const onUpdateShow = async (value: boolean, type: CloseType) => { await nextTick() if (!value) { closePopup(type) } }
const vnode = createVNode( Popup, { ...options, show: true, 'onUpdate:show': onUpdateShow }, { default: (): VNode => h(comp, { ...props, closePopup }) } )
render(vnode, container) }) }
|