Skip to content

Commit 1f1efbb

Browse files
committed
refactor(projects): refactor the menu section code
1 parent dab5333 commit 1f1efbb

File tree

10 files changed

+87
-253
lines changed

10 files changed

+87
-253
lines changed

src/layouts/base-layout/index.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AdminLayout, LAYOUT_SCROLL_EL_ID } from '@sa/materials';
22
import type { LayoutMode } from '@sa/materials';
33
import { configResponsive } from 'ahooks';
4-
import { Suspense } from 'react';
54
import './index.scss';
65
import {
76
getContentXScrollable,
@@ -19,8 +18,7 @@ import GlobalHeader from '../modules/global-header';
1918
import GlobalSider from '../modules/global-sider';
2019
import ThemeDrawer from '../modules/theme-drawer';
2120
import GlobalTab from '../modules/global-tab';
22-
23-
const GlobalMenu = lazy(() => import('../modules/global-menu'));
21+
import GlobalMenu from '../modules/global-menu';
2422

2523
const LAYOUT_MODE_VERTICAL: LayoutMode = 'vertical';
2624
const LAYOUT_MODE_HORIZONTAL: LayoutMode = 'horizontal';
@@ -77,8 +75,6 @@ export function Component() {
7775
[activeFirstLevelMenuKey, menus]
7876
);
7977

80-
console.log(childrenMenu);
81-
8278
function getSiderWidth() {
8379
const { width, mixWidth, mixChildMenuWidth } = themeSettings.sider;
8480

@@ -150,13 +146,13 @@ export function Component() {
150146
Footer={<GlobalFooter />}
151147
>
152148
<GlobalContent />
153-
<Suspense fallback={null}>
154-
<GlobalMenu
155-
childrenMenu={childrenMenu}
156-
mode={themeSettings.layout.mode}
157-
menus={menus}
158-
/>
159-
</Suspense>
149+
150+
<GlobalMenu
151+
childrenMenu={childrenMenu}
152+
mode={themeSettings.layout.mode}
153+
menus={menus}
154+
/>
155+
160156
<ThemeDrawer />
161157
</AdminLayout>
162158
);

src/layouts/modules/global-menu/components/FirstLevelMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function MixMenuItem({ label, Icon, active, isMini, inverted, onClick }: MixMenu
6161
);
6262
}
6363

64-
const FirstLevelMenu: FC<Props> = memo(({ children, inverted, onSelect }) => {
64+
const FirstLevelMenu = ({ children, inverted, onSelect }: Props) => {
6565
const menus = useMenu();
6666

6767
const siderCollapse = useAppSelector(getSiderCollapse);
@@ -88,6 +88,6 @@ const FirstLevelMenu: FC<Props> = memo(({ children, inverted, onSelect }) => {
8888
/>
8989
</div>
9090
);
91-
});
91+
};
9292

9393
export default FirstLevelMenu;

src/layouts/modules/global-menu/components/HorizontalMenu.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { Route } from '@sa/simple-router';
22
import type { MenuInfo } from 'rc-menu/lib/interface';
3+
import type { MenuProps } from 'antd';
34
import { useRouterPush } from '@/hooks/common/routerPush';
45

6+
export interface Props {
7+
menus: MenuProps['items'];
8+
}
9+
510
function getSelectKey(route: Route) {
611
const { hideInMenu, activeMenu } = route.meta;
712

@@ -12,11 +17,9 @@ function getSelectKey(route: Route) {
1217
return [routeName];
1318
}
1419

15-
const HorizontalMenu = () => {
20+
const HorizontalMenu: FC<Props> = memo(({ menus }) => {
1621
const route = useRoute();
1722

18-
const menus = useMenu();
19-
2023
const router = useRouterPush();
2124

2225
const selectKey = getSelectKey(route);
@@ -31,9 +34,10 @@ const HorizontalMenu = () => {
3134
items={menus}
3235
inlineIndent={18}
3336
onSelect={handleClickMenu}
37+
className="size-full bg-container transition-300 border-0!"
3438
selectedKeys={selectKey}
3539
/>
3640
);
37-
};
41+
});
3842

3943
export default HorizontalMenu;

src/layouts/modules/global-menu/components/VerticalMenu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { SimpleScrollbar } from '@sa/materials';
22
import type { Route, RouteRecordNormalized } from '@sa/simple-router';
33
import type { MenuInfo } from 'rc-menu/lib/interface';
44
import type { MenuProps } from 'antd';
5+
import type { FC } from 'react';
56
import { getSiderCollapse } from '@/store/slice/app';
67
import { getThemeSettings } from '@/store/slice/theme';
8+
import type { Props } from './HorizontalMenu';
79

810
interface LevelKeysProps {
911
key?: string;
@@ -47,9 +49,7 @@ const getSelectedMenuKeyPath = (matches: RouteRecordNormalized[]) => {
4749
return result;
4850
};
4951

50-
const VerticalMenu = memo(() => {
51-
const menus = useMenu();
52-
52+
const VerticalMenu: FC<Props> = memo(({ menus }) => {
5353
const route = useRoute();
5454
const levelKeys = getLevelKeys(menus as LevelKeysProps[]);
5555

src/layouts/modules/global-menu/index.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
import type { MenuProps } from 'antd';
2-
import { createPortal } from 'react-dom';
3-
import { GLOBAL_HEADER_MENU_ID, GLOBAL_SIDER_MENU_ID } from '@/constants/app';
2+
import type { SubMenuType } from 'antd/es/menu/interface';
3+
import { setActiveFirstLevelMenuKey } from '@/store/slice/tab';
44
import VerticalMixMenu from './modules/VerticalMixMenu';
55
import HorizontalMenu from './modules/HorizontalMenu';
6-
import HorizontalMixMenu from './modules/HorizontalMixMenu';
76
import VerticalMenu from './modules/VerticalMenu';
7+
import FirstLevelMenu from './components/FirstLevelMenu';
88

99
interface Props {
1010
mode: UnionKey.ThemeLayoutMode;
1111
menus: MenuProps['items'];
1212
childrenMenu: MenuProps['items'];
1313
}
1414

15-
const headerContainer = document.getElementById(GLOBAL_HEADER_MENU_ID);
15+
const GlobalMenu: FC<Props> = memo(({ mode, menus, childrenMenu }) => {
16+
const dispatch = useAppDispatch();
1617

17-
const siderContainer = document.getElementById(GLOBAL_SIDER_MENU_ID);
18+
const router = useRouterPush();
19+
function handleSelectMixMenu(menu: SubMenuType) {
20+
dispatch(setActiveFirstLevelMenuKey(menu.key));
1821

19-
const GlobalMenu: FC<Props> = memo(({ mode, menus, childrenMenu }) => {
20-
if (!headerContainer || !siderContainer) return null;
22+
if (!menu.children?.length) {
23+
router.routerPush({ name: menu.key });
24+
}
25+
}
2126

2227
const componentsMap = {
23-
vertical: createPortal(<VerticalMenu menus={menus} />, siderContainer),
24-
'vertical-mix': createPortal(<VerticalMixMenu menus={childrenMenu} />, siderContainer),
25-
horizontal: createPortal(<HorizontalMenu />, headerContainer),
26-
'horizontal-mix': <HorizontalMixMenu />
28+
vertical: <VerticalMenu menus={menus} />,
29+
'vertical-mix': <VerticalMixMenu menus={childrenMenu} />,
30+
horizontal: <HorizontalMenu menus={menus} />,
31+
'horizontal-mix': [
32+
<HorizontalMenu
33+
key={1}
34+
menus={childrenMenu}
35+
/>,
36+
<FirstLevelMenu
37+
key={2}
38+
onSelect={handleSelectMixMenu}
39+
/>
40+
]
2741
};
2842

2943
return componentsMap[mode];
Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
1-
import type { Route } from '@sa/simple-router';
2-
import type { MenuInfo } from 'rc-menu/lib/interface';
3-
import { useRouterPush } from '@/hooks/common/routerPush';
4-
import { headerContainer } from './shared';
1+
import { createPortal } from 'react-dom';
2+
import { GLOBAL_HEADER_MENU_ID } from '@/constants/app';
3+
import HorizontalMenu from '../components/HorizontalMenu';
4+
import type { Props } from '../components/HorizontalMenu';
5+
import { useGetElementById } from './hook';
56

6-
function getSelectKey(route: Route) {
7-
const { hideInMenu, activeMenu } = route.meta;
7+
const Horizontal = ({ menus }: Props) => {
8+
const container = useGetElementById(GLOBAL_HEADER_MENU_ID);
89

9-
const name = route.name as string;
10+
if (!container) return null;
1011

11-
const routeName = (hideInMenu ? activeMenu : name) || name;
12-
13-
return [routeName];
14-
}
15-
16-
const HorizontalMenu = () => {
17-
const route = useRoute();
18-
19-
const menus = useMenu();
20-
21-
const router = useRouterPush();
22-
23-
const selectKey = getSelectKey(route);
24-
25-
function handleClickMenu(menuInfo: MenuInfo) {
26-
router.menuPush(menuInfo.key);
27-
}
28-
29-
if (!headerContainer) return null;
30-
31-
return (
32-
<AMenu
33-
mode="horizontal"
34-
items={menus}
35-
inlineIndent={18}
36-
onSelect={handleClickMenu}
37-
selectedKeys={selectKey}
38-
/>
39-
);
12+
return createPortal(<HorizontalMenu menus={menus} />, container);
4013
};
4114

42-
export default HorizontalMenu;
15+
export default Horizontal;

src/layouts/modules/global-menu/modules/HorizontalMixMenu.tsx

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)