Skip to content

Commit

Permalink
Merge pull request #144 from flatironinstitute/tab-widget-rendering
Browse files Browse the repository at this point in the history
Track viewed tabs in TabWidget component
  • Loading branch information
magland authored Jul 24, 2024
2 parents 692b66a + b6477cf commit ad19ff6
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions gui/src/app/components/TabWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { FunctionComponent, useState } from "react";
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import Box from "@mui/material/Box";
import Tab from "@mui/material/Tab";
import Tabs from "@mui/material/Tabs";
import { FunctionComponent, useState } from "react";

type TabWidgetProps = {
labels: string[];
Expand All @@ -15,8 +15,17 @@ const TabWidget: FunctionComponent<TabWidgetProps> = ({ labels, children }) => {
}

const [index, setIndex] = useState(0);
const [tabsThatHaveBeenViewed, setTabsThatHaveBeenViewed] = useState<
number[]
>([index]);

const handleChange = (_: React.SyntheticEvent, newValue: number) => {
setTabsThatHaveBeenViewed((prev) => {
if (!prev.includes(newValue)) {
return [...prev, newValue];
}
return prev;
});
setIndex(newValue);
};

Expand All @@ -29,7 +38,12 @@ const TabWidget: FunctionComponent<TabWidgetProps> = ({ labels, children }) => {
</Tabs>
<Box flex="1" overflow="scroll">
{children.map((child, i) => (
<CustomTabPanel key={i} value={index} index={i}>
<CustomTabPanel
key={i}
value={index}
index={i}
mounted={tabsThatHaveBeenViewed.includes(i)}
>
{child}
</CustomTabPanel>
))}
Expand All @@ -41,11 +55,12 @@ const TabWidget: FunctionComponent<TabWidgetProps> = ({ labels, children }) => {
interface TabPanelProps {
children?: React.ReactNode;
index: number;
mounted: boolean;
value: number;
}

const CustomTabPanel = (props: TabPanelProps) => {
const { children, value, index, ...other } = props;
const { children, value, index, mounted, ...other } = props;

return (
<div
Expand All @@ -56,7 +71,7 @@ const CustomTabPanel = (props: TabPanelProps) => {
style={{ height: "100%", width: "100%" }}
{...other}
>
{value === index && <>{children}</>}
{mounted && <>{children}</>}
</div>
);
};
Expand Down

0 comments on commit ad19ff6

Please sign in to comment.