Files
Checkmate/Client/src/Components/Table/index.jsx
T

128 lines
3.3 KiB
React
Raw Normal View History

2025-01-03 10:31:38 -08:00
import {
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
} from "@mui/material";
import PropTypes from "prop-types";
import { useTheme } from "@emotion/react";
/**
* @typedef {Object} Header
* @property {number|string} id - The unique identifier for the header.
* @property {React.ReactNode} content - The content to display in the header cell.
* @property {Function} render - A function to render the cell content for a given row.
*/
/**
* @typedef {Object} Config
* @property {Function} onRowClick - A function to be called when a row is clicked, receiving the row data as an argument.
* @property {Object} rowSX - Style object for the table row.
*/
/**
* DataTable component renders a table with headers and data.
*
* @param {Object} props - The component props.
* @param {Header[]} props.headers - An array of header objects, each containing an `id`, `content`, and `render` function.
* @param {Array} props.data - An array of data objects, each representing a row.
* @returns {JSX.Element} The rendered table component.
*/
2025-01-03 11:17:55 -08:00
const DataTable = ({ headers, data, config = { emptyView: "No data" } }) => {
2025-01-03 10:31:38 -08:00
const theme = useTheme();
2025-01-03 11:14:23 -08:00
if ((headers?.length ?? 0) === 0) {
2025-01-03 10:31:38 -08:00
return "No data";
}
return (
<TableContainer component={Paper}>
2025-01-12 13:33:11 -05:00
<Table
stickyHeader
sx={{
2025-01-14 18:54:00 -05:00
"&.MuiTable-root :is(.MuiTableHead-root, .MuiTableBody-root) :is(th, td)": {
paddingLeft: theme.spacing(8),
},
"& :is(th)": {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
fontWeight: 600,
fontSize: "12px",
},
"& :is(td)": {
backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastTextSecondary,
},
2025-01-12 13:33:11 -05:00
}}
>
2025-01-14 18:54:00 -05:00
<TableHead>
2025-01-03 10:31:38 -08:00
<TableRow>
{headers.map((header, index) => (
<TableCell
key={header.id}
align={index === 0 ? "left" : "center"}
>
{header.content}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
2025-01-03 12:09:05 -08:00
{(data?.length ?? 0) === 0 ? (
2025-01-03 11:14:23 -08:00
<TableRow>
<TableCell
colSpan={headers.length}
align="center"
2025-01-03 10:31:38 -08:00
>
2025-01-03 11:14:23 -08:00
{config.emptyView}
</TableCell>
</TableRow>
) : (
data.map((row) => {
2025-01-21 15:39:49 -08:00
const key = row.id || row._id || Math.random();
2025-01-03 11:14:23 -08:00
return (
<TableRow
2025-01-21 15:39:49 -08:00
key={key}
2025-01-03 11:14:23 -08:00
sx={config?.rowSX ?? {}}
2025-01-22 12:40:29 -08:00
onClick={config?.onRowClick ? () => config.onRowClick(row) : null}
2025-01-03 11:14:23 -08:00
>
{headers.map((header, index) => {
return (
<TableCell
align={index === 0 ? "left" : "center"}
key={header.id}
>
{header.render(row)}
</TableCell>
);
})}
</TableRow>
);
})
)}
2025-01-03 10:31:38 -08:00
</TableBody>
</Table>
</TableContainer>
);
};
DataTable.propTypes = {
headers: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
content: PropTypes.node.isRequired,
render: PropTypes.func.isRequired,
})
).isRequired,
data: PropTypes.array.isRequired,
config: PropTypes.shape({
onRowClick: PropTypes.func.isRequired,
rowSX: PropTypes.object,
2025-01-03 11:14:23 -08:00
emptyView: PropTypes.node,
2025-01-03 10:36:53 -08:00
}),
2025-01-03 10:31:38 -08:00
};
export default DataTable;