Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 1 | import React, { useMemo, forwardRef } from 'react'; |
| 2 | import PropTypes from 'prop-types'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 3 | import ListItem from '@mui/material/ListItem'; |
| 4 | import ListItemIcon from '@mui/material/ListItemIcon'; |
| 5 | import ListItemText from '@mui/material/ListItemText'; |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 6 | import { Link as RouterLink } from 'react-router-dom'; |
| 7 | |
| 8 | function ListItemLink(props) { |
| 9 | const { icon, primary, secondary, to } = props |
| 10 | |
| 11 | const renderLink = useMemo( |
| 12 | () => forwardRef((itemProps, ref) => <RouterLink to={to} ref={ref} {...itemProps} />), |
| 13 | [to]) |
| 14 | |
| 15 | return ( |
| 16 | <ListItem button component={renderLink}> |
| 17 | {icon ? <ListItemIcon>{icon}</ListItemIcon> : null} |
| 18 | <ListItemText primary={primary} secondary={secondary} /> |
| 19 | </ListItem> |
| 20 | ) |
| 21 | } |
| 22 | |
| 23 | ListItemLink.propTypes = { |
| 24 | icon: PropTypes.element, |
| 25 | primary: PropTypes.string.isRequired, |
| 26 | secondary: PropTypes.string, |
| 27 | to: PropTypes.string.isRequired, |
| 28 | } |
| 29 | |
| 30 | export default ListItemLink |