All files / src/components/Tabs index.tsx

0% Statements 0/21
0% Branches 0/20
0% Functions 0/7
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                                                                                                                                                                                                                                                           
import React, { type JSX, useEffect, useMemo, useState } from 'react';
 
import { TabContext, TabList, TabPanel } from '@mui/lab';
import { Box, Tab as MuiTab, SxProps, Theme, Tooltip, useTheme } from '@mui/material';
 
import { useDeviceInfo } from '../../utils';
import Icon from '../Icon/Icon';
import { IconName } from '../Icon/icons';
 
export type Tab = {
  children: React.ReactNode;
  disabled?: boolean;
  icon?: IconName;
  id: string;
  label: string;
  tooltip?: string;
};
 
export type TabsProps = {
  activeTab?: string;
  children?: React.ReactNode;
  onChangeTab?: (tab: string) => void;
  tabs: Tab[];
  tabStyle?: SxProps<Theme>;
  headerBorder?: boolean;
  sx?: SxProps;
};
 
const Tabs = ({
  activeTab,
  children,
  onChangeTab,
  tabs,
  tabStyle,
  headerBorder = false,
  sx,
}: TabsProps): JSX.Element => {
  const [selectedTab, setSelectedTab] = useState<string>(activeTab ?? tabs[0]?.id ?? '');
  const theme = useTheme();
  const { isMobile } = useDeviceInfo();
 
  const tabStyles = [
    {
      color: theme.palette.TwClrTxtSecondary as string,
      fontSize: '16px',
      fontWeight: 600,
      lineHeight: '24px',
      minHeight: theme.spacing(4.5),
      padding: theme.spacing(1, 2),
      textTransform: 'capitalize',
      '&:hover': {
        backgroundColor: theme.palette.TwClrBgHover as string,
      },
      '&.Mui-selected': {
        color: theme.palette.TwClrTxtBrand as string,
      },
      '&.MuiTab-labelIcon': {
        alignItems: 'center',
        display: 'flex',
        flexDirection: 'row',
      },
      '& .MuiTab-iconWrapper': {
        fill: theme.palette.TwClrIcnSecondary as string,
        marginBottom: 0,
        marginRight: theme.spacing(1),
      },
      '&.Mui-selected .MuiTab-iconWrapper': {
        fill: theme.palette.TwClrIcnBrand as string,
      },
    },
    ...(Array.isArray(tabStyle) ? tabStyle : [tabStyle]),
  ];
 
  const tabHeaderProps = {
    borderBottom: headerBorder ? 1 : 0,
    borderColor: 'divider',
    margin: isMobile ? 0 : theme.spacing(0, 4),
  };
 
  const tabPanelStyles = {
    padding: 0,
  };
 
  const setTab = (tab: string) => {
    if (onChangeTab) {
      onChangeTab(tab);
    } else {
      setSelectedTab(tab);
    }
  };
 
  useEffect(() => {
    if (activeTab !== undefined) {
      setSelectedTab(activeTab);
    }
  }, [activeTab]);
 
  const renderedTabs = useMemo(
    () =>
      tabs.map((tab, index) => {
        const muiTab = (
          <MuiTab
            disabled={tab.disabled}
            icon={tab.icon ? <Icon name={tab.icon} /> : undefined}
            label={tab.label}
            sx={tab.disabled ? [...tabStyles, { pointerEvents: 'none' }] : tabStyles}
            value={tab.id}
          />
        );
 
        return (
          <Tooltip title={tab.tooltip} key={index}>
            {tab.disabled ? <span style={{ display: 'inline-flex' }}>{muiTab}</span> : muiTab}
          </Tooltip>
        );
      }),
    [tabs, tabStyles]
  );
 
  return (
    <Box sx={{ width: '100%', ...sx }}>
      <TabContext value={selectedTab}>
        <Box sx={tabHeaderProps} className='tab-header'>
          <TabList
            onChange={(unused, value: string) => setTab(value)}
            sx={{ minHeight: theme.spacing(4.5) }}
            TabIndicatorProps={{
              style: {
                background: theme.palette.TwClrBgBrand,
                height: '4px',
              },
            }}
            variant={'scrollable'}
          >
            {renderedTabs}
          </TabList>
        </Box>
        {children}
        {tabs.map((tab, index) => (
          <TabPanel key={index} sx={tabPanelStyles} value={tab.id}>
            {tab.children}
          </TabPanel>
        ))}
      </TabContext>
    </Box>
  );
};
 
export default Tabs;