import React from "react";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import Paper from "@mui/material/Paper";
import { NoDataOverlay } from "@/components/noDataOverlay";
import { StyledTableCell, StyledTableRow } from "./style";
import { ICustomTable, CustomTableConfiguration } from "@/types";
import { Checkbox, Skeleton } from "@mui/material";
import ErrorBoundary from "@/components/error_boundary";
const CustomTable = ({ configuration, data, loading, tkey }: ICustomTable) => {
if (loading)
return ;
// display empty icon in case there is no data
if (!data || data.length === 0)
return ;
const renderTableCell = (
value: any,
cellKey: string,
render?: (param: any) => void | undefined,
) => {
let renderedValue = value;
// cover use case if the data is an array
if (Array.isArray(value)) renderedValue = value.join(", ");
// cover use case if the data is an boolean
if (typeof value === "boolean")
renderedValue = ;
// cover use case if we want to render a component
if (render) renderedValue = render(value);
if (typeof renderedValue === "object" && render === undefined) {
console.warn("Missing render function for column " + cellKey);
}
return (
{renderedValue}
);
};
return (
{configuration.map((header: CustomTableConfiguration) => (
{header.label}
))}
{data.map((data: any, rowIndex: number) => (
{configuration.map(
(column: CustomTableConfiguration, columnIndex: number) => {
const cellValue: any = data[column.key];
const cellKey = tkey + ":" + column.key + ":" + rowIndex;
const renderComponent = column?.render;
return renderTableCell(
cellValue,
cellKey + ":" + columnIndex,
renderComponent,
);
},
)}
))}
);
};
export default CustomTable;