Skip to content
On this page

PopDialog 气泡对话框

vue
<template>
  <ta-group title="基础用法">
    <ta-cell label="基础">
      <ta-button
        size="small"
        id="popDialog"
        shape="circle"
        icon="DeleteOutlined"
        @click=";(selector = '#popDialog'), (visible = true)"
      >
      </ta-button>
    </ta-cell>
    <ta-cell label="不展示取消按钮">
      <ta-button
        size="small"
        id="popDialogNoCancel"
        shape="circle"
        icon="DeleteOutlined"
        @click=";(selector = '#popDialogNoCancel'), (showCancel = false), (visible = true)"
      >
      </ta-button>
    </ta-cell>
    <ta-cell label="设置按钮文案">
      <ta-button
        size="small"
        id="popDialogButtonText"
        shape="circle"
        icon="DeleteOutlined"
        @click="
          ;(selector = '#popDialogButtonText'),
            (confirmText = '接受'),
            (cancelText = '拒绝'),
            (visible = true)
        "
      >
      </ta-button>
    </ta-cell>
    <ta-cell label="不展示蒙层">
      <ta-button
        size="small"
        id="popDialogNoMask"
        shape="circle"
        icon="DeleteOutlined"
        @click="visible3 = true"
      >
      </ta-button>
    </ta-cell>
  </ta-group>
  <ta-group title="方向 placement=top/bottom/left/right">
    <div class="exp-popover-box2">
      <div>
        <ta-button
          size="small"
          id="popDialogTop2"
          shape="circle"
          icon="UpOutlined"
          @click=";(placement2 = 'top'), (selector2 = '#popDialogTop2'), (visible2 = true)"
        >

        </ta-button>
      </div>
      <div>
        <ta-button
          size="small"
          id="popDialogLeft2"
          shape="circle"
          icon="LeftOutlined"
          @click=";(placement2 = 'left'), (selector2 = '#popDialogLeft2'), (visible2 = true)"
        >

        </ta-button>
        <ta-button
          class="exp-popover-box2-ml"
          size="small"
          id="popDialogRight2"
          shape="circle"
          icon="RightOutlined"
          @click=";(placement2 = 'right'), (selector2 = '#popDialogRight2'), (visible2 = true)"
        >

        </ta-button>
      </div>
      <div>
        <ta-button
          size="small"
          id="popDialogBottom2"
          shape="circle"
          icon="DownOutlined"
          @click=";(placement2 = 'bottom'), (selector2 = '#popDialogBottom2'), (visible2 = true)"
        >

        </ta-button>
      </div>
    </div>
  </ta-group>
  <ta-group title="事件监听">
    <ta-cell label="confirm/cancel">
      <ta-button
        size="small"
        id="popDialogEvent"
        shape="circle"
        icon="DeleteOutlined"
        @click=";(selector = '#popDialogEvent'), (showEvent = true), (visible = true)"
      >
      </ta-button>
    </ta-cell>
    <ta-cell label="visible-state-change">
      <ta-button
        size="small"
        id="popDialogPopupEvent"
        shape="circle"
        icon="DeleteOutlined"
        @click=";(selector = '#popDialogPopupEvent'), (visibleEvent = true), (visible = true)"
      >
      </ta-button>
    </ta-cell>
  </ta-group>
  <ta-group title="API">
    <ta-cell label="showPopDialog">
      <ta-button
        size="small"
        id="popDialogApi"
        shape="circle"
        icon="DeleteOutlined"
        @click="onCallApi('#popDialogApi')"
      >
      </ta-button>
    </ta-cell>
  </ta-group>
  <ta-pop-dialog
    v-model:visible="visible"
    :selector="selector"
    :content="content"
    :show-cancel="showCancel"
    :confirm-text="confirmText"
    :cancel-text="cancelText"
    @cancel="onCancel"
    @confirm="onConfirm"
    @visibleStateChange="onVisibleStateChange"
  >
  </ta-pop-dialog>
  <ta-pop-dialog
    v-model:visible="visible2"
    :selector="selector2"
    :placement="placement2"
    content="这是气泡内容"
    :show-cancel="false"
  >
  </ta-pop-dialog>
  <ta-pop-dialog
    v-model:visible="visible3"
    selector="#popDialogNoMask"
    content="这是气泡内容"
    :showCancel="false"
    :showMask="false"
  >
  </ta-pop-dialog>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import {
  showToast,
  showPopDialog,
  type PlacementType,
  type PopupOnVisibleStateChange,
  type PopupOnCancel
} from '@/index'

export default defineComponent({
  name: 'ExpPopDialog',
  setup() {
    const visible = ref(false)
    const content = ref('确定要删除该条数据?')
    const showCancel = ref(true)
    const selector = ref('')
    const confirmText = ref('确定')
    const cancelText = ref('取消')
    const placement2 = ref<PlacementType>('bottom')
    const selector2 = ref('')
    const visible2 = ref(false)
    const visible3 = ref(false)
    const showEvent = ref(false)
    const visibleEvent = ref(false)

    const onVisibleStateChange: PopupOnVisibleStateChange = res => {
      if (visibleEvent.value) {
        console.log('visible-state-change', res)
        showToast(`${res.state} 事件触发`)
      }
      if (res.state === 'hidden') {
        showCancel.value = true
        visibleEvent.value = false
        showEvent.value = false
        content.value = '确定要删除该条数据?'
        confirmText.value = '确定'
        cancelText.value = '取消'
      }
    }

    const onCancel: PopupOnCancel = res => {
      if (showEvent.value) {
        console.log('cancel', res)
        showToast(`取消事件触发`)
      }
    }

    const onConfirm = (res: any) => {
      if (showEvent.value) {
        console.log('confirm', res)
        showToast(`确定事件触发`)
      }
    }

    function onCallApi(selector: string) {
      showPopDialog({
        selector,
        placement: 'top',
        content: content.value,
        success: res => {
          console.log('success', res)
          showToast(res.confirm ? `点击了确定` : `点击了取消`)
        }
      })
    }

    return {
      visible,
      content,
      showCancel,
      selector,
      confirmText,
      cancelText,

      placement2,
      visible2,
      selector2,

      visible3,

      showEvent,
      visibleEvent,

      onVisibleStateChange,
      onConfirm,
      onCancel,
      onCallApi
    }
  }
})
</script>

Import

js
import { TaPopDialog } from 'tantalum-ui-mobile'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

ts
import type { VisibleState, PopupOnVisibleStateChange, PopupOnCancel } from 'tantalum-ui-mobile'

Props

属性类型默认值必填说明
v-model:visiblebooleanfalse是否显示
selectorstring | HTMLElement从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到
placementPlacementType'bottom'展开位置,可选 'bottom', 'top', 'left', 'right'
show-maskbooleantrue是否展示蒙层,如果设置不展示,气泡则是跟随 selector 对应的元素
contentstring文本内容
show-cancelbooleantrue是否显示取消按钮
cancel-textstring'取消'取消按钮的文本
confirm-textstring'确定'确定按钮的文本

Events

事件描述回调函数参数TypeScript 函数
confirm确认按钮点击时触发
cancel取消按钮点击时触发payload: { source: string }PopupOnCancel
visible-state-change展示隐藏时触发payload: { state: VisibleState }PopupOnVisibleStateChange

VisibleState 值说明

说明备注
show展示时触发
shown展示且动画结束后触发
hide隐藏时触发可能携带其他参数 cancel, maskClick, closeClick 等
hidden隐藏且动画结束后触发可能携带其他参数 cancel, maskClick, closeClick 等

showPopDialog(object)

显示气泡对话框。

object

属性类型默认值必填说明
selectorstring | HTMLElement从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到
placementPlacementType'bottom'展开位置,可选 'bottom', 'top', 'left', 'right'
contentstring文本内容
showCancelbooleantrue是否显示取消按钮
cancelTextstring'取消'取消按钮的文本
confirmTextstring'确定'确定按钮的文本
success(payload: SuccessPayload) => void接口调用成功(在用户做出选择后,如取消,选择选项)的回调函数
fail(e: Error) => void接口调用失败(如传入错误的参数)的回调函数(不传入 fail 遇错误直接抛出)
complete() => void弹窗关闭或调用失败的回调函数

SuccessPayload

属性类型说明
cancel?boolean为 true 时,表示取消
confirm?boolean为 true 时,表示点击了确定

Usage

具体调用方式可以参考API 调用

js
showPopDialog({
  selector: '#popDialogTarget',
  content: this.content,
  success: res => {
    console.log('success', res)
  }
})