RouteTabList.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Box, Tab, Tabs } from '@mui/material';
  2. import { FC } from 'react';
  3. import { useNavigate, useLocation } from 'react-router-dom';
  4. import { ITabListProps, ITabPanel } from './Types';
  5. import { useStyles } from './style';
  6. const TabPanel = (props: ITabPanel) => {
  7. const { children, value, index, className = '', ...other } = props;
  8. return (
  9. <div
  10. role="tabpanel"
  11. hidden={value !== index}
  12. className={className}
  13. id={`tabpanel-${index}`}
  14. aria-labelledby={`tabpanel-${index}`}
  15. {...other}
  16. >
  17. {value === index && <Box height="100%">{children}</Box>}
  18. </div>
  19. );
  20. };
  21. const a11yProps = (index: number) => {
  22. return {
  23. id: `tab-${index}`,
  24. 'aria-controls': `tabpanel-${index}`,
  25. };
  26. };
  27. const RouteTabList: FC<ITabListProps> = props => {
  28. const { tabs, activeIndex = 0, wrapperClass = '' } = props;
  29. const classes = useStyles();
  30. const navigate = useNavigate();
  31. const location = useLocation();
  32. const handleChange = (event: any, newValue: any) => {
  33. const newPath =
  34. location.pathname.split('/').slice(0, -1).join('/') +
  35. '/' +
  36. tabs[newValue].path;
  37. navigate(newPath);
  38. };
  39. return (
  40. <div className={`${classes.wrapper} ${wrapperClass}`}>
  41. <Tabs
  42. classes={{
  43. indicator: classes.tab,
  44. flexContainer: classes.tabContainer,
  45. }}
  46. // if not provide this property, Material will add single span element by default
  47. TabIndicatorProps={{ children: <div className="tab-indicator" /> }}
  48. value={activeIndex}
  49. onChange={handleChange}
  50. aria-label="tabs"
  51. >
  52. {tabs.map((tab, index) => (
  53. <Tab
  54. classes={{ root: classes.tabContent }}
  55. key={tab.label}
  56. label={tab.label}
  57. {...a11yProps(index)}
  58. ></Tab>
  59. ))}
  60. </Tabs>
  61. {tabs.map((tab, index) => (
  62. <TabPanel
  63. key={tab.label}
  64. value={activeIndex}
  65. index={index}
  66. className={classes.tabPanel}
  67. >
  68. {tab.component}
  69. </TabPanel>
  70. ))}
  71. </div>
  72. );
  73. };
  74. export default RouteTabList;