|
1 | | -import React, { useEffect, useState } from 'react'; |
2 | 1 | import { TabContext, TabList, TabPanel } from '@mui/lab'; |
3 | | -import { Box, Tab as MuiTab, useTheme } from '@mui/material'; |
| 2 | +import { Box, Tab as MuiTab, SxProps, Theme, useTheme } from '@mui/material'; |
| 3 | +import React, { useEffect, useState } from 'react'; |
| 4 | + |
4 | 5 | import { useDeviceInfo } from '../../utils'; |
5 | 6 | import Icon from '../Icon/Icon'; |
6 | 7 | import { IconName } from '../Icon/icons'; |
7 | 8 |
|
8 | 9 | export type Tab = { |
| 10 | + children: React.ReactNode; |
| 11 | + disabled?: boolean; |
9 | 12 | icon?: IconName; |
10 | 13 | id: string; |
11 | 14 | label: string; |
12 | | - children: React.ReactNode; |
13 | | - disabled?: boolean; |
14 | 15 | }; |
15 | 16 |
|
16 | 17 | export type TabsProps = { |
17 | | - tabs: Tab[]; |
18 | | - onTabChange?: (tab: string) => void; |
19 | 18 | activeTab?: string; |
| 19 | + onTabChange?: (tab: string) => void; |
| 20 | + tabs: Tab[]; |
| 21 | + tabStyle?: SxProps<Theme>; |
20 | 22 | }; |
21 | 23 |
|
22 | | -const Tabs = ({ tabs, onTabChange, activeTab }: TabsProps): JSX.Element => { |
| 24 | +const Tabs = ({ activeTab, onTabChange, tabs, tabStyle }: TabsProps): JSX.Element => { |
23 | 25 | const [selectedTab, setSelectedTab] = useState<string>(activeTab ?? tabs[0]?.id ?? ''); |
24 | 26 | const theme = useTheme(); |
25 | 27 | const { isMobile } = useDeviceInfo(); |
26 | 28 |
|
27 | | - const tabStyles = { |
28 | | - color: theme.palette.TwClrTxtSecondary as string, |
29 | | - fontSize: '16px', |
30 | | - fontWeight: 600, |
31 | | - lineHeight: '24px', |
32 | | - padding: theme.spacing(1, 2), |
33 | | - minHeight: theme.spacing(4.5), |
34 | | - textTransform: 'capitalize', |
35 | | - '&:hover': { |
36 | | - backgroundColor: theme.palette.TwClrBgHover as string, |
37 | | - }, |
38 | | - '&.Mui-selected': { |
39 | | - color: theme.palette.TwClrTxtBrand as string, |
40 | | - }, |
41 | | - '&.MuiTab-labelIcon': { |
42 | | - display: 'flex', |
43 | | - flexDirection: 'row', |
44 | | - alignItems: 'center', |
| 29 | + const tabStyles = [ |
| 30 | + { |
| 31 | + color: theme.palette.TwClrTxtSecondary as string, |
| 32 | + fontSize: '16px', |
| 33 | + fontWeight: 600, |
| 34 | + lineHeight: '24px', |
| 35 | + minHeight: theme.spacing(4.5), |
| 36 | + padding: theme.spacing(1, 2), |
| 37 | + textTransform: 'capitalize', |
| 38 | + '&:hover': { |
| 39 | + backgroundColor: theme.palette.TwClrBgHover as string, |
| 40 | + }, |
| 41 | + '&.Mui-selected': { |
| 42 | + color: theme.palette.TwClrTxtBrand as string, |
| 43 | + }, |
| 44 | + '&.MuiTab-labelIcon': { |
| 45 | + alignItems: 'center', |
| 46 | + display: 'flex', |
| 47 | + flexDirection: 'row', |
| 48 | + }, |
| 49 | + '& .MuiTab-iconWrapper': { |
| 50 | + fill: theme.palette.TwClrIcnSecondary as string, |
| 51 | + marginBottom: 0, |
| 52 | + marginRight: theme.spacing(1), |
| 53 | + }, |
| 54 | + '&.Mui-selected .MuiTab-iconWrapper': { |
| 55 | + fill: theme.palette.TwClrIcnBrand as string, |
| 56 | + }, |
45 | 57 | }, |
46 | | - '& .MuiTab-iconWrapper': { |
47 | | - fill: theme.palette.TwClrIcnSecondary as string, |
48 | | - marginBottom: 0, |
49 | | - marginRight: theme.spacing(1), |
50 | | - }, |
51 | | - '&.Mui-selected .MuiTab-iconWrapper': { |
52 | | - fill: theme.palette.TwClrIcnBrand as string, |
53 | | - }, |
54 | | - }; |
| 58 | + ...(Array.isArray(tabStyle) ? tabStyle : [tabStyle]), |
| 59 | + ]; |
55 | 60 |
|
56 | 61 | const tabHeaderProps = { |
57 | 62 | borderBottom: 1, |
@@ -82,30 +87,30 @@ const Tabs = ({ tabs, onTabChange, activeTab }: TabsProps): JSX.Element => { |
82 | 87 | <TabContext value={selectedTab}> |
83 | 88 | <Box sx={tabHeaderProps}> |
84 | 89 | <TabList |
85 | | - variant='scrollable' |
86 | | - sx={{ minHeight: theme.spacing(4.5) }} |
87 | 90 | onChange={(unused, value: string) => setTab(value)} |
| 91 | + sx={{ minHeight: theme.spacing(4.5) }} |
88 | 92 | TabIndicatorProps={{ |
89 | 93 | style: { |
90 | 94 | background: theme.palette.TwClrBgBrand, |
91 | 95 | height: '4px', |
92 | 96 | }, |
93 | 97 | }} |
| 98 | + variant='scrollable' |
94 | 99 | > |
95 | 100 | {tabs.map((tab, index) => ( |
96 | 101 | <MuiTab |
| 102 | + disabled={tab.disabled} |
97 | 103 | icon={tab.icon ? <Icon name={tab.icon} /> : undefined} |
| 104 | + key={index} |
98 | 105 | label={tab.label} |
99 | | - value={tab.id} |
100 | 106 | sx={tabStyles} |
101 | | - key={index} |
102 | | - disabled={tab.disabled} |
| 107 | + value={tab.id} |
103 | 108 | /> |
104 | 109 | ))} |
105 | 110 | </TabList> |
106 | 111 | </Box> |
107 | 112 | {tabs.map((tab, index) => ( |
108 | | - <TabPanel value={tab.id} key={index} sx={tabPanelStyles}> |
| 113 | + <TabPanel key={index} sx={tabPanelStyles} value={tab.id}> |
109 | 114 | {tab.children} |
110 | 115 | </TabPanel> |
111 | 116 | ))} |
|
0 commit comments