import { Box, makeStyles, Tab, Tabs, Theme } from '@material-ui/core'; import { FC, useState } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { ITabListProps, ITabPanel } from './Types'; const useStyles = makeStyles((theme: Theme) => ({ wrapper: { display: 'flex', flexDirection: 'column', flexBasis: 0, flexGrow: 1, '& .MuiTab-wrapper': { textTransform: 'capitalize', fontWeight: 'normal', color: '#323232', }, background: '#fff', border: '1px solid #e9e9ed', padding: theme.spacing(0, 1), }, tab: { backgroundColor: theme.palette.primary.main, }, tabContainer: { borderBottom: '1px solid #e0e0e0', }, tabContent: { minWidth: 0, marginRight: theme.spacing(3), }, tabPanel: { flexBasis: 0, flexGrow: 1, marginTop: theme.spacing(1), overflow: 'hidden', }, })); const TabPanel = (props: ITabPanel) => { const { children, value, index, className = '', ...other } = props; return ( ); }; const a11yProps = (index: number) => { return { id: `tab-${index}`, 'aria-controls': `tabpanel-${index}`, }; }; const RouteTabList: FC = props => { const { tabs, activeIndex = 0, wrapperClass = '' } = props; const classes = useStyles(); const [value, setValue] = useState(activeIndex); const navigate = useNavigate(); const location = useLocation(); const handleChange = (event: any, newValue: any) => { setValue(newValue); const newPath = location.pathname.split('/').slice(0, -1).join('/') + '/' + tabs[newValue].path; navigate(`${newPath}`); }; return (
}} value={value} onChange={handleChange} aria-label="tabs" > {tabs.map((tab, index) => ( ))} {tabs.map((tab, index) => ( {tab.component} ))}
); }; export default RouteTabList;