CustomTabList.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. overflowY: 'auto',
  28. },
  29. }));
  30. const TabPanel = (props: ITabPanel) => {
  31. const { children, value, index, className = '', ...other } = props;
  32. return (
  33. <div
  34. role="tabpanel"
  35. hidden={value !== index}
  36. className={className}
  37. id={`tabpanel-${index}`}
  38. aria-labelledby={`tabpanel-${index}`}
  39. {...other}
  40. >
  41. {value === index && <Box height="100%">{children}</Box>}
  42. </div>
  43. );
  44. };
  45. const a11yProps = (index: number) => {
  46. return {
  47. id: `tab-${index}`,
  48. 'aria-controls': `tabpanel-${index}`,
  49. };
  50. };
  51. const CustomTabList: FC<ITabListProps> = props => {
  52. const { tabs, activeIndex = 0, handleTabChange, wrapperClass = '' } = props;
  53. const classes = useStyles();
  54. const [value, setValue] = useState<number>(activeIndex);
  55. const handleChange = (event: any, newValue: any) => {
  56. setValue(newValue);
  57. handleTabChange && handleTabChange(newValue);
  58. };
  59. return (
  60. <>
  61. <Tabs
  62. classes={{
  63. root: `${classes.wrapper} ${wrapperClass}`,
  64. indicator: classes.tab,
  65. flexContainer: classes.tabContainer,
  66. }}
  67. // if not provide this property, Material will add single span element by default
  68. TabIndicatorProps={{ children: <div className="tab-indicator" /> }}
  69. value={value}
  70. onChange={handleChange}
  71. aria-label="tabs"
  72. >
  73. {tabs.map((tab, index) => (
  74. <Tab
  75. classes={{ root: classes.tabContent }}
  76. textColor="primary"
  77. key={tab.label}
  78. label={tab.label}
  79. {...a11yProps(index)}
  80. ></Tab>
  81. ))}
  82. </Tabs>
  83. {tabs.map((tab, index) => (
  84. <TabPanel
  85. key={tab.label}
  86. value={value}
  87. index={index}
  88. className={classes.tabPanel}
  89. >
  90. {tab.component}
  91. </TabPanel>
  92. ))}
  93. </>
  94. );
  95. };
  96. export default CustomTabList;