mirror of
https://github.com/Unleash/unleash.git
synced 2024-10-23 20:07:40 +02:00
64 lines
1.7 KiB
React
64 lines
1.7 KiB
React
|
import React, { useState } from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import { Tabs, Tab, Paper } from '@material-ui/core';
|
||
|
|
||
|
import TabPanel from './TabPanel';
|
||
|
|
||
|
import { useStyles } from './styles';
|
||
|
import { useHistory } from 'react-router-dom';
|
||
|
|
||
|
const a11yProps = index => ({
|
||
|
id: `tab-${index}`,
|
||
|
'aria-controls': `tabpanel-${index}`,
|
||
|
});
|
||
|
|
||
|
const TabNav = ({ tabData, className, startingTab = 0 }) => {
|
||
|
const styles = useStyles();
|
||
|
const [activeTab, setActiveTab] = useState(startingTab);
|
||
|
const history = useHistory();
|
||
|
|
||
|
const renderTabs = () =>
|
||
|
tabData.map((tab, index) => (
|
||
|
<Tab
|
||
|
key={`${tab.label}_${index}`}
|
||
|
label={tab.label}
|
||
|
{...a11yProps(index)}
|
||
|
onClick={() => history.push(tab.path)}
|
||
|
/>
|
||
|
));
|
||
|
|
||
|
const renderTabPanels = () =>
|
||
|
tabData.map((tab, index) => (
|
||
|
<TabPanel key={`tab_panel_${index}`} value={activeTab} index={index}>
|
||
|
{tab.component}
|
||
|
</TabPanel>
|
||
|
));
|
||
|
|
||
|
return (
|
||
|
<>
|
||
|
<Paper className={styles.tabNav}>
|
||
|
<Tabs
|
||
|
value={activeTab}
|
||
|
onChange={(_, tabId) => {
|
||
|
setActiveTab(tabId);
|
||
|
}}
|
||
|
indicatorColor="primary"
|
||
|
textColor="primary"
|
||
|
centered
|
||
|
>
|
||
|
{renderTabs()}
|
||
|
</Tabs>
|
||
|
</Paper>
|
||
|
<div className={className}>{renderTabPanels()}</div>
|
||
|
</>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
TabNav.propTypes = {
|
||
|
tabData: PropTypes.array.isRequired,
|
||
|
className: PropTypes.string,
|
||
|
startingTab: PropTypes.number,
|
||
|
};
|
||
|
|
||
|
export default TabNav;
|