feat: `autoActivateChild` support more layout mode (#5148)
							parent
							
								
									2efb5b71c3
								
							
						
					
					
						commit
						f6faeb034e
					
				|  | @ -103,6 +103,7 @@ const { | ||||||
| 
 | 
 | ||||||
| const { | const { | ||||||
|   handleMenuSelect, |   handleMenuSelect, | ||||||
|  |   handleMenuOpen, | ||||||
|   headerActive, |   headerActive, | ||||||
|   headerMenus, |   headerMenus, | ||||||
|   sidebarActive, |   sidebarActive, | ||||||
|  | @ -260,6 +261,7 @@ const headerSlots = computed(() => { | ||||||
|         :rounded="isMenuRounded" |         :rounded="isMenuRounded" | ||||||
|         :theme="sidebarTheme" |         :theme="sidebarTheme" | ||||||
|         mode="vertical" |         mode="vertical" | ||||||
|  |         @open="handleMenuOpen" | ||||||
|         @select="handleMenuSelect" |         @select="handleMenuSelect" | ||||||
|       /> |       /> | ||||||
|     </template> |     </template> | ||||||
|  |  | ||||||
|  | @ -14,12 +14,17 @@ const props = withDefaults(defineProps<Props>(), { | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| const emit = defineEmits<{ | const emit = defineEmits<{ | ||||||
|  |   open: [string, string[]]; | ||||||
|   select: [string, string?]; |   select: [string, string?]; | ||||||
| }>(); | }>(); | ||||||
| 
 | 
 | ||||||
| function handleMenuSelect(key: string) { | function handleMenuSelect(key: string) { | ||||||
|   emit('select', key, props.mode); |   emit('select', key, props.mode); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | function handleMenuOpen(key: string, path: string[]) { | ||||||
|  |   emit('open', key, path); | ||||||
|  | } | ||||||
| </script> | </script> | ||||||
| 
 | 
 | ||||||
| <template> | <template> | ||||||
|  | @ -32,6 +37,7 @@ function handleMenuSelect(key: string) { | ||||||
|     :mode="mode" |     :mode="mode" | ||||||
|     :rounded="rounded" |     :rounded="rounded" | ||||||
|     :theme="theme" |     :theme="theme" | ||||||
|  |     @open="handleMenuOpen" | ||||||
|     @select="handleMenuSelect" |     @select="handleMenuSelect" | ||||||
|   /> |   /> | ||||||
| </template> | </template> | ||||||
|  |  | ||||||
|  | @ -15,7 +15,8 @@ function useMixedMenu() { | ||||||
|   const route = useRoute(); |   const route = useRoute(); | ||||||
|   const splitSideMenus = ref<MenuRecordRaw[]>([]); |   const splitSideMenus = ref<MenuRecordRaw[]>([]); | ||||||
|   const rootMenuPath = ref<string>(''); |   const rootMenuPath = ref<string>(''); | ||||||
| 
 |   /** 记录当前顶级菜单下哪个子菜单最后激活 */ | ||||||
|  |   const defaultSubMap = new Map<string, string>(); | ||||||
|   const { isMixedNav } = usePreferences(); |   const { isMixedNav } = usePreferences(); | ||||||
| 
 | 
 | ||||||
|   const needSplit = computed( |   const needSplit = computed( | ||||||
|  | @ -86,6 +87,25 @@ function useMixedMenu() { | ||||||
|     splitSideMenus.value = rootMenu?.children ?? []; |     splitSideMenus.value = rootMenu?.children ?? []; | ||||||
|     if (splitSideMenus.value.length === 0) { |     if (splitSideMenus.value.length === 0) { | ||||||
|       navigation(key); |       navigation(key); | ||||||
|  |     } else if (rootMenu && preferences.sidebar.autoActivateChild) { | ||||||
|  |       navigation( | ||||||
|  |         defaultSubMap.has(rootMenu.path) | ||||||
|  |           ? (defaultSubMap.get(rootMenu.path) as string) | ||||||
|  |           : rootMenu.path, | ||||||
|  |       ); | ||||||
|  |     } | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 侧边菜单展开事件 | ||||||
|  |    * @param key 路由路径 | ||||||
|  |    * @param parentsPath 父级路径 | ||||||
|  |    */ | ||||||
|  |   const handleMenuOpen = (key: string, parentsPath: string[]) => { | ||||||
|  |     if (parentsPath.length <= 1 && preferences.sidebar.autoActivateChild) { | ||||||
|  |       navigation( | ||||||
|  |         defaultSubMap.has(key) ? (defaultSubMap.get(key) as string) : key, | ||||||
|  |       ); | ||||||
|     } |     } | ||||||
|   }; |   }; | ||||||
| 
 | 
 | ||||||
|  | @ -107,6 +127,8 @@ function useMixedMenu() { | ||||||
|     (path) => { |     (path) => { | ||||||
|       const currentPath = (route?.meta?.activePath as string) ?? path; |       const currentPath = (route?.meta?.activePath as string) ?? path; | ||||||
|       calcSideMenus(currentPath); |       calcSideMenus(currentPath); | ||||||
|  |       if (rootMenuPath.value) | ||||||
|  |         defaultSubMap.set(rootMenuPath.value, currentPath); | ||||||
|     }, |     }, | ||||||
|     { immediate: true }, |     { immediate: true }, | ||||||
|   ); |   ); | ||||||
|  | @ -118,6 +140,7 @@ function useMixedMenu() { | ||||||
| 
 | 
 | ||||||
|   return { |   return { | ||||||
|     handleMenuSelect, |     handleMenuSelect, | ||||||
|  |     handleMenuOpen, | ||||||
|     headerActive, |     headerActive, | ||||||
|     headerMenus, |     headerMenus, | ||||||
|     sidebarActive, |     sidebarActive, | ||||||
|  |  | ||||||
|  | @ -43,7 +43,11 @@ const sidebarExpandOnHover = defineModel<boolean>('sidebarExpandOnHover'); | ||||||
|   <SwitchItem |   <SwitchItem | ||||||
|     v-model="sidebarAutoActivateChild" |     v-model="sidebarAutoActivateChild" | ||||||
|     :disabled=" |     :disabled=" | ||||||
|       !sidebarEnable || currentLayout !== 'sidebar-mixed-nav' || disabled |       !sidebarEnable || | ||||||
|  |       !['sidebar-mixed-nav', 'mixed-nav', 'sidebar-nav'].includes( | ||||||
|  |         currentLayout as string, | ||||||
|  |       ) || | ||||||
|  |       disabled | ||||||
|     " |     " | ||||||
|     :tip="$t('preferences.sidebar.autoActivateChildTip')" |     :tip="$t('preferences.sidebar.autoActivateChildTip')" | ||||||
|   > |   > | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Netfan
						Netfan