Skip to content

Commit 62aac25

Browse files
committed
[WIP] - update table component
1 parent 1b4d4f3 commit 62aac25

File tree

4 files changed

+85
-55
lines changed

4 files changed

+85
-55
lines changed

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

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,25 @@ import {
88
TableRow,
99
} from "@mui/material";
1010
import type { ComponentProps, ComponentState } from "@/index";
11+
import type { SxProps } from "@mui/system";
1112

1213
interface TableCellProps {
1314
id: string | number;
14-
size?: "medium" | "small" | string;
15+
size?: "medium" | "small";
1516
align?: "inherit" | "left" | "center" | "right" | "justify";
17+
padding?: "checkbox" | "none" | "normal";
18+
sx?: SxProps;
1619
}
1720

1821
interface TableColumn extends TableCellProps {
1922
label: string;
2023
}
2124

22-
type row = {
23-
[key: string]: string | number | boolean | undefined;
24-
};
25-
26-
interface TableRowData extends TableCellProps {
27-
data: row;
28-
}
29-
3025
interface TableState extends ComponentState {
31-
rows?: TableRowData[];
26+
rows?: (string | number | boolean | undefined)[][];
3227
columns?: TableColumn[];
3328
hover?: boolean;
29+
stickyHeader?: boolean;
3430
}
3531

3632
interface TableProps extends ComponentProps, TableState {}
@@ -42,6 +38,7 @@ export const Table = ({
4238
rows,
4339
columns,
4440
hover,
41+
stickyHeader,
4542
onChange,
4643
}: TableProps) => {
4744
if (!columns || columns.length === 0) {
@@ -52,42 +49,83 @@ export const Table = ({
5249
return <div>No rows provided.</div>;
5350
}
5451

55-
const handleRowClick = (row: TableRowData) => {
52+
const handleRowClick = (row: (string | number | boolean | undefined)[]) => {
53+
const rowData = row.reduce(
54+
(acc, cell, cellIndex) => {
55+
const columnId = columns[cellIndex]?.id;
56+
if (columnId) {
57+
acc[columnId] = cell;
58+
}
59+
return acc;
60+
},
61+
{} as Record<string, string | number | boolean | undefined>,
62+
);
5663
if (id) {
5764
onChange({
5865
componentType: type,
5966
id: id,
6067
property: "value",
61-
value: row,
68+
value: rowData,
6269
});
6370
}
6471
};
6572

73+
// function descendingComparator<T>(a: T, b: T, orderBy: keyof T) {
74+
// if (b[orderBy] < a[orderBy]) {
75+
// return -1;
76+
// }
77+
// if (b[orderBy] > a[orderBy]) {
78+
// return 1;
79+
// }
80+
// return 0;
81+
// }
82+
//
83+
// type Order = "asc" | "desc";
84+
//
85+
// function getComparator<Key extends keyof any>(
86+
// order: Order,
87+
// orderBy: Key,
88+
// ): (
89+
// a: { [key in Key]: number | string },
90+
// b: { [key in Key]: number | string },
91+
// ) => number {
92+
// return order === "desc"
93+
// ? (a, b) => descendingComparator(a, b, orderBy)
94+
// : (a, b) => -descendingComparator(a, b, orderBy);
95+
// }
96+
6697
return (
67-
<TableContainer component={Paper} style={style} id={id}>
68-
<MuiTable>
98+
<TableContainer component={Paper} sx={style} id={id}>
99+
<MuiTable stickyHeader={stickyHeader}>
69100
<TableHead>
70101
<TableRow>
71102
{columns.map((column) => (
72-
<TableCell key={column.id} align={column.align || "left"}>
103+
<TableCell
104+
key={column.id}
105+
align={column.align || "inherit"}
106+
size={column.size || "medium"}
107+
padding={column.padding}
108+
>
73109
{column.label}
74110
</TableCell>
75111
))}
76112
</TableRow>
77113
</TableHead>
78114
<TableBody>
79-
{rows.map((row) => (
115+
{rows.map((row, row_index) => (
80116
<TableRow
81117
hover={hover}
82-
key={row.id}
118+
key={row_index}
83119
onClick={() => handleRowClick(row)}
84120
>
85-
{columns.map((column) => (
121+
{row?.map((item, item_index) => (
86122
<TableCell
87-
key={`${row.id}-${column.id}`}
88-
align={column.align || "left"}
123+
key={item_index}
124+
align={columns[item_index].align || "inherit"}
125+
size={columns[item_index].size || "medium"}
126+
padding={columns[item_index].padding}
89127
>
90-
{row.data[column.id]}
128+
{item}
91129
</TableCell>
92130
))}
93131
</TableRow>
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import List, Literal, TypedDict, Optional, Dict, Union
2+
from typing import Literal, TypedDict
33
from chartlets import Component
44

55

@@ -15,20 +15,16 @@ class TableCellProps(TypedDict, total=False):
1515
align: Literal["inherit", "left", "center", "right", "justify"] | None
1616
"""The alignment of the cell content."""
1717

18+
# sortDirection:
19+
1820

1921
class TableColumn(TableCellProps):
2022
"""Defines a column in the table."""
2123

2224
label: str
2325
"""The display label for the column header."""
2426

25-
26-
class TableRowData(TableCellProps):
27-
"""Defines a row in the table."""
28-
29-
data: dict[str, Union[str, int, float, bool, None]]
30-
"""The data for the row, as a dictionary where keys are the column ids."""
31-
27+
type TableRow = list[list[str | int | float | bool | None]]
3228

3329
@dataclass(frozen=True)
3430
class Table(Component):
@@ -37,8 +33,11 @@ class Table(Component):
3733
columns: list[TableColumn] | None = None
3834
"""The columns to display in the table."""
3935

40-
rows: list[TableRowData] | None = None
36+
rows: TableRow | None = None
4137
"""The rows of data to display in the table."""
4238

4339
hover: bool | None = None
4440
"""A boolean indicating whether to highlight a row when hovered over"""
41+
42+
stickyHeader: bool | None = None
43+
"""A boolean to set the header of the table sticky"""

chartlets.py/demo/my_extension/my_panel_6.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from chartlets import Component, Input, State, Output
1+
from chartlets import Component, Input, Output
22
from chartlets.components import Box, Typography, Table
33

44
from server.context import Context
55
from server.panel import Panel
66

7-
from chartlets.components.table import TableColumn, TableRowData
7+
from chartlets.components.table import TableColumn, TableRow
88

99
panel = Panel(__name__, title="Panel F")
1010

@@ -15,25 +15,21 @@ def render_panel(
1515
ctx: Context,
1616
) -> Component:
1717
columns: list[TableColumn] = [
18-
{"id": "id", "label": "ID"},
19-
{"id": "firstName", "label": "First Name", "align": "left"},
18+
{"id": "id", "label": "ID", "sortDirection": "desc"},
19+
{
20+
"id": "firstName",
21+
"label": "First Name",
22+
"align": "left",
23+
"sortDirection": "desc",
24+
},
2025
{"id": "lastName", "label": "Last Name", "align": "center"},
2126
{"id": "age", "label": "Age"},
2227
]
2328

24-
rows: list[TableRowData] = [
25-
{
26-
"id": 1,
27-
"data": {"id": "1", "firstName": "John", "lastName": "Doe", "age": 30},
28-
},
29-
{
30-
"id": 2,
31-
"data": {"id": "2", "firstName": "Jane", "lastName": "Smith", "age": 25},
32-
},
33-
{
34-
"id": 3,
35-
"data": {"id": "3", "firstName": "Peter", "lastName": "Jones", "age": 40},
36-
},
29+
rows: TableRow = [
30+
["1", "John", "Doe", 30],
31+
["2", "Jane", "Smith", 25],
32+
["3", "Peter", "Jones", 40],
3733
]
3834

3935
table = Table(id="table", rows=rows, columns=columns, hover=True)

chartlets.py/tests/components/table_test.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from chartlets.components.table import TableColumn, TableRowData, Table
1+
from chartlets.components.table import TableColumn, Table, TableRow
22
from tests.component_test import make_base
33

44

@@ -18,13 +18,10 @@ def test_is_json_serializable_empty(self):
1818
{"id": "lastName", "label": "Last Name"},
1919
{"id": "age", "label": "Age"},
2020
]
21-
rows: list[TableRowData] = [
22-
{
23-
"id": "1",
24-
"data": {"firstName": "John", "lastName": "Doe", "age": 30},
25-
},
26-
{"id": "2", "data": {"firstName": "Jane", "lastName": "Smith", "age": 25}},
27-
{"id": 3, "data": {"firstName": "Johnie", "lastName": "Undoe", "age": 40}},
21+
rows: TableRow = [
22+
["John", "Doe", 30],
23+
["Jane", "Smith", 25],
24+
["Johnie", "Undoe", 40],
2825
]
2926
hover: bool = True
3027
style = {"background-color": "lightgray", "width": "100%"}

0 commit comments

Comments
 (0)