@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
56 lines (55 loc) • 2.83 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import MuiTab from '@mui/material/Tab';
import MuiTabs from '@mui/material/Tabs';
import Typography from '@mui/material/Typography';
import React from 'react';
import { useId } from '../../lib/util';
/**
* A scrollable tab navigation component using Material UI,
* with support for default tab selection, custom styles,
* and dynamic content rendering.
*
* @param props - The properties to configure the Tabs component.
* @returns A tabbed interface with selectable content panels.
*/
export default function Tabs(props) {
const { tabs, tabProps = {}, defaultIndex = 0, onTabChanged = null, ariaLabel } = props;
const [tabIndex, setTabIndex] = React.useState(defaultIndex && Math.min(defaultIndex, 0));
/**
* Handles tab change events.
*
* @param event - The event triggered by selecting a new tab.
* @param newValue - The index of the newly selected tab.
*/
function handleTabChange(event, newValue) {
setTabIndex(newValue);
if (onTabChanged !== null) {
onTabChanged(newValue);
}
}
React.useEffect(() => {
if (defaultIndex === null) {
setTabIndex(false);
return;
}
setTabIndex(defaultIndex);
},
// eslint-disable-next-line
[defaultIndex]);
const uniqueIdSuffix = useId('tabs-');
return (_jsxs(React.Fragment, { children: [_jsx(MuiTabs, { value: tabIndex, onChange: handleTabChange, indicatorColor: "primary", textColor: "primary", "aria-label": ariaLabel, variant: "scrollable", centered: false, scrollButtons: "auto", sx: props.sx, ...tabProps, children: tabs.map(({ label }, i) => (_jsx(MuiTab, { label: label, sx: tabs?.length > 7
? {
minWidth: 150, // allows 8 tabs to show like on pods
}
: {}, id: `full-width-tab-${i}-${ariaLabel.replace(' ', '')}-${uniqueIdSuffix}`, "aria-controls": `full-width-tabpanel-${i}-${ariaLabel.replace(' ', '')}-${uniqueIdSuffix}` }, i))) }), tabs.map(({ component }, i) => (_jsx(TabPanel, { tabIndex: Number(tabIndex), index: i, id: `full-width-tabpanel-${i}-${ariaLabel.replace(' ', '')}-${uniqueIdSuffix}`, labeledBy: `full-width-tab-${i}-${ariaLabel.replace(' ', '')}-${uniqueIdSuffix}`, children: component }, i)))] }));
}
/**
* Renders a panel for the currently active tab.
*
* @param props - The properties of the tab panel.
* @returns A container showing the content if this panel is active.
*/
export function TabPanel(props) {
const { children, tabIndex, index, id, labeledBy } = props;
return (_jsx(Typography, { component: "div", role: "tabpanel", hidden: tabIndex !== index, id: id, "aria-labelledby": labeledBy, children: children }));
}