forked from yunsii/use-switch-tabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicLayout.tsx
119 lines (110 loc) · 3.38 KB
/
BasicLayout.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React from 'react';
import { history, useLocation } from '@vitjs/runtime';
import { Divider, Layout, Menu, Space, Typography } from 'antd';
import { GithubOutlined } from '@ant-design/icons';
import * as H from 'history';
import SwitchTabs from '@/components/SwitchTabs';
// import { Mode } from '../../../src';
export interface IRoute {
component: React.ComponentType<{ location: H.Location }>;
icon?: React.ReactNode;
name?: string;
path: string;
redirect: string;
routes: IRoute[];
}
export interface BasicLayoutProps {
children: JSX.Element;
/** 完整路由表 */
routes: IRoute[];
/** 当前层级路由表 */
route: IRoute;
}
export default function BasicLayout(props: BasicLayoutProps) {
const { children, route } = props;
const location = useLocation();
const getRoutesMenuData = (routes: IRoute[]) => {
return routes.filter((item) => {
return !item.redirect && item.path;
});
};
const renderSubMenu = (route: IRoute) => {
const subRoutes = getRoutesMenuData(route.routes);
return (
<Menu.SubMenu key={route.path} icon={route.icon} title={route.name}>
{getRoutesMenuData(subRoutes).map((item) => {
return (
<Menu.Item key={item.path} icon={item.icon} onClick={() => history.push(item.path)}>
{item.path === '/' ? 'Home' : item.name || item.path}
</Menu.Item>
);
})}
</Menu.SubMenu>
);
};
const renderMenu = () => {
return (
<Menu mode='inline' selectedKeys={[location.pathname]}>
{getRoutesMenuData(route.routes).map((item) => {
const subRoutes = item.routes;
if (subRoutes) {
return renderSubMenu(item);
}
return (
<Menu.Item key={item.path} icon={item.icon} onClick={() => history.push(item.path)}>
{item.path === '/' ? 'Home' : item.name || item.path}
</Menu.Item>
);
})}
</Menu>
);
};
return (
<Layout>
<Layout.Sider
style={{
overflow: 'auto',
height: '100vh',
position: 'fixed',
left: 0,
}}
width={240}
theme='light'
>
<h2 style={{ padding: 16 }}>🚀 use-switch-tabs</h2>
{renderMenu()}
</Layout.Sider>
<Layout style={{ height: '100vh', marginLeft: 240 }}>
<Layout.Content>
<SwitchTabs
originalRoutes={route.routes}
persistent={{
cacheName: 'use-switch-tabs-state',
}}
// mode={Mode.Dynamic}
// setTabName={({ path, name }) => {
// if (path === '/search/applications') {
// return `${name} - 自定义`;
// }
// }}
>
{children}
</SwitchTabs>
</Layout.Content>
<Layout.Footer style={{ textAlign: 'center' }}>
<Space>
<a href='https://github.com/theprimone/use-switch-tabs' target='_blank'>
<Typography.Text type='secondary'>
<GithubOutlined />
</Typography.Text>
</a>
<Divider type='vertical' />
<a href='https://github.com/theprimone' target='_blank'>
<Typography.Text type='secondary'>theprimone</Typography.Text>
</a>
</Space>
</Layout.Footer>
</Layout>
</Layout>
);
}