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:visible | boolean | false | 否 | 是否显示 | 
| selector | string | HTMLElement | 是 | 从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到 | |
| placement | PlacementType | 'bottom' | 否 | 展开位置,可选 'bottom', 'top', 'left', 'right' | 
| show-mask | boolean | true | 否 | 是否展示蒙层,如果设置不展示,气泡则是跟随 selector对应的元素 | 
| content | string | 是 | 文本内容 | |
| show-cancel | boolean | true | 否 | 是否显示取消按钮 | 
| cancel-text | string | '取消' | 否 | 取消按钮的文本 | 
| confirm-text | string | '确定' | 否 | 确定按钮的文本 | 
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
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| selector | string | HTMLElement | 是 | 从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到 | |
| placement | PlacementType | 'bottom' | 否 | 展开位置,可选 'bottom', 'top', 'left', 'right' | 
| content | string | 是 | 文本内容 | |
| showCancel | boolean | true | 否 | 是否显示取消按钮 | 
| cancelText | string | '取消' | 否 | 取消按钮的文本 | 
| confirmText | string | '确定' | 否 | 确定按钮的文本 | 
| 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)
  }
})