|
| 1 | +<script lang="ts" setup> |
| 2 | +import { h, nextTick, ref } from 'vue' |
| 3 | +import { Icon } from '@vben/components/index' |
| 4 | +import { useMsg } from '@vben/vbencomponents' |
| 5 | +
|
| 6 | +const msg = useMsg() |
| 7 | +
|
| 8 | +const renderIcon = (icon: string) => { |
| 9 | + return () => h(Icon, { class: 'mr-2', icon }) |
| 10 | +} |
| 11 | +
|
| 12 | +const x = ref(0) |
| 13 | +const y = ref(0) |
| 14 | +const showDropdown = ref(false) |
| 15 | +const options = [ |
| 16 | + { |
| 17 | + key: 'New', |
| 18 | + label: 'New', |
| 19 | + icon: renderIcon('bi:plus'), |
| 20 | + children: [ |
| 21 | + { |
| 22 | + key: 'New1-1', |
| 23 | + label: 'New1-1', |
| 24 | + icon: renderIcon('bi:plus'), |
| 25 | + children: [ |
| 26 | + { key: 'New1-1-1', label: 'New1-1-1' }, |
| 27 | + { key: 'New1-2-1', label: 'New1-2-1', disabled: true }, |
| 28 | + ], |
| 29 | + }, |
| 30 | + { key: 'New1-2', label: 'New1-2', icon: renderIcon('bi:plus') }, |
| 31 | + ], |
| 32 | + }, |
| 33 | +] |
| 34 | +
|
| 35 | +const handleSelect = (key: string | number) => { |
| 36 | + showDropdown.value = false |
| 37 | + msg.info(String(key)) |
| 38 | +} |
| 39 | +const handleContextMenu = (e: MouseEvent) => { |
| 40 | + e.preventDefault() |
| 41 | + showDropdown.value = false |
| 42 | + nextTick().then(() => { |
| 43 | + showDropdown.value = true |
| 44 | + x.value = e.clientX |
| 45 | + y.value = e.clientY |
| 46 | + }) |
| 47 | +} |
| 48 | +const onClickoutside = () => { |
| 49 | + msg.info('clickoutside') |
| 50 | + showDropdown.value = false |
| 51 | +} |
| 52 | +</script> |
| 53 | + |
| 54 | +<template> |
| 55 | + <VbenCard title="右键菜单示例"> |
| 56 | + <VbenCard title="Simple"> |
| 57 | + <VbenButton type="primary" @contextmenu="handleContextMenu"> |
| 58 | + Right Click on me |
| 59 | + </VbenButton> |
| 60 | + <VbenDropdown |
| 61 | + placement="bottom-start" |
| 62 | + trigger="manual" |
| 63 | + :x="x" |
| 64 | + :y="y" |
| 65 | + :options="options" |
| 66 | + :show="showDropdown" |
| 67 | + :on-clickoutside="onClickoutside" |
| 68 | + @select="handleSelect" |
| 69 | + /> |
| 70 | + </VbenCard> |
| 71 | + </VbenCard> |
| 72 | +</template> |
0 commit comments