Popover 气泡
vue
<template>
<ta-group title="基础用法">
<div class="exp-popover-box">
<ta-button
size="small"
id="popoverLeft"
shape="circle"
icon="PlusOutlined"
@click=";(selector = '#popoverLeft'), (visible = true)"
>
左
</ta-button>
<ta-button
size="small"
id="popoverCenter"
shape="circle"
icon="PlusOutlined"
@click=";(selector = '#popoverCenter'), (visible = true)"
>
中
</ta-button>
<ta-button
size="small"
id="popoverRight"
shape="circle"
icon="PlusOutlined"
@click=";(selector = '#popoverRight'), (visible = true)"
>
右
</ta-button>
</div>
</ta-group>
<ta-group title="方向 placement=top/bottom/left/right">
<div class="exp-popover-box2">
<div>
<ta-button
size="small"
id="popoverTop2"
shape="circle"
icon="UpOutlined"
@click=";(placement = 'top'), (selector = '#popoverTop2'), (visible = true)"
>
上
</ta-button>
</div>
<div>
<ta-button
size="small"
id="popoverLeft2"
shape="circle"
icon="LeftOutlined"
@click=";(placement = 'left'), (selector = '#popoverLeft2'), (visible = true)"
>
左
</ta-button>
<ta-button
class="exp-popover-box2-ml"
size="small"
id="popoverRight2"
shape="circle"
icon="RightOutlined"
@click=";(placement = 'right'), (selector = '#popoverRight2'), (visible = true)"
>
右
</ta-button>
</div>
<div>
<ta-button
size="small"
id="popoverBottom2"
shape="circle"
icon="DownOutlined"
@click=";(placement = 'bottom'), (selector = '#popoverBottom2'), (visible = true)"
>
下
</ta-button>
</div>
</div>
</ta-group>
<ta-group title="带选项">
<ta-cell label="长文案">
<ta-button
size="small"
id="popoverLongContent"
shape="circle"
icon="PlusOutlined"
@click="
;(selector = '#popoverLongContent'),
(content = '这是气泡内容这是气泡内容这是气泡内容这是气泡内容这是气泡内容这是气泡内容'),
(visible = true)
"
>
</ta-button>
</ta-cell>
<ta-cell label="不展示蒙层">
<ta-button
size="small"
id="popoverNoMask"
shape="circle"
icon="PlusOutlined"
@click="onShowNoMask"
>
</ta-button>
</ta-cell>
</ta-group>
<ta-group title="事件监听">
<ta-cell label="visible-state-change">
<ta-button
size="small"
id="popoverEvent"
shape="circle"
icon="PlusOutlined"
@click=";(selector = '#popoverEvent'), (visibleEvent = true), (visible = true)"
>
</ta-button>
</ta-cell>
</ta-group>
<ta-group title="API">
<ta-cell label="showPopover">
<ta-button
size="small"
id="popoverApi"
shape="circle"
icon="PlusOutlined"
@click="onCallApi('#popoverApi')"
>
</ta-button>
</ta-cell>
</ta-group>
<ta-popover
v-model:visible="visible"
:selector="selector"
:placement="placement"
:content="content"
@visibleStateChange="onVisibleStateChange"
>
</ta-popover>
<ta-popover
v-model:visible="noMaskVisible"
selector="#popoverNoMask"
:showMask="false"
content="无蒙层气泡内容"
>
</ta-popover>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { showToast, showPopover, type PlacementType, type PopupOnVisibleStateChange } from '@/index'
export default defineComponent({
name: 'ExpPopover',
setup() {
const visible = ref(false)
const noMaskVisible = ref(false)
const visibleEvent = ref(false)
const selector = ref('')
const content = ref('这是气泡内容')
const placement = ref<PlacementType>('bottom')
function onShowNoMask() {
noMaskVisible.value = true
setTimeout(() => {
noMaskVisible.value = false
}, 5000)
}
const onVisibleStateChange: PopupOnVisibleStateChange = res => {
if (visibleEvent.value) {
console.log('visible-state-change', res)
showToast(`${res.state} 事件触发`)
}
if (res.state === 'hidden') {
selector.value = ''
placement.value = 'bottom'
content.value = '这是气泡内容'
visibleEvent.value = false
}
}
function onCallApi(selector: string) {
showPopover({
selector,
content: '这是气泡内容',
placement: 'top',
success: res => {
console.log('success', res)
}
})
}
return {
visible,
noMaskVisible,
visibleEvent,
selector,
content,
placement,
onShowNoMask,
onVisibleStateChange,
onCallApi
}
}
})
</script>
Import
js
import { TaPopover } from 'tantalum-ui-mobile'
具体的引入方式可以参考引入组件。
Import Type
组件导出的类型定义:
ts
import type { VisibleState, PopupOnVisibleStateChange } 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 | 否 | 展示文本,如设置了 slot ,则不使用该字段 |
Events
事件 | 描述 | 回调函数参数 | TypeScript 函数 |
---|---|---|---|
visible-state-change | 展示隐藏时触发 | payload: { state: VisibleState } | PopupOnVisibleStateChange |
VisibleState 值说明
值 | 说明 | 备注 |
---|---|---|
show | 展示时触发 | |
shown | 展示且动画结束后触发 | |
hide | 隐藏时触发 | 可能携带其他参数 cancel, maskClick, closeClick 等 |
hidden | 隐藏且动画结束后触发 | 可能携带其他参数 cancel, maskClick, closeClick 等 |
Slots
#default
vue
<ta-popover>自定义内容</ta-popover>
showPopover(object)
显示气泡。
object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
selector | string | HTMLElement | 是 | 从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到 | |
placement | PlacementType | 'bottom' | 否 | 展开位置,可选 'bottom', 'top', 'left', 'right' |
content | string | 是 | 文本内容 | |
showMask | boolean | true | 否 | 是否展示蒙层,如果设置不展示,气泡则是跟随 selector 对应的元素 |
success | () => void | 否 | 接口调用成功的回调函数 | |
fail | (e: Error) => void | 否 | 接口调用失败的回调函数(不传入 fail 遇错误直接抛出) | |
complete | () => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Usage
具体调用方式可以参考API 调用。
js
showPopover({
selector: '#popoverTarget',
content: '这是气泡内容',
success: res => {
console.log('success', res)
}
})