-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathListRouter.tsx
79 lines (72 loc) · 2.24 KB
/
ListRouter.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
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import Paper from '@material-ui/core/Paper';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Divider from '@material-ui/core/Divider';
import InboxIcon from '@material-ui/icons/Inbox';
import DraftsIcon from '@material-ui/icons/Drafts';
import Typography from '@material-ui/core/Typography';
import { Route, MemoryRouter } from 'react-router';
import {
Link as RouterLink,
LinkProps as RouterLinkProps,
} from 'react-router-dom';
import { Omit } from '@material-ui/types';
interface ListItemLinkProps {
icon?: React.ReactElement;
primary: string;
to: string;
}
function ListItemLink(props: ListItemLinkProps) {
const { icon, primary, to } = props;
const renderLink = React.useMemo(
() =>
React.forwardRef<any, Omit<RouterLinkProps, 'to'>>((itemProps, ref) => (
<RouterLink to={to} ref={ref} {...itemProps} />
)),
[to],
);
return (
<li>
<ListItem button component={renderLink}>
{icon ? <ListItemIcon>{icon}</ListItemIcon> : null}
<ListItemText primary={primary} />
</ListItem>
</li>
);
}
const useStyles = makeStyles({
root: {
width: 360,
},
});
export default function ListRouter() {
const classes = useStyles();
return (
<MemoryRouter initialEntries={['/drafts']} initialIndex={0}>
<div className={classes.root}>
<Route>
{({ location }) => (
<Typography gutterBottom>
Current route: {location.pathname}
</Typography>
)}
</Route>
<Paper elevation={0}>
<List aria-label="main mailbox folders">
<ListItemLink to="/inbox" primary="Inbox" icon={<InboxIcon />} />
<ListItemLink to="/drafts" primary="Drafts" icon={<DraftsIcon />} />
</List>
<Divider />
<List aria-label="secondary mailbox folders">
<ListItemLink to="/trash" primary="Trash" />
<ListItemLink to="/spam" primary="Spam" />
</List>
</Paper>
</div>
</MemoryRouter>
);
}