Add dynamic menu to MainFramework

This commit is contained in:
2023-09-07 18:10:28 +08:00
parent 2c7b5fd6e0
commit e3d2a089f6
3 changed files with 42 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
import React, { createContext } from 'react' import React, { createContext } from 'react'
import '@/assets/css/header.scss' import '@/assets/css/header.scss'
import LoadingMask from '@/components/LoadingMask.tsx' import LoadingMask from '@/components/LoadingMask.tsx'
import router from '@/router'
export const MainFrameworkContext = createContext<{ export const MainFrameworkContext = createContext<{
navbarHiddenState: { navbarHiddenState: {
@@ -16,6 +17,8 @@ export const MainFrameworkContext = createContext<{
const MainFramework: React.FC = () => { const MainFramework: React.FC = () => {
const [navbarHidden, setNavbarHidden] = useState(false) const [navbarHidden, setNavbarHidden] = useState(false)
const routeId = useMatches()[1].id
const routeChildren = router.routes[0].children?.find((value) => value.id === routeId)?.children
return ( return (
<> <>
@@ -26,36 +29,20 @@ const MainFramework: React.FC = () => {
</a> </a>
<nav> <nav>
<ul className={'menu'}> <ul className={'menu'}>
<li className={'item'}> {routeChildren?.map((route) => {
<NavLink return (
to={''} <li className={'item'}>
className={({ isActive, isPending }) => <NavLink
isPending ? 'pending' : isActive ? 'active' : '' to={route.path ?? ''}
} className={({ isActive, isPending }) =>
> isPending ? 'pending' : isActive ? 'active' : ''
}
</NavLink> >
</li> {(route.handle as RouteHandle).name}
<li className={'item'}> </NavLink>
<NavLink </li>
to={'https://blog.fatweb.top'} )
className={({ isActive, isPending }) => })}
isPending ? 'pending' : isActive ? 'active' : ''
}
>
</NavLink>
</li>
<li className={'item'}>
<NavLink
to={'project'}
className={({ isActive, isPending }) =>
isPending ? 'pending' : isActive ? 'active' : ''
}
>
App
</NavLink>
</li>
</ul> </ul>
</nav> </nav>
</header> </header>

View File

@@ -25,6 +25,17 @@ const routes: RouteObject[] = [
id: 'home', id: 'home',
Component: React.lazy(() => import('@/components/Home')), Component: React.lazy(() => import('@/components/Home')),
handle: { handle: {
name: '主页',
menu: true,
auth: false
}
},
{
path: 'https://blog.fatweb.top',
id: 'blog',
handle: {
name: '博客',
menu: true,
auth: false auth: false
} }
}, },
@@ -33,6 +44,8 @@ const routes: RouteObject[] = [
id: 'project', id: 'project',
Component: React.lazy(() => import('@/components/Project')), Component: React.lazy(() => import('@/components/Project')),
handle: { handle: {
name: '项目',
menu: true,
auth: false auth: false
} }
} }

17
src/vite-env.d.ts vendored
View File

@@ -1,12 +1,14 @@
/// <reference types="vite/client" /> /// <reference types="vite/client" />
type Captcha = { type RouteHandle = {
value: string name?: string
base64Src: string menu?: boolean
auth?: boolean
} }
type RouteHandle = { interface FitFullscreenProps extends PropsWithChildren {
auth: boolean zIndex?: number
backgroundColor?: string
} }
type _Response<T> = { type _Response<T> = {
@@ -15,6 +17,11 @@ type _Response<T> = {
data: T | null data: T | null
} }
type Captcha = {
value: string
base64Src: string
}
type Token = { type Token = {
token: string token: string
} }