Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增菜单自动折叠 #744

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default {
showFooter: true,
isSidebarCompact: false,
showBreadcrumb: false,
menuAutoCollapsed: false,
mode: 'light',
layout: 'side',
splitMenu: false,
Expand Down
44 changes: 34 additions & 10 deletions src/layouts/components/SideNav.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<template>
<div :class="sideNavCls">
<t-menu :class="menuCls" :theme="theme" :value="active" :collapsed="collapsed" :default-expanded="defaultExpanded">
<t-menu
:class="menuCls"
:theme="theme"
:value="active"
:collapsed="collapsed"
:expanded="expanded"
:expand-mutex="menuAutoCollapsed"
@expand="onExpanded"
>
<template #logo>
<span v-if="showLogo" :class="`${prefix}-side-nav-logo-wrapper`" @click="goHome">
<component :is="getLogo()" :class="logoCls" />
Expand All @@ -16,15 +24,16 @@
</template>

<script setup lang="ts">
import union from 'lodash/union';
import { difference, remove, union } from 'lodash';
import { MenuValue } from 'tdesign-vue-next';
import type { PropType } from 'vue';
import { computed, onMounted } from 'vue';
import { computed, onMounted, ref, watch } from 'vue';
import { useRouter } from 'vue-router';

import AssetLogoFull from '@/assets/assets-logo-full.svg?component';
import AssetLogo from '@/assets/assets-t-logo.svg?component';
import { prefix } from '@/config/global';
import { getActive, getRoutesExpanded } from '@/router';
import { getActive } from '@/router';
import { useSettingStore } from '@/store';
import type { MenuRoute, ModeType } from '@/types/interface';

Expand Down Expand Up @@ -65,15 +74,27 @@ const props = defineProps({
});

const collapsed = computed(() => useSettingStore().isSidebarCompact);
const menuAutoCollapsed = computed(() => useSettingStore().menuAutoCollapsed);

const active = computed(() => getActive());

const defaultExpanded = computed(() => {
const path = getActive();
const parentPath = path.substring(0, path.lastIndexOf('/'));
const expanded = getRoutesExpanded();
return union(expanded, parentPath === '' ? [] : [parentPath]);
});
const expanded = ref<MenuValue[]>([]);

watch(
() => active.value,
() => {
const path = getActive();
const parentPath = path.substring(0, path.lastIndexOf('/'));
expanded.value = menuAutoCollapsed.value ? [parentPath] : union([parentPath], expanded.value);
},
);

const onExpanded = (value: MenuValue[]) => {
const currentOperationMenu = difference(expanded.value, value);
const allExpanded = union(value, expanded.value);
remove(allExpanded, (item) => currentOperationMenu.includes(item));
expanded.value = allExpanded;
};

const sideMode = computed(() => {
const { theme } = props;
Expand Down Expand Up @@ -127,6 +148,9 @@ const autoCollapsed = () => {
};

onMounted(() => {
const path = getActive();
const parentPath = path.substring(0, path.lastIndexOf('/'));
expanded.value = union([parentPath], expanded.value);
autoCollapsed();
window.onresize = () => {
autoCollapsed();
Expand Down
3 changes: 3 additions & 0 deletions src/layouts/setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
<t-form-item :label="$t('layout.setting.element.useTagTabs')" name="isUseTabsRouter">
<t-switch v-model="formData.isUseTabsRouter"></t-switch>
</t-form-item>
<t-form-item :label="$t('layout.setting.element.menuAutoCollapsed')" name="menuAutoCollapsed">
<t-switch v-model="formData.menuAutoCollapsed"></t-switch>
</t-form-item>
</t-form>
<div class="setting-info">
<p>{{ $t('layout.setting.tips') }}</p>
Expand Down
1 change: 1 addition & 0 deletions src/locales/lang/en_US/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
showBreadcrumb: 'Show Breadcrumb',
showFooter: 'Show Footer',
useTagTabs: 'Use Tag Tabs',
menuAutoCollapsed: 'Menu Auto Collapsed',
},
tips: 'Please copy and manually modify the configuration file: /src/config/style.ts',
copy: {
Expand Down
1 change: 1 addition & 0 deletions src/locales/lang/zh_CN/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
showBreadcrumb: '显示面包屑',
showFooter: '显示页脚',
useTagTabs: '展示多标签Tab页',
menuAutoCollapsed: '菜单自动折叠',
},
tips: '请复制后手动修改配置文件: /src/config/style.ts',
copy: {
Expand Down
4 changes: 4 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export function mapModuleRouterList(modules: Record<string, unknown>): Array<Rou
return routerList;
}

/**
*
* @deprecated 未使用
*/
export const getRoutesExpanded = () => {
const expandedRoutes: Array<string> = [];

Expand Down
Loading