houzhongjian
2024-12-13 eeddc808a8d6428bfd1c2d6e21e4a71f5e9bdbef
提交 | 用户 | 时间
820397 1 import { ElSubMenu, ElMenuItem } from 'element-plus'
H 2 import { hasOneShowingChild } from '../helper'
3 import { isUrl } from '@/utils/is'
4 import { useRenderMenuTitle } from './useRenderMenuTitle'
5 import { pathResolve } from '@/utils/routerHelper'
6
7 const { renderMenuTitle } = useRenderMenuTitle()
8
9 export const useRenderMenuItem = () =>
10   // allRouters: AppRouteRecordRaw[] = [],
11   {
12     const renderMenuItem = (routers: AppRouteRecordRaw[], parentPath = '/') => {
13       return routers
14         .filter((v) => !v.meta?.hidden)
15         .map((v) => {
16           const meta = v.meta ?? {}
17           const { oneShowingChild, onlyOneChild } = hasOneShowingChild(v.children, v)
18           const fullPath = isUrl(v.path) ? v.path : pathResolve(parentPath, v.path) // getAllParentPath<AppRouteRecordRaw>(allRouters, v.path).join('/')
19           if (
20             oneShowingChild &&
21             (!onlyOneChild?.children || onlyOneChild?.noShowingChildren) &&
22             !meta?.alwaysShow
23           ) {
24             return (
25               <ElMenuItem
26                 index={onlyOneChild ? pathResolve(fullPath, onlyOneChild.path) : fullPath}
27               >
28                 {{
29                   default: () => renderMenuTitle(onlyOneChild ? onlyOneChild?.meta : meta)
30                 }}
31               </ElMenuItem>
32             )
33           } else {
34             return (
35               <ElSubMenu index={fullPath}>
36                 {{
37                   title: () => renderMenuTitle(meta),
38                   default: () => renderMenuItem(v.children!, fullPath)
39                 }}
40               </ElSubMenu>
41             )
42           }
43         })
44     }
45
46     return {
47       renderMenuItem
48     }
49   }