跳到主要内容

ContextMenu 类右键菜单

通过函数弹出一个右键菜单,通常是长按事件弹出的右键菜单

示例

import { Header, ScrollView, TopView, GroupList, showContextMenu, toast } from '@/duxuiExample'
import { View } from '@tarojs/components'

export default TopView.page(function ContextMenuExample() {
return <>
<Header title='ContextMenu' />
<ScrollView>
<GroupList>
<GroupList.Item title='基础用法' desc='点击任意位置弹出菜单'>
<View className='w-full square bg-white'
onClick={async e => {
const touch = e.changedTouches[0]
const item = await showContextMenu({
x: touch.pageX,
y: touch.pageY,
list: ['复制', '粘贴', '删除']
})
toast(`选择了:${item.item}`)
}}
>
</View>
</GroupList.Item>
<GroupList.Item title='长按' desc='长按任意位置弹出菜单'>
<View className='w-full square bg-white'
onLongPress={async e => {
const touch = e.changedTouches[0]
const item = await showContextMenu({
x: touch.pageX,
y: touch.pageY,
list: ['复制', '粘贴', '删除']
})
toast(`选择了:${item.item}`)
}}
>
</View>
</GroupList.Item>
</GroupList>
</ScrollView>
</>
})

参数

showContextMenu(option) 的 option 参数内容如下

x

菜单显示的x坐标

需要真实坐标

类型必填默认值
number

y

菜单显示的y坐标

需要真实坐标

类型必填默认值
number

list

菜单列表

类型必填默认值
string[] | { name: string, children?: list }

支持显示子菜单

const { item } = await showContextMenu({
x: touch.pageX,
y: touch.pageY,
list: [
{ name: '菜单1', callback: () => console.log('点击菜单1') },
{
name: '菜单2', children: [
{ name: '子菜单1', callback: () => console.log('点击子菜单1') },
{ name: '子菜单2', callback: () => console.log('点击子菜单2') }
]
}
]
})

item.callback()

animation

是否开启动画效果

类型必填默认值
booleantrue

oneCallback

只有一个菜单时,是否立即返回这个菜单作为结果,而不经过用户选择

类型必填默认值
booleanfalse

返回值

返回值是异步返回的,返回一个对象

{
// 选中的菜单
index: number
// 选中菜单名称
item: string
}