ActionSheet 动作面板
从底部列出可选操作,支持禁用、加载与关闭拦截。
引入
uni-app 配置 easycom 后使用 <hui-action-sheet>;公开类型从 @nscode/handy-uni 导入。普通 Vue 3 项目安装 @nscode/handy-uni/web 预览版插件后使用同名组件。
示例
操作控件后,页面直接显示当前值和事件结果。
vue
<template>
<view>
<button @click="visible = true">操作 / Actions</button
><hui-action-sheet
ref="control"
v-model:visible="visible"
title="选择操作 / Choose action"
:items="items"
cancel-text="取消 / Cancel"
@update:visible="event0"
@select="event1"
@cancel="event2"
@close="event3"
@open="event4"
@opened="event5"
@closed="event6"
/>
<hui-button @click="() => control?.open()">open()</hui-button>
<hui-button @click="() => control?.close('api')">close()</hui-button>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const control = ref()
const visible = ref(false)
const items = [
{ label: '编辑 / Edit', value: 'edit' },
{ label: '只读项 / Disabled', value: 'disabled', disabled: true },
{ label: '处理中 / Working', value: 'busy', loading: true }
]
const event0 = (arg0: unknown) => {
lastEvent.value = 'update:visible' + ': ' + [describe(arg0)].join(', ')
}
const event1 = (arg0: unknown, arg1: unknown) => {
lastEvent.value = 'select' + ': ' + [describe(arg0), describe(arg1)].join(', ')
}
const event2 = () => {
lastEvent.value = 'cancel'
}
const event3 = (arg0: unknown) => {
lastEvent.value = 'close' + ': ' + [describe(arg0)].join(', ')
}
const event4 = () => {
lastEvent.value = 'open'
}
const event5 = () => {
lastEvent.value = 'opened'
}
const event6 = () => {
lastEvent.value = 'closed'
}
</script>
select 的第二个参数含 index。禁用或加载中的项不触发选择。closeOnClickAction=false 可保留面板,由业务决定何时关闭。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
visible | boolean | undefined | v-model:visible |
defaultVisible | boolean | false | v-model:visible |
items | ActionSheetItem[] | [] | 动作列表(总体设计 9.6:菜单类叫 items) |
title | string | — | 标题。 |
description | string | — | 标题下的说明 |
cancelText | string | — | 默认 t('common.cancel');传 '' 不显示取消 |
closeOnClickAction | boolean | true | 点了动作后自动关 |
closeOnClickOverlay | boolean | true | 点击遮罩后请求关闭,仍执行关闭拦截。 |
beforeClose | ((reason: ActionSheetCloseReason) => unknown) | — | 关闭前钩子 |
safeAreaInsetBottom | boolean | true | 预留底部安全区。 |
zIndex | number | — | 弹层层号;未传时由本页层栈分配。 |
rootPortal | boolean | false | 尝试挂到页面根层;能力依平台而异。 |
根节点属性见通用属性。布尔默认值为 undefined 时支持继承;显式 false 会覆盖继承值。其他布尔值按表中默认值处理。
Events
| 事件 | 参数 | 说明 |
|---|---|---|
update:visible | (value: boolean) | |
select | (item: ActionSheetItem, ctx: { index: number }) | 点了可用的动作;在关闭之前发 |
cancel | () | 点了取消 |
close | (reason: ActionSheetCloseReason) | |
open | () | |
opened | () | |
closed | () |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
title | — | 自定义 title 区域,覆盖默认内容。 |
description | — | 自定义 description 区域,覆盖默认内容。 |
default | — | 主体内容。 |
item | { item, index } | 自定义 item 区域,覆盖默认内容。 |
cancel | — | 自定义 cancel 区域,覆盖默认内容。 |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
open | (): void | 打开非受控面板;受控时发更新请求。 |
close | (reason?: ActionSheetCloseReason | undefined): Promise<boolean> | 经前置钩子请求关闭。 |
类型
ActionSheetProps、ActionSheetItem、ActionSheetCloseReason、ActionSheetExpose、ActionSheetEmits 可从 @nscode/handy-uni 导入。
ts
export interface ActionSheetItem {
label: string
value?: string | number
description?: string
color?: string
disabled?: boolean
loading?: boolean
}
export type ActionSheetCloseReason = PopupCloseReason | 'cancel' | 'select'
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 基本交互 | 已实现,待设备复核 | 已实现,待设备复核 | 已实现,待设备复核 | 由 Web 平台适配层实现 |
设备操作见冒烟清单。真实设备读数单独记录,单元测试通过不代表真机验收。