Skip to content

Commit

Permalink
feat(ui-common): create TabsView component
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Jan 9, 2024
1 parent 01aed56 commit 931380e
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions webapp/src/components/common/TabsView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable react/no-array-index-key */
import { TabContext, TabList, TabListProps, TabPanel } from "@mui/lab";
import { Tab } from "@mui/material";
import { useState } from "react";
import { mergeSxProp } from "../../utils/muiUtils";

interface TabsViewProps {
items: Array<{
label: string;
content?: React.ReactNode;
}>;
TabListProps?: TabListProps;
}

function TabsView({ items, TabListProps }: TabsViewProps) {
const [value, setValue] = useState("0");

////////////////////////////////////////////////////////////////
// Event Handlers
////////////////////////////////////////////////////////////////

const handleChange = (event: React.SyntheticEvent, newValue: string) => {
setValue(newValue);
TabListProps?.onChange?.(event, newValue);
};

////////////////////////////////////////////////////////////////
// JSX
////////////////////////////////////////////////////////////////

return (
<TabContext value={value}>
<TabList
{...TabListProps}
onChange={handleChange}
sx={mergeSxProp(
{ borderBottom: 1, borderColor: "divider" },
TabListProps?.sx,
)}
>
{items.map(({ label }, index) => (
<Tab key={index} label={label} value={index.toString()} />
))}
</TabList>
{items.map(({ content }, index) => (
<TabPanel
key={index}
value={index.toString()}
sx={{
px: 0,
}}
>
{content}
</TabPanel>
))}
</TabContext>
);
}

export default TabsView;

0 comments on commit 931380e

Please sign in to comment.