2
0

CustomTabList.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Box, makeStyles, Tab, Tabs, Theme } from '@material-ui/core';
  2. import { FC, useState } from 'react';
  3. import { ITabListProps, ITabPanel } from './Types';
  4. const useStyles = makeStyles((theme: Theme) => ({
  5. wrapper: {
  6. '& .MuiTab-wrapper': {
  7. textTransform: 'capitalize',
  8. fontWeight: 'bold',
  9. color: '#323232',
  10. },
  11. },
  12. tab: {
  13. height: theme.spacing(0.5),
  14. backgroundColor: theme.palette.primary.main,
  15. },
  16. tabContainer: {
  17. borderBottom: '1px solid #e9e9ed',
  18. },
  19. tabContent: {
  20. minWidth: 0,
  21. marginRight: theme.spacing(3),
  22. },
  23. tabPanel: {
  24. flexBasis: 0,
  25. flexGrow: 1,
  26. marginTop: theme.spacing(2),
  27. },
  28. }));
  29. const TabPanel = (props: ITabPanel) => {
  30. const { children, value, index, className = '', ...other } = props;
  31. return (
  32. <div
  33. role="tabpanel"
  34. hidden={value !== index}
  35. className={className}
  36. id={`tabpanel-${index}`}
  37. aria-labelledby={`tabpanel-${index}`}
  38. {...other}
  39. >
  40. {value === index && <Box height="100%">{children}</Box>}
  41. </div>
  42. );
  43. };
  44. const a11yProps = (index: number) => {
  45. return {
  46. id: `tab-${index}`,
  47. 'aria-controls': `tabpanel-${index}`,
  48. };
  49. };
  50. const CustomTabList: FC<ITabListProps> = props => {
  51. const { tabs, activeIndex = 0, handleTabChange } = props;
  52. const classes = useStyles();
  53. const [value, setValue] = useState<number>(activeIndex);
  54. const handleChange = (event: any, newValue: any) => {
  55. setValue(newValue);
  56. handleTabChange && handleTabChange(newValue);
  57. };
  58. return (
  59. <>
  60. <Tabs
  61. classes={{
  62. root: classes.wrapper,
  63. indicator: classes.tab,
  64. flexContainer: classes.tabContainer,
  65. }}
  66. value={value}
  67. onChange={handleChange}
  68. aria-label="tabs"
  69. >
  70. {tabs.map((tab, index) => (
  71. <Tab
  72. classes={{ root: classes.tabContent }}
  73. textColor="primary"
  74. key={tab.label}
  75. label={tab.label}
  76. {...a11yProps(index)}
  77. ></Tab>
  78. ))}
  79. </Tabs>
  80. {tabs.map((tab, index) => (
  81. <TabPanel
  82. key={tab.label}
  83. value={value}
  84. index={index}
  85. className={classes.tabPanel}
  86. >
  87. {tab.component}
  88. </TabPanel>
  89. ))}
  90. </>
  91. );
  92. };
  93. export default CustomTabList;