Skip to content

Commit 4d34fb4

Browse files
committed
add skeleton to table
1 parent 4a5af08 commit 4d34fb4

File tree

1 file changed

+31
-1
lines changed
  • chartlets.js/packages/lib/src/plugins/mui

1 file changed

+31
-1
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
} from "@mui/material";
1010
import type { ComponentProps, ComponentState } from "@/index";
1111
import type { SxProps } from "@mui/system";
12+
import { Skeleton } from "@/plugins/mui/Skeleton";
13+
import type { ReactElement } from "react";
14+
import { useLoadingState } from "@/hooks";
1215

1316
interface TableCellProps {
1417
id: string | number;
@@ -38,8 +41,19 @@ export const Table = ({
3841
columns,
3942
hover,
4043
stickyHeader,
44+
skeletonProps,
4145
onChange,
4246
}: TableProps) => {
47+
const loadingState = useLoadingState();
48+
console.log("loadingState", loadingState);
49+
if (!id) {
50+
return;
51+
}
52+
const isLoading = loadingState[id];
53+
if (isLoading == "failed") {
54+
return <div>An error occurred while loading the data.</div>;
55+
}
56+
4357
if (!columns || columns.length === 0) {
4458
return <div>No columns provided.</div>;
4559
}
@@ -69,7 +83,7 @@ export const Table = ({
6983
}
7084
};
7185

72-
return (
86+
const table: ReactElement | null = (
7387
<TableContainer component={Paper} sx={style} id={id}>
7488
<MuiTable stickyHeader={stickyHeader}>
7589
<TableHead>
@@ -107,4 +121,20 @@ export const Table = ({
107121
</MuiTable>
108122
</TableContainer>
109123
);
124+
125+
const isSkeletonRequired = skeletonProps !== undefined;
126+
if (!isSkeletonRequired) {
127+
return table;
128+
}
129+
const skeletonId = id + "-skeleton";
130+
return (
131+
<Skeleton
132+
isLoading={isLoading}
133+
id={skeletonId}
134+
style={style}
135+
{...skeletonProps}
136+
>
137+
{table}
138+
</Skeleton>
139+
);
110140
};

0 commit comments

Comments
 (0)