File tree Expand file tree Collapse file tree 1 file changed +99
-0
lines changed
chartlets.js/packages/lib/src/plugins/mui Expand file tree Collapse file tree 1 file changed +99
-0
lines changed Original file line number Diff line number Diff line change 1+ import {
2+ Paper ,
3+ Table as MuiTable ,
4+ TableBody ,
5+ TableCell ,
6+ TableContainer ,
7+ TableHead ,
8+ TableRow ,
9+ } from "@mui/material" ;
10+ import type { ComponentProps , ComponentState } from "@/index" ;
11+
12+ interface TableCellProps {
13+ id : string | number ;
14+ size ?: "medium" | "small" | string ;
15+ align ?: "inherit" | "left" | "center" | "right" | "justify" ;
16+ }
17+
18+ interface TableColumn extends TableCellProps {
19+ label : string ;
20+ }
21+
22+ type row = {
23+ [ key : string ] : string | number | boolean | undefined ;
24+ } ;
25+
26+ interface TableRowData extends TableCellProps {
27+ data : row ;
28+ }
29+
30+ interface TableState extends ComponentState {
31+ rows ?: TableRowData [ ] ;
32+ columns ?: TableColumn [ ] ;
33+ hover ?: boolean ;
34+ }
35+
36+ interface TableProps extends ComponentProps , TableState { }
37+
38+ export const Table = ( {
39+ type,
40+ id,
41+ style,
42+ rows,
43+ columns,
44+ hover,
45+ onChange,
46+ } : TableProps ) => {
47+ if ( ! columns || columns . length === 0 ) {
48+ return < div > No columns provided.</ div > ;
49+ }
50+
51+ if ( ! rows || rows . length === 0 ) {
52+ return < div > No rows provided.</ div > ;
53+ }
54+
55+ const handleRowClick = ( row : TableRowData ) => {
56+ if ( id ) {
57+ onChange ( {
58+ componentType : type ,
59+ id : id ,
60+ property : "value" ,
61+ value : row ,
62+ } ) ;
63+ }
64+ } ;
65+
66+ return (
67+ < TableContainer component = { Paper } style = { style } id = { id } >
68+ < MuiTable >
69+ < TableHead >
70+ < TableRow >
71+ { columns . map ( ( column ) => (
72+ < TableCell key = { column . id } align = { column . align || "left" } >
73+ { column . label }
74+ </ TableCell >
75+ ) ) }
76+ </ TableRow >
77+ </ TableHead >
78+ < TableBody >
79+ { rows . map ( ( row ) => (
80+ < TableRow
81+ hover = { hover }
82+ key = { row . id }
83+ onClick = { ( ) => handleRowClick ( row ) }
84+ >
85+ { columns . map ( ( column ) => (
86+ < TableCell
87+ key = { `${ row . id } -${ column . id } ` }
88+ align = { column . align || "left" }
89+ >
90+ { row . data [ column . id ] }
91+ </ TableCell >
92+ ) ) }
93+ </ TableRow >
94+ ) ) }
95+ </ TableBody >
96+ </ MuiTable >
97+ </ TableContainer >
98+ ) ;
99+ } ;
You can’t perform that action at this time.
0 commit comments