Files
schoolNews/schoolNewsWeb/src/components/base/MenuItem.vue

222 lines
4.6 KiB
Vue
Raw Normal View History

2025-10-07 13:31:06 +08:00
<template>
<div class="menu-item">
<!-- 有子菜单的情况 -->
<template v-if="hasChildren">
<div
class="menu-item-content"
2025-10-27 17:29:25 +08:00
:class="{ 'active': isActive }"
2025-10-07 13:31:06 +08:00
@click="toggleExpanded"
>
<div class="menu-item-inner">
2025-10-28 19:04:35 +08:00
<img
v-if="menu.icon"
:src="String(PUBLIC_IMG_PATH + '/' + menu.icon)"
class="tab-icon"
:alt="String(menu.name || '')"
/>
2025-10-27 17:29:25 +08:00
<span class="menu-title">{{ menu.name }}</span>
<img
src="@/assets/imgs/arrow-down.svg"
alt="arrow"
2025-10-07 13:31:06 +08:00
class="expand-icon"
:class="{ 'expanded': expanded }"
2025-10-27 17:29:25 +08:00
/>
2025-10-07 13:31:06 +08:00
</div>
</div>
<!-- 子菜单 -->
<transition name="submenu">
<div
class="submenu"
2025-10-27 17:29:25 +08:00
v-if="expanded"
2025-10-07 13:31:06 +08:00
>
<MenuItem
2025-10-27 16:21:00 +08:00
v-for="child in filteredChildren"
2025-10-07 13:31:06 +08:00
:key="child.menuID"
:menu="child"
:level="level + 1"
@menu-click="$emit('menu-click', $event)"
/>
</div>
</transition>
</template>
<!-- 没有子菜单的情况 -->
<template v-else>
<div
class="menu-item-content"
2025-10-27 17:29:25 +08:00
:class="{ 'active': isActive }"
2025-10-07 13:31:06 +08:00
@click="handleClick"
>
<div class="menu-item-inner">
2025-10-28 19:04:35 +08:00
<img
v-if="menu.icon"
:src="String(PUBLIC_IMG_PATH + '/' + menu.icon)"
class="tab-icon"
:alt="String(menu.name || '')"
/>
2025-10-27 17:29:25 +08:00
<span class="menu-title">{{ menu.name }}</span>
2025-10-07 13:31:06 +08:00
</div>
</div>
</template>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRoute } from 'vue-router';
import type { SysMenu } from '@/types';
import { MenuType } from '@/types/enums';
2025-10-28 19:04:35 +08:00
import { PUBLIC_IMG_PATH} from '@/config'
2025-10-07 13:31:06 +08:00
// 递归组件需要声明名称Vue 3.5+
defineOptions({
name: 'MenuItem'
});
// Props
interface Props {
menu: SysMenu;
level?: number;
}
const props = withDefaults(defineProps<Props>(), {
level: 0
});
// Emits
const emit = defineEmits<{
'menu-click': [menu: SysMenu];
}>();
2025-10-27 16:21:00 +08:00
// 状态 - 顶层菜单默认展开
const expanded = ref(props.level === 0);
2025-10-07 13:31:06 +08:00
// Composition API
const route = useRoute();
// 计算属性
const hasChildren = computed(() => {
2025-10-27 16:21:00 +08:00
// 只显示SIDEBAR类型的子菜单过滤掉PAGE类型
return props.menu.children &&
props.menu.children.filter((child: SysMenu) => child.type === MenuType.SIDEBAR).length > 0;
});
// 过滤后的子菜单只显示SIDEBAR类型
const filteredChildren = computed(() => {
if (!props.menu.children) return [];
return props.menu.children.filter((child: SysMenu) => child.type === MenuType.SIDEBAR);
2025-10-07 13:31:06 +08:00
});
const isActive = computed(() => {
// 检查当前路由是否匹配此菜单
return route.path === props.menu.url;
});
// 方法 - 使用 function 声明
function toggleExpanded() {
if (hasChildren.value) {
expanded.value = !expanded.value;
}
}
function handleClick() {
2025-10-27 16:21:00 +08:00
// 支持NAVIGATION和SIDEBAR类型的菜单点击
if (props.menu.url && (props.menu.type === MenuType.NAVIGATION || props.menu.type === MenuType.SIDEBAR)) {
2025-10-07 13:31:06 +08:00
emit('menu-click', props.menu);
}
}
</script>
<style lang="scss" scoped>
.menu-item {
2025-10-27 17:29:25 +08:00
margin-bottom: 4px;
2025-10-07 13:31:06 +08:00
}
.menu-item-content {
position: relative;
2025-10-27 17:29:25 +08:00
border-radius: 8px;
2025-10-07 13:31:06 +08:00
cursor: pointer;
transition: all 0.2s ease;
2025-10-27 17:29:25 +08:00
height: 36px;
2025-10-07 13:31:06 +08:00
&:hover {
2025-10-27 17:29:25 +08:00
background-color: rgba(231, 0, 11, 0.05);
2025-10-07 13:31:06 +08:00
}
&.active {
2025-10-27 17:29:25 +08:00
background-color: #E7000B;
2025-10-07 13:31:06 +08:00
.menu-item-inner {
2025-10-27 17:29:25 +08:00
color: #FFFFFF;
2025-10-07 13:31:06 +08:00
}
}
}
.menu-item-inner {
display: flex;
align-items: center;
2025-10-27 17:29:25 +08:00
height: 100%;
padding: 0 12px;
color: #0A0A0A;
2025-10-07 13:31:06 +08:00
font-size: 14px;
2025-10-27 17:29:25 +08:00
font-weight: 500;
line-height: 1.43;
2025-10-07 13:31:06 +08:00
}
2025-10-28 19:04:35 +08:00
.tab-icon {
2025-10-07 13:31:06 +08:00
width: 16px;
2025-10-28 19:04:35 +08:00
height: 16px;
2025-10-07 13:31:06 +08:00
margin-right: 12px;
2025-10-28 19:04:35 +08:00
flex-shrink: 0;
object-fit: contain;
2025-10-07 13:31:06 +08:00
}
.menu-title {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.expand-icon {
2025-10-27 17:29:25 +08:00
width: 12px;
height: 12px;
transition: transform 0.3s ease;
2025-10-07 13:31:06 +08:00
margin-left: auto;
2025-10-27 17:29:25 +08:00
flex-shrink: 0;
2025-10-07 13:31:06 +08:00
&.expanded {
2025-10-27 17:29:25 +08:00
transform: rotate(180deg);
2025-10-07 13:31:06 +08:00
}
}
.submenu {
margin: 4px 0;
overflow: hidden;
2025-10-27 17:29:25 +08:00
.menu-item {
margin-bottom: 2px;
}
2025-10-07 13:31:06 +08:00
.menu-item-content {
.menu-item-inner {
2025-10-27 17:29:25 +08:00
padding-left: 40px;
font-size: 14px;
font-weight: 400;
2025-10-07 13:31:06 +08:00
}
}
}
/* 动画效果 */
.submenu-enter-active,
.submenu-leave-active {
transition: all 0.3s ease;
}
.submenu-enter-from,
.submenu-leave-to {
opacity: 0;
transform: translateY(-10px);
}
</style>