Skip to content

Commit f03d85e

Browse files
committed
update Tabs component
1 parent 53d2885 commit f03d85e

File tree

2 files changed

+65
-26
lines changed

2 files changed

+65
-26
lines changed

chartlets.js/packages/lib/src/plugins/mui/Tabs.tsx

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import MuiIcon from "@mui/material/Icon";
22
import MuiTabs from "@mui/material/Tabs";
33
import MuiTab from "@mui/material/Tab";
44

5-
import type { ComponentProps, ComponentState } from "@/index";
5+
import { type ComponentProps, type ComponentState } from "@/index";
66
import type { SyntheticEvent } from "react";
7+
import { Box } from "@/plugins/mui/Box";
78
import { isString } from "@/utils/isString";
89
import { isComponentState } from "@/types/state/component";
910

@@ -12,6 +13,7 @@ interface TabState {
1213
label?: string;
1314
icon?: string;
1415
disabled?: boolean;
16+
children?: ComponentProps[];
1517
}
1618

1719
interface TabsState extends ComponentState {
@@ -41,19 +43,37 @@ export function Tabs({
4143
}
4244
};
4345
return (
44-
<MuiTabs id={id} style={style} value={value} onChange={handleChange}>
45-
{tabItems?.map((tab) => {
46+
<div>
47+
<MuiTabs id={id} style={style} value={value} onChange={handleChange}>
48+
{tabItems?.map((tab, index) => {
49+
const tabState = isComponentState(tab)
50+
? (tab as TabState)
51+
: undefined;
52+
return (
53+
<MuiTab
54+
key={index}
55+
label={tabState ? tabState.label : isString(tab) ? tab : ""}
56+
icon={
57+
tabState && tabState.icon && <MuiIcon>{tabState.icon}</MuiIcon>
58+
}
59+
disabled={disabled || (tabState && tabState.disabled)}
60+
/>
61+
);
62+
})}
63+
</MuiTabs>
64+
{tabItems?.map((tab, index) => {
4665
const tabState = isComponentState(tab) ? (tab as TabState) : undefined;
4766
return (
48-
<MuiTab
49-
label={tabState ? tabState.label : isString(tab) ? tab : ""}
50-
icon={
51-
tabState && tabState.icon && <MuiIcon>{tabState.icon}</MuiIcon>
52-
}
53-
disabled={disabled || (tabState && tabState.disabled)}
54-
/>
67+
value === index && (
68+
<Box
69+
key={index}
70+
type={type}
71+
onChange={onChange}
72+
children={tabState?.children ?? undefined}
73+
/>
74+
)
5575
);
5676
})}
57-
</MuiTabs>
77+
</div>
5878
);
5979
}
Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import altair as alt
2-
import pandas as pd
2+
33
from chartlets import Component, Input, State, Output
4-
from chartlets.components import Tabs, Tab, Typography, Box, VegaChart
4+
from chartlets.components import (Tabs, Tab, Typography, Box,
5+
VegaChart, Table)
6+
from chartlets.components.table import TableColumn, TableRow
57

68
from server.context import Context
79
from server.panel import Panel
@@ -17,26 +19,43 @@ def render_panel(
1719
) -> Component:
1820
dataset = ctx.datasets.get(selected_dataset_id)
1921

20-
c = (
22+
columns: list[TableColumn] = [
23+
{"id": "id", "label": "ID", "sortDirection": "desc"},
24+
{
25+
"id": "firstName",
26+
"label": "First Name",
27+
"align": "left",
28+
"sortDirection": "desc",
29+
},
30+
{"id": "lastName", "label": "Last Name", "align": "center"},
31+
{"id": "age", "label": "Age"},
32+
]
33+
34+
rows: TableRow = [
35+
["1", "John", "Doe", 30],
36+
["2", "Jane", "Smith", 25],
37+
["3", "Peter", "Jones", 40],
38+
]
39+
40+
table = Table(id="table", rows=rows, columns=columns, hover=True)
41+
42+
info_text = Typography(id="info_text", children=["This is a text."])
43+
chart = VegaChart(
44+
id="chart", chart=(
2145
alt.Chart(dataset)
2246
.mark_bar()
2347
.encode(
2448
x=alt.X("x:N", title="x"),
2549
y=alt.Y("a:Q", title="a"))
50+
.properties(width=290, height=300, title="Vega charts")
51+
), style={"flexGrow": 1}
2652
)
2753

28-
chart = VegaChart(
29-
id="chart1", chart=c, style={"flexGrow": 1}
30-
)
31-
32-
info_text = Typography(id="info_text", children=["hallo"])
33-
34-
tab1=Tab(id = "tab1",label="Plot 1", children=["Hallo"])
35-
tab2=Tab(id = "tab2",label="Plot 2", children=[info_text])
36-
tab3 = Tab(id="tab3", label="Plot 3", children=[chart])
37-
38-
select = Tabs(id = "tab", value = 0, children=[tab1,tab2,tab3])
54+
tab1 = Tab(id = "tab1", label="Tab 1", children=[table])
55+
tab2 = Tab(id = "tab2", label="Tab 2", children=[info_text])
56+
tab3 = Tab(id="tab3", label="Tab 3", children=[chart])
3957

58+
tabs = Tabs(id = "tab", value = 0, children=[tab1,tab2,tab3])
4059

4160
return Box(
4261
style={
@@ -45,6 +64,6 @@ def render_panel(
4564
"width": "100%",
4665
"height": "100%",
4766
},
48-
children=[chart, info_text, select,tab2],
67+
children=[ tabs ],
4968
)
5069

0 commit comments

Comments
 (0)