AuthDropDown.vue
2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<script lang="ts" setup>
import { Dropdown, Menu, Popconfirm } from 'ant-design-vue';
import { computed, useSlots } from 'vue';
import Icon from '../Icon';
import { usePermission } from '/@/hooks/web/usePermission';
export interface AuthDropDownProps {
dropMenuList: AuthDropMenuList[];
trigger?: ('contextmenu' | 'click' | 'hover')[];
}
export interface AuthDropMenuList {
icon?: string;
event: string | number;
text: string;
disabled?: boolean;
divider?: boolean;
auth?: string;
onClick?: Fn;
popconfirm?: {
cancelText?: string;
okText?: string;
okType?: string;
title?: string;
icon?: string;
disabled?: boolean;
onCancel?: Fn;
onConfirm?: Fn;
onVisibleChange?: Fn;
};
}
const props = defineProps<AuthDropDownProps>();
const slot = useSlots();
const { hasPermission } = usePermission();
const getMenuList = computed(() => {
const { dropMenuList } = props;
return dropMenuList.filter((menu) => (menu.auth ? hasPermission(menu.auth) : true));
});
const hasDefaultSlot = computed(() => {
return !!slot.default;
});
</script>
<template>
<Dropdown :trigger="$props.trigger">
<template #overlay>
<Menu v-if="getMenuList.length">
<template v-for="item in getMenuList" :key="item.event">
<Menu.Divider v-if="item.divider" />
<Menu.Item :disabled="item.disabled" v-if="!item.popconfirm" @click="item.onClick">
<span class="flex justify-center items-center">
<Icon :icon="item.icon" />
<span class="ml-2">{{ item.text }}</span>
</span>
</Menu.Item>
<Menu.Item :disabled="item.disabled" v-if="item.popconfirm">
<Popconfirm :disabled="item.disabled" v-bind="item.popconfirm">
<template v-if="item.popconfirm.icon" #icon>
<Icon :icon="item.popconfirm.icon" />
</template>
<span class="flex justify-center items-center">
<Icon :icon="item.icon" />
<span class="ml-2">{{ item.text }}</span>
</span>
</Popconfirm>
</Menu.Item>
</template>
</Menu>
</template>
<Icon
v-if="!hasDefaultSlot"
class="items-center justify-center"
icon="ant-design:ellipsis-outlined"
:class="!getMenuList.length ? '!text-gray-200 !cursor-not-allowed' : ''"
/>
<slot name="default"></slot>
</Dropdown>
</template>