import { TableContainer, Paper, Table, TableHead, TableRow, TableCell, TableBody, Pagination, PaginationItem, } from "@mui/material"; import React, { useState } from "react"; import PropTypes from "prop-types"; import "./index.css"; import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded"; import ArrowForwardRoundedIcon from "@mui/icons-material/ArrowForwardRounded"; /** * BasicTable Component * Renders a table with optional pagination. * * @component * @param {Object} props - Component props. * @param {Object} props.data - Data for the table including columns and rows. * @param {Array} props.data.cols - Array of objects for column headers. * @param {number} props.data.cols[].id - Unique identifier for the column. * @param {string} props.data.cols[].name - Name of the column to display as header. * @param {Array} props.data.rows - Array of row objects. * @param {number} props.data.rows[].id - Unique identifier for the row. * @param {Array} props.data.rows[].data - Array of cell data objects for the row. * @param {number} props.data.rows[].data[].id - Unique identifier for the cell. * @param {JSX.Element} props.data.rows[].data[].data - The content to display in the cell. * @param {function} props.data.rows.data.handleClick - Function to call when the row is clicked. * @param {boolean} [props.paginated=false] - Flag to enable pagination. * * @example * const data = { * cols: [ * { id: 1, name: "First Col" }, * { id: 2, name: "Second Col" }, * { id: 3, name: "Third Col" }, * { id: 4, name: "Fourth Col" }, * ], * rows: [ * { * id: 1, * data: [ * { id: 1, data:
Data for Row 1 Col 1
}, * { id: 2, data:
Data for Row 1 Col 2
}, * { id: 3, data:
Data for Row 1 Col 3
}, * { id: 4, data:
Data for Row 1 Col 4
}, * ], * }, * { * id: 2, * data: [ * { id: 5, data:
Data for Row 2 Col 1
}, * { id: 6, data:
Data for Row 2 Col 2
}, * { id: 7, data:
Data for Row 2 Col 3
}, * { id: 8, data:
Data for Row 2 Col 4
}, * ], * }, * ], * }; * * */ const BasicTable = ({ data, paginated }) => { // Add headers to props validation const [page, setPage] = useState(0); const rowsPerPage = 5; const handleChangePage = (event, newPage) => { setPage(newPage); }; let displayData = []; if (data && data.rows) { displayData = paginated ? data.rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) : data.rows; } if (!data || !data.cols || !data.rows) { return
No data
; } return ( <> {data.cols.map((col) => ( {col.name} ))} {displayData.map((row) => { return ( {row.data.map((cell) => { return {cell.data}; })} ); })}
{paginated === true && ( handleChangePage(event, value - 1)} shape="rounded" renderItem={(item) => ( )} /> )} ); }; BasicTable.propTypes = { data: PropTypes.object.isRequired, paginated: PropTypes.bool, }; export default BasicTable;