fix: layout error

This commit is contained in:
vben
2024-06-09 15:39:11 +08:00
parent 35c3dd78ec
commit 640ad6d9e7
58 changed files with 678 additions and 679 deletions

View File

@@ -0,0 +1,88 @@
import { describe, expect, it } from 'vitest';
import { findMenuByPath, findRootMenuByPath } from './find-menu-by-path';
// 示例菜单数据
const menus: any[] = [
{ path: '/', children: [] },
{ path: '/about', children: [] },
{
path: '/contact',
children: [
{ path: '/contact/email', children: [] },
{ path: '/contact/phone', children: [] },
],
},
{
path: '/services',
children: [
{ path: '/services/design', children: [] },
{
path: '/services/development',
children: [{ path: '/services/development/web', children: [] }],
},
],
},
];
describe('menu Finder Tests', () => {
it('finds a top-level menu', () => {
const menu = findMenuByPath(menus, '/about');
expect(menu).toBeDefined();
expect(menu?.path).toBe('/about');
});
it('finds a nested menu', () => {
const menu = findMenuByPath(menus, '/services/development/web');
expect(menu).toBeDefined();
expect(menu?.path).toBe('/services/development/web');
});
it('returns null for a non-existent path', () => {
const menu = findMenuByPath(menus, '/non-existent');
expect(menu).toBeNull();
});
it('handles empty menus list', () => {
const menu = findMenuByPath([], '/about');
expect(menu).toBeNull();
});
it('handles menu items without children', () => {
const menu = findMenuByPath(
[{ path: '/only', children: undefined }] as any[],
'/only',
);
expect(menu).toBeDefined();
expect(menu?.path).toBe('/only');
});
it('finds root menu by path', () => {
const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
menus,
'/services/development/web',
);
expect(findMenu).toBeDefined();
expect(rootMenu).toBeUndefined();
expect(rootMenuPath).toBeUndefined();
expect(findMenu?.path).toBe('/services/development/web');
});
it('returns null for undefined or empty path', () => {
const menuUndefinedPath = findMenuByPath(menus);
const menuEmptyPath = findMenuByPath(menus, '');
expect(menuUndefinedPath).toBeNull();
expect(menuEmptyPath).toBeNull();
});
it('checks for root menu when path does not exist', () => {
const { findMenu, rootMenu, rootMenuPath } = findRootMenuByPath(
menus,
'/non-existent',
);
expect(findMenu).toBeNull();
expect(rootMenu).toBeUndefined();
expect(rootMenuPath).toBeUndefined();
});
});

View File

@@ -0,0 +1,35 @@
import type { MenuRecordRaw } from '@vben-core/typings';
function findMenuByPath(
list: MenuRecordRaw[],
path?: string,
): MenuRecordRaw | null {
for (const menu of list) {
if (menu.path === path) {
return menu;
}
const findMenu = menu.children && findMenuByPath(menu.children, path);
if (findMenu) {
return findMenu;
}
}
return null;
}
/**
* 查找根菜单
* @param menus
* @param path
*/
function findRootMenuByPath(menus: MenuRecordRaw[], path?: string) {
const findMenu = findMenuByPath(menus, path);
const rootMenuPath = findMenu?.parents?.[0];
const rootMenu = menus.find((item) => item.path === rootMenuPath);
return {
findMenu,
rootMenu,
rootMenuPath,
};
}
export { findMenuByPath, findRootMenuByPath };

View File

@@ -1,3 +1,4 @@
export * from './find-menu-by-path';
export * from './flatten-object';
export * from './generator-menus';
export * from './generator-routes';

View File

@@ -36,7 +36,7 @@
"@vben-core/helpers": "workspace:*",
"@vben-core/toolkit": "workspace:*",
"@vben-core/typings": "workspace:*",
"@vueuse/core": "^10.10.0",
"@vueuse/core": "^10.10.1",
"vue": "3.4.27"
}
}

View File

@@ -12,7 +12,7 @@ const defaultPreferences: Preferences = {
'https://cdn.jsdelivr.net/npm/@vbenjs/static-source@0.1.0/source/avatar-v1.webp',
dynamicTitle: true,
isMobile: false,
layout: 'side-nav',
layout: 'sidebar-nav',
locale: 'zh-CN',
name: 'Vben Admin Pro',
semiDarkMenu: true,
@@ -47,8 +47,8 @@ const defaultPreferences: Preferences = {
},
shortcutKeys: { enable: true },
sidebar: {
collapse: false,
collapseShowTitle: true,
collapsed: false,
collapsedShowTitle: true,
enable: true,
expandOnHover: true,
extraCollapse: true,

View File

@@ -94,9 +94,9 @@ interface NavigationPreferences {
interface SidebarPreferences {
/** 侧边栏是否折叠 */
collapse: boolean;
collapsed: boolean;
/** 侧边栏折叠时是否显示title */
collapseShowTitle: boolean;
collapsedShowTitle: boolean;
/** 侧边栏是否可见 */
enable: boolean;
/** 菜单自动展开状态 */

View File

@@ -33,7 +33,7 @@ function usePreferences() {
* @zh_CN 布局方式
*/
const layout = computed(() =>
appPreferences.value.isMobile ? 'side-nav' : appPreferences.value.layout,
appPreferences.value.isMobile ? 'sidebar-nav' : appPreferences.value.layout,
);
/**
@@ -46,13 +46,15 @@ function usePreferences() {
/**
* @zh_CN 是否侧边导航模式
*/
const isSideNav = computed(() => appPreferences.value.layout === 'side-nav');
const isSideNav = computed(
() => appPreferences.value.layout === 'sidebar-nav',
);
/**
* @zh_CN 是否侧边混合模式
*/
const isSideMixedNav = computed(
() => appPreferences.value.layout === 'side-mixed-nav',
() => appPreferences.value.layout === 'sidebar-mixed-nav',
);
/**