Table now renders an arbitrary number of columns.

This commit is contained in:
M M committed 2024-07-11 13:20:15 -07:00
1 parent 14bd0da891
commit c9a3f54a65
2 files changed
+36 -18

No files matched your search

+29 -17
View File
@@ -44,10 +44,11 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
/** /**
* Customized table component displaying incident history. * Customized table component displaying incident history.
* @param {object} props - The props object. * @param {object} props - The props object.
* @param {array} props.data - The array of data objects containing name, date, and message. * @param {array} props.data - The array of data objects containing JSX elements for name, and strings for date and message.
* @param {array} props.columns - The array of column headers and alignments.
* @returns {JSX.Element} The JSX element representing the customized table. * @returns {JSX.Element} The JSX element representing the customized table.
*/ */
export default function StatusTable({ data }) { export default function StatusTable({ data, columns }) {
const theme = useTheme(); const theme = useTheme();
return ( return (
@@ -66,19 +67,27 @@ export default function StatusTable({ data }) {
<Table sx={{ minWidth: 700 }} aria-label="customized table"> <Table sx={{ minWidth: 700 }} aria-label="customized table">
<TableHead> <TableHead>
<TableRow> <TableRow>
<StyledTableCell>Status</StyledTableCell> {columns.map((column, index) => (
<StyledTableCell align="right">Date & Time</StyledTableCell> <StyledTableCell key={index} align={column.align || 'left'}>
<StyledTableCell align="right">Message</StyledTableCell> {column.header}
</StyledTableCell>
))}
</TableRow> </TableRow>
</TableHead> </TableHead>
<TableBody> <TableBody>
{data.map((row, index) => ( {data.map((row, rowIndex) => (
<StyledTableRow key={index}> <StyledTableRow key={rowIndex}>
<StyledTableCell component="th" scope="row"> {columns.map((column, colIndex) => (
{row.name} <StyledTableCell key={colIndex} align={column.align || 'left'}>
</StyledTableCell> {column.id === 'name' ? (
<StyledTableCell align="right">{row.date}</StyledTableCell> <div style={row.name.props.customStyles}>
<StyledTableCell align="right">{row.message}</StyledTableCell> {row.name}
</div>
) : (
row[column.id]
)}
</StyledTableCell>
))}
</StyledTableRow> </StyledTableRow>
))} ))}
</TableBody> </TableBody>
@@ -89,9 +98,12 @@ export default function StatusTable({ data }) {
} }
StatusTable.propTypes = { StatusTable.propTypes = {
data: PropTypes.arrayOf(PropTypes.shape({ data: PropTypes.arrayOf(PropTypes.object).isRequired,
name: PropTypes.node.isRequired, columns: PropTypes.arrayOf(
date: PropTypes.string.isRequired, PropTypes.shape({
message: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
})).isRequired, header: PropTypes.node.isRequired,
align: PropTypes.oneOf(['left', 'right', 'center']),
})
).isRequired,
}; };
+7 -1
View File
@@ -65,6 +65,12 @@ const sampleData = [
{ name: <StatusLabel status="Down" customStyles={{ backgroundColor: '#fff9f9', borderColor: '#ffcac6', color: '#344054' }} />, date: '2024-03-14 21:41:09', message: 'HTTP 350 - NOK' }, { name: <StatusLabel status="Down" customStyles={{ backgroundColor: '#fff9f9', borderColor: '#ffcac6', color: '#344054' }} />, date: '2024-03-14 21:41:09', message: 'HTTP 350 - NOK' },
]; ];
const columns = [
{ id: 'name', header: 'Status' },
{ id: 'date', header: 'Date & Time' },
{ id: 'message', header: 'Message' },
];
/** /**
* Customized table component displaying incident history with a filter. * Customized table component displaying incident history with a filter.
* @returns {JSX.Element} The JSX element representing the customized table with a filter. * @returns {JSX.Element} The JSX element representing the customized table with a filter.
@@ -90,7 +96,7 @@ export default function CustomizedTables() {
</Typography> </Typography>
<Filter /> <Filter />
</Box> </Box>
<StatusTable data={sampleData} /> <StatusTable data={sampleData} columns={columns} />
</Box> </Box>
); );
} }