mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 17:09:19 +08:00
feat: 新增右键菜单二次封装 系统菜单和权限使用
This commit is contained in:
+129
@@ -0,0 +1,129 @@
|
||||
<script lang="ts" setup>
|
||||
import type { VxeRowContextMenuItem } from './types';
|
||||
|
||||
import { computed, h, onBeforeUnmount, watch } from 'vue';
|
||||
|
||||
import { Menu } from 'ant-design-vue';
|
||||
|
||||
defineOptions({ name: 'VxeRowContextMenu' });
|
||||
|
||||
const props = defineProps<{
|
||||
open: boolean;
|
||||
x: number;
|
||||
y: number;
|
||||
items: VxeRowContextMenuItem[];
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:open': [value: boolean];
|
||||
select: [key: string];
|
||||
}>();
|
||||
|
||||
const menuItems = computed(() =>
|
||||
props.items.map((item) => {
|
||||
if (item.divider) {
|
||||
return { type: 'divider' as const, key: item.key };
|
||||
}
|
||||
return {
|
||||
key: item.key,
|
||||
label: item.label,
|
||||
danger: item.danger,
|
||||
disabled: item.disabled,
|
||||
icon: item.icon
|
||||
? () => h(item.icon!, { class: 'size-4 shrink-0' })
|
||||
: undefined,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
const panelStyle = computed(() => ({
|
||||
left: `${props.x}px`,
|
||||
top: `${props.y}px`,
|
||||
}));
|
||||
|
||||
function close() {
|
||||
emit('update:open', false);
|
||||
}
|
||||
|
||||
function onMenuClick(info: { key: string | number }) {
|
||||
emit('select', String(info.key));
|
||||
close();
|
||||
}
|
||||
|
||||
function onKeydown(e: KeyboardEvent) {
|
||||
if (e.key === 'Escape' && props.open) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.open,
|
||||
(open) => {
|
||||
if (open) {
|
||||
document.addEventListener('keydown', onKeydown);
|
||||
} else {
|
||||
document.removeEventListener('keydown', onKeydown);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
document.removeEventListener('keydown', onKeydown);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Teleport to="body">
|
||||
<div
|
||||
v-if="open"
|
||||
class="fixed inset-0 z-[1040]"
|
||||
@mousedown="close"
|
||||
@contextmenu.prevent="close"
|
||||
/>
|
||||
<div
|
||||
v-if="open"
|
||||
class="vxe-row-ctx-panel fixed z-[1050] min-w-[160px] overflow-hidden rounded-lg bg-white py-1 shadow-[0_6px_16px_0_rgba(0,0,0,0.08),0_3px_6px_-4px_rgba(0,0,0,0.12),0_9px_28px_8px_rgba(0,0,0,0.05)] dark:bg-[hsl(222.34deg_10.43%_14%)] dark:shadow-[0_6px_16px_0_rgba(0,0,0,0.4)]"
|
||||
:style="panelStyle"
|
||||
@mousedown.stop
|
||||
>
|
||||
<Menu
|
||||
class="vxe-row-ctx-menu !border-0 !bg-transparent !shadow-none"
|
||||
:items="menuItems"
|
||||
@click="onMenuClick"
|
||||
/>
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
:deep(.vxe-row-ctx-menu.ant-menu-vertical) {
|
||||
border-inline-end: none !important;
|
||||
}
|
||||
|
||||
:deep(.vxe-row-ctx-menu .ant-menu-item) {
|
||||
margin-block: 2px;
|
||||
margin-inline: 4px;
|
||||
width: calc(100% - 8px);
|
||||
height: 32px;
|
||||
line-height: 32px;
|
||||
border-radius: 6px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
:deep(.vxe-row-ctx-menu .ant-menu-item .anticon),
|
||||
:deep(.vxe-row-ctx-menu .ant-menu-item .ant-menu-item-icon) {
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
:deep(.vxe-row-ctx-menu .ant-menu-item-danger) {
|
||||
color: #ff4d4f;
|
||||
}
|
||||
|
||||
:deep(.vxe-row-ctx-menu .ant-menu-item-danger:hover),
|
||||
:deep(.vxe-row-ctx-menu .ant-menu-item-danger-selected) {
|
||||
color: #ff4d4f !important;
|
||||
background: #fff2f0 !important;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,6 @@
|
||||
export { default as VxeRowContextMenu } from './VxeRowContextMenu.vue';
|
||||
export type {
|
||||
VxeRowContextMenuItem,
|
||||
VxeRowContextMenuItemsFactory,
|
||||
} from './types';
|
||||
export { useVxeRowContextMenu } from './use-vxe-row-context-menu';
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { Component } from 'vue';
|
||||
|
||||
/** 表格行右键菜单项(Ant Design Menu 风格) */
|
||||
export interface VxeRowContextMenuItem {
|
||||
key: string;
|
||||
label: string;
|
||||
icon?: Component;
|
||||
danger?: boolean;
|
||||
disabled?: boolean;
|
||||
divider?: boolean;
|
||||
}
|
||||
|
||||
export type VxeRowContextMenuItemsFactory<T = any> = (
|
||||
row: T,
|
||||
) => VxeRowContextMenuItem[];
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
import type {
|
||||
VxeRowContextMenuItem,
|
||||
VxeRowContextMenuItemsFactory,
|
||||
} from './types';
|
||||
|
||||
import { ref } from 'vue';
|
||||
|
||||
/**
|
||||
* 配合 VxeGrid `cell-menu`:拦截 Vxe 默认右键面板,弹出 Ant Design 风格菜单。
|
||||
* gridOptions.menuConfig 需 enabled + visibleMethod: () => false。
|
||||
*/
|
||||
export function useVxeRowContextMenu<T = any>(
|
||||
itemsFactory: VxeRowContextMenuItemsFactory<T>,
|
||||
) {
|
||||
const open = ref(false);
|
||||
const x = ref(0);
|
||||
const y = ref(0);
|
||||
const row = ref<T | null>(null);
|
||||
const items = ref<VxeRowContextMenuItem[]>([]);
|
||||
|
||||
function close() {
|
||||
open.value = false;
|
||||
row.value = null;
|
||||
items.value = [];
|
||||
}
|
||||
|
||||
function clamp(clientX: number, clientY: number, itemCount: number) {
|
||||
const menuW = 168;
|
||||
const menuH = Math.max(40, itemCount * 36 + 12);
|
||||
return {
|
||||
x: Math.max(8, Math.min(clientX, window.innerWidth - menuW - 8)),
|
||||
y: Math.max(8, Math.min(clientY, window.innerHeight - menuH - 8)),
|
||||
};
|
||||
}
|
||||
|
||||
/** 绑到 gridEvents.cellMenu */
|
||||
function onCellMenu(params: { row?: T; $event?: MouseEvent }) {
|
||||
const e = params.$event;
|
||||
if (e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
if (!params.row) {
|
||||
close();
|
||||
return;
|
||||
}
|
||||
const nextItems = itemsFactory(params.row) ?? [];
|
||||
const pos = clamp(e?.clientX ?? 0, e?.clientY ?? 0, nextItems.length);
|
||||
row.value = params.row;
|
||||
items.value = nextItems;
|
||||
x.value = pos.x;
|
||||
y.value = pos.y;
|
||||
// 先关再开,保证连续右键不同行时位置/内容刷新
|
||||
open.value = false;
|
||||
requestAnimationFrame(() => {
|
||||
open.value = true;
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
open,
|
||||
x,
|
||||
y,
|
||||
row,
|
||||
items,
|
||||
close,
|
||||
onCellMenu,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user