Skip to content

Actionsheet 操作菜单 ​

查看 sn-actionsheet 的 2.0 版本差异

基础用法 ​

从底部弹出的操作菜单,用于展示一组操作项供用户选择。

  • 点击操作项后自动关闭并派发选择事件
  • 内置取消按钮与标题,均可按需隐藏
  • 支持操作项文字颜色与禁用状态

必须放在 sn-page 下

本组件为弹出层,需直接放在 sn-page 根节点下使用,否则主题颜色与动画时长等 CSS 变量无法作用到组件内部。

建议直接使用 API

推荐使用 snu.showActionsheet:无需在页面放置组件或绑定 ref,配置字段与组件 props 一致,结果经 success 回调返回;作为整页弹层可覆盖导航栏 / tabbar 并拦截返回键。组件本身不提供插槽,需先放置到页面再通过 ref 调用。

typescript
snu.showActionsheet({
	title: '请选择',
	actions: [{ text: '拍照' }, { text: '相册' }],
	success: (tapIndex) => {}
})
vue
<template>
	<sn-page>
		<sn-button text="打开操作菜单" type="primary" @click="open"></sn-button>
		<sn-actionsheet ref="sheetRef" title="选择操作" :actions="actions"
			@click-item="onClickItem"></sn-actionsheet>
	</sn-page>
</template>

<script lang="uts" setup>
import type { SnDataItem } from '@/uni_modules/sinle-ui/core/types/index.uts'

const sheetRef = ref<SnActionsheetComponentPublicInstance | null>(null)
const actions = ref<SnDataItem[]>([
	{ text: '选项一' },
	{ text: '选项二' },
	{ text: '选项三' }
])

function open(): void {
	sheetRef.value?.$callMethod('open')
}

function onClickItem(item: SnDataItem): void {
	console.log(item.text)
}
</script>

更多演示请下载 demo 查看

自定义操作项 ​

操作项颜色支持 $ 简写,可单独禁用某一项。

vue
<template>
	<sn-actionsheet ref="sheetRef" title="分享到" :actions="actions"></sn-actionsheet>
</template>

<script lang="uts" setup>
import type { SnDataItem } from '@/uni_modules/sinle-ui/core/types/index.uts'

const actions = ref<SnDataItem[]>([
	{ text: '微信', color: '$success' },
	{ text: '朋友圈', color: '$success' },
	{ text: '删除', color: '$error' },
	{ text: '暂不可用', disabled: true }
])
</script>

属性 ​

参数说明类型默认值可选值
title操作菜单标题,为空则不显示标题与分隔线String''-
actions操作项数组,每项支持 text(文字)、color(文字颜色,支持 $ 简写)、disabled(是否禁用)等字段Array<SnDataItem>[]-
showCancel是否显示底部取消按钮Booleantruetrue | false
titleColor标题颜色,支持 $ 简写String$title-
titleSize标题字号,支持 $ 简写String | Number$13-
bgColor面板背景颜色,支持 $ 简写String$front-
cancelText取消按钮文字String取消-
maskOpacity遮罩透明度Number0.4-
aniTime动画时长(ms),支持 $ 简写String | Number$long-
preventBack是否阻止返回键关闭Booleanfalsetrue | false
disabled是否禁用,禁用后操作项与取消按钮均不可点击Booleanfalsetrue | false
maskClose点击遮罩时是否关闭Booleantruetrue | false
customStyle自定义面板样式,后应用,优先于 panelStyleSnStyleValue''-
wrapStyle自定义外层容器样式SnStyleValue''-
wrapClass外层容器外部类String''-
panelStyle自定义面板样式SnStyleValue''-
panelClass面板外部类String''-
itemStyle自定义操作项样式(透传内部 sn-cell)SnStyleValue''-
itemClass操作项外部类(透传内部 sn-cell)String''-
itemBorder是否显示操作项之间的分隔线Booleantruetrue | false
itemHoverStyle操作项按压时应用的样式SnStyleValuebackground-color: var(--sn-info)-

通用数据项类型,全局弹窗操作菜单与多数列表类组件共用(组件侧详见组件类型)。

字段类型必备描述
idString否项 id
textString否显示文本
iconString否图标名
valueString否关联值
colorString否文本颜色
disabledBoolean否是否禁用

事件 ​

名称类型说明
clickItem(item: SnDataItem) => Void点击操作项时触发,参数为所选操作项,随后自动关闭
cancel() => Void点击取消按钮(或调用 cancel 方法)时触发,随后关闭
clickoverlay() => Void点击遮罩时触发
beforeenter() => Void入场动画开始前触发
enter() => Void入场动画开始时触发
afterenter() => Void入场动画结束后触发
beforeleave() => Void离场动画开始前触发
leave() => Void离场动画开始时触发
afterleave() => Void离场动画结束后触发

方法 ​

名称参数返回值描述
open--打开操作菜单
close--关闭操作菜单
cancel--关闭操作菜单并触发 cancel 事件

使用 MIT 协议