Skip to content

Commit e6099f4

Browse files
committed
refactor(tsx): convert CustomTabs component
1 parent e66ad7a commit e6099f4

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed
Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import classNames from 'classnames';
3-
import PropTypes from 'prop-types';
43
import { makeStyles } from '@material-ui/core/styles';
54
import Tabs from '@material-ui/core/Tabs';
65
import Tab from '@material-ui/core/Tab';
76
import Card from '../Card/Card';
87
import CardBody from '../Card/CardBody';
98
import CardHeader from '../Card/CardHeader';
10-
119
import styles from '../../assets/jss/material-dashboard-react/components/customTabsStyle';
1210

13-
const useStyles = makeStyles(styles);
11+
const useStyles = makeStyles(styles as any);
1412

15-
export default function CustomTabs(props) {
16-
const [value, setValue] = React.useState(0);
17-
const handleChange = (event, val) => {
18-
setValue(val);
19-
};
13+
type HeaderColor = 'warning' | 'success' | 'danger' | 'info' | 'primary' | 'rose';
14+
15+
interface TabItem {
16+
tabName: string;
17+
tabIcon?: React.ComponentType;
18+
tabContent: React.ReactNode;
19+
}
20+
21+
interface CustomTabsProps {
22+
headerColor?: HeaderColor;
23+
title?: string;
24+
tabs: TabItem[];
25+
rtlActive?: boolean;
26+
plainTabs?: boolean;
27+
}
28+
29+
const CustomTabs: React.FC<CustomTabsProps> = ({
30+
headerColor = 'primary',
31+
plainTabs = false,
32+
tabs,
33+
title,
34+
rtlActive = false,
35+
}) => {
36+
const [value, setValue] = useState(0);
2037
const classes = useStyles();
21-
const { headerColor, plainTabs, tabs, title, rtlActive } = props;
38+
39+
const handleChange = (event: React.ChangeEvent<unknown>, newValue: number) => {
40+
setValue(newValue);
41+
};
42+
2243
const cardTitle = classNames({
2344
[classes.cardTitle]: true,
2445
[classes.cardTitleRTL]: rtlActive,
2546
});
47+
2648
return (
2749
<Card plain={plainTabs}>
2850
<CardHeader color={headerColor} plain={plainTabs}>
@@ -39,12 +61,7 @@ export default function CustomTabs(props) {
3961
scrollButtons='auto'
4062
>
4163
{tabs.map((prop, key) => {
42-
let icon = {};
43-
if (prop.tabIcon) {
44-
icon = {
45-
icon: <prop.tabIcon />,
46-
};
47-
}
64+
const icon = prop.tabIcon ? { icon: <prop.tabIcon /> } : {};
4865
return (
4966
<Tab
5067
classes={{
@@ -61,27 +78,14 @@ export default function CustomTabs(props) {
6178
</Tabs>
6279
</CardHeader>
6380
<CardBody>
64-
{tabs.map((prop, key) => {
65-
if (key === value) {
66-
return <div key={key}>{prop.tabContent}</div>;
67-
}
68-
return null;
69-
})}
81+
{tabs.map((prop, key) => (
82+
<div key={key} style={{ display: key === value ? 'block' : 'none' }}>
83+
{prop.tabContent}
84+
</div>
85+
))}
7086
</CardBody>
7187
</Card>
7288
);
73-
}
74-
75-
CustomTabs.propTypes = {
76-
headerColor: PropTypes.oneOf(['warning', 'success', 'danger', 'info', 'primary', 'rose']),
77-
title: PropTypes.string,
78-
tabs: PropTypes.arrayOf(
79-
PropTypes.shape({
80-
tabName: PropTypes.string.isRequired,
81-
tabIcon: PropTypes.object,
82-
tabContent: PropTypes.node.isRequired,
83-
}),
84-
),
85-
rtlActive: PropTypes.bool,
86-
plainTabs: PropTypes.bool,
8789
};
90+
91+
export default CustomTabs;

0 commit comments

Comments
 (0)