-
Notifications
You must be signed in to change notification settings - Fork 0
Modify Tabs and create demo panel
#129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
53d2885
f03d85e
cbab032
4f20f09
d6a515a
4aed331
19ad7ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import MuiBox from "@mui/material/Box"; | ||
| import MuiIcon from "@mui/material/Icon"; | ||
| import MuiTabs from "@mui/material/Tabs"; | ||
| import MuiTab from "@mui/material/Tab"; | ||
|
|
||
| import type { ComponentProps, ComponentState } from "@/index"; | ||
| import type { SyntheticEvent } from "react"; | ||
| import { Box } from "@/plugins/mui/Box"; | ||
| import { isString } from "@/utils/isString"; | ||
| import { isComponentState } from "@/types/state/component"; | ||
|
|
||
|
|
@@ -12,6 +14,7 @@ interface TabState { | |
| label?: string; | ||
| icon?: string; | ||
| disabled?: boolean; | ||
| children?: ComponentProps[]; | ||
| } | ||
|
|
||
| interface TabsState extends ComponentState { | ||
|
|
@@ -41,19 +44,40 @@ export function Tabs({ | |
| } | ||
| }; | ||
| return ( | ||
| <MuiTabs id={id} style={style} value={value} onChange={handleChange}> | ||
| {tabItems?.map((tab) => { | ||
| <MuiBox sx={{ width: "100%" }}> | ||
| <MuiBox sx={{ borderBottom: 1, borderColor: "divider" }}> | ||
| <MuiTabs id={id} style={style} value={value} onChange={handleChange}> | ||
| {tabItems?.map((tab, index) => { | ||
| const tabState = isComponentState(tab) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can make this cleaner code if you introduce a type guard |
||
| ? (tab as TabState) | ||
| : undefined; | ||
| return ( | ||
| <MuiTab | ||
| key={index} | ||
| label={tabState ? tabState.label : isString(tab) ? tab : ""} | ||
| icon={ | ||
| tabState && | ||
| tabState.icon && <MuiIcon>{tabState.icon}</MuiIcon> | ||
| } | ||
| disabled={disabled || (tabState && tabState.disabled)} | ||
| /> | ||
| ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can make this cleaner by constructing separate Even better would be to ensure / assert upfront whether |
||
| })} | ||
| </MuiTabs> | ||
| </MuiBox> | ||
| {tabItems?.map((tab, index) => { | ||
| const tabState = isComponentState(tab) ? (tab as TabState) : undefined; | ||
| return ( | ||
| <MuiTab | ||
| label={tabState ? tabState.label : isString(tab) ? tab : ""} | ||
| icon={ | ||
| tabState && tabState.icon && <MuiIcon>{tabState.icon}</MuiIcon> | ||
| } | ||
| disabled={disabled || (tabState && tabState.disabled)} | ||
| /> | ||
| value === index && ( | ||
| <Box | ||
| key={index} | ||
| type={type} | ||
| onChange={onChange} | ||
| children={tabState?.children ?? undefined} | ||
| /> | ||
| ) | ||
| ); | ||
| })} | ||
| </MuiTabs> | ||
| </MuiBox> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import altair as alt | ||
|
|
||
| from chartlets import Component, Input, State, Output | ||
| from chartlets.components import (Tabs, Tab, Typography, Box, | ||
| VegaChart, Table) | ||
| from chartlets.components.table import TableColumn, TableRow | ||
|
|
||
| from server.context import Context | ||
| from server.panel import Panel | ||
|
|
||
|
|
||
| panel = Panel(__name__, title="Panel H") | ||
|
|
||
|
|
||
| @panel.layout(State("@app", "selectedDatasetId")) | ||
| def render_panel( | ||
| ctx: Context, | ||
| selected_dataset_id: str = "", | ||
| ) -> Component: | ||
| dataset = ctx.datasets.get(selected_dataset_id) | ||
|
|
||
| columns: list[TableColumn] = [ | ||
| {"id": "id", "label": "ID", "sortDirection": "desc"}, | ||
| { | ||
| "id": "firstName", | ||
| "label": "First Name", | ||
| "align": "left", | ||
| "sortDirection": "desc", | ||
| }, | ||
| {"id": "lastName", "label": "Last Name", "align": "center"}, | ||
| {"id": "age", "label": "Age"}, | ||
| ] | ||
|
|
||
| rows: TableRow = [ | ||
| ["1", "John", "Doe", 30], | ||
| ["2", "Jane", "Smith", 25], | ||
| ["3", "Peter", "Jones", 40], | ||
| ] | ||
|
|
||
| table = Table(id="table", rows=rows, columns=columns, hover=True) | ||
|
|
||
| info_text = Typography(id="info_text", children=["This is a text."]) | ||
| chart = VegaChart( | ||
| id="chart", chart=( | ||
| alt.Chart(dataset) | ||
| .mark_bar() | ||
| .encode( | ||
| x=alt.X("x:N", title="x"), | ||
| y=alt.Y("a:Q", title="a")) | ||
| .properties(width=290, height=300, title="Vega charts") | ||
| ), style={"flexGrow": 1} | ||
| ) | ||
|
|
||
| tab1 = Tab(id = "tab1", label="Tab 1", children=[table]) | ||
| tab2 = Tab(id = "tab2", label="Tab 2", children=[info_text]) | ||
| tab3 = Tab(id="tab3", label="Tab 3", children=[chart]) | ||
|
|
||
| tabs = Tabs(id = "tabs", value = 0, children=[tab1,tab2,tab3]) | ||
|
|
||
| return Box( | ||
| style={ | ||
| "display": "flex", | ||
| "flexDirection": "column", | ||
| "width": "100%", | ||
| "height": "100%", | ||
| }, | ||
| children=[ tabs ], | ||
| ) | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.