2024-08-19 17:34:10 -04:00
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import { useState, useEffect } from "react";
|
2024-07-12 14:30:39 -07:00
|
|
|
import {
|
|
|
|
|
TableContainer,
|
|
|
|
|
Paper,
|
|
|
|
|
Table,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableRow,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableBody,
|
2024-08-19 17:34:10 -04:00
|
|
|
TablePagination,
|
|
|
|
|
Box,
|
2024-07-12 14:30:39 -07:00
|
|
|
} from "@mui/material";
|
2024-08-19 17:34:10 -04:00
|
|
|
import LeftArrowDouble from "../../assets/icons/left-arrow-double.svg?react";
|
|
|
|
|
import RightArrowDouble from "../../assets/icons/right-arrow-double.svg?react";
|
|
|
|
|
import LeftArrow from "../../assets/icons/left-arrow.svg?react";
|
|
|
|
|
import RightArrow from "../../assets/icons/right-arrow.svg?react";
|
2024-08-19 18:49:14 -04:00
|
|
|
import SelectorVertical from "../../assets/icons/selector-vertical.svg?react";
|
2024-08-19 17:58:48 -04:00
|
|
|
import Button from "../Button";
|
2024-08-19 18:49:14 -04:00
|
|
|
import "./index.css";
|
2024-08-19 17:34:10 -04:00
|
|
|
|
|
|
|
|
const TablePaginationActions = (props) => {
|
|
|
|
|
const { count, page, rowsPerPage, onPageChange } = props;
|
|
|
|
|
|
|
|
|
|
const handleFirstPageButtonClick = (event) => {
|
|
|
|
|
onPageChange(event, 0);
|
|
|
|
|
};
|
|
|
|
|
const handleBackButtonClick = (event) => {
|
|
|
|
|
onPageChange(event, page - 1);
|
|
|
|
|
};
|
|
|
|
|
const handleNextButtonClick = (event) => {
|
|
|
|
|
onPageChange(event, page + 1);
|
|
|
|
|
};
|
|
|
|
|
const handleLastPageButtonClick = (event) => {
|
|
|
|
|
onPageChange(event, Math.max(0, Math.ceil(count / rowsPerPage) - 1));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2024-08-19 18:49:14 -04:00
|
|
|
<Box sx={{ flexShrink: 0, ml: "25px" }}>
|
2024-08-19 17:58:48 -04:00
|
|
|
<Button
|
|
|
|
|
level="secondary"
|
2024-08-19 18:49:14 -04:00
|
|
|
label=""
|
2024-08-19 17:58:48 -04:00
|
|
|
img={<LeftArrowDouble />}
|
2024-08-19 17:34:10 -04:00
|
|
|
onClick={handleFirstPageButtonClick}
|
|
|
|
|
disabled={page === 0}
|
|
|
|
|
aria-label="first page"
|
2024-08-19 17:58:48 -04:00
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
level="secondary"
|
2024-08-19 18:49:14 -04:00
|
|
|
label=""
|
2024-08-19 17:58:48 -04:00
|
|
|
img={<LeftArrow />}
|
2024-08-19 17:34:10 -04:00
|
|
|
onClick={handleBackButtonClick}
|
|
|
|
|
disabled={page === 0}
|
|
|
|
|
aria-label="previous page"
|
2024-08-19 17:58:48 -04:00
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
level="secondary"
|
2024-08-19 18:49:14 -04:00
|
|
|
label=""
|
2024-08-19 17:58:48 -04:00
|
|
|
img={<RightArrow />}
|
2024-08-19 17:34:10 -04:00
|
|
|
onClick={handleNextButtonClick}
|
|
|
|
|
disabled={page >= Math.ceil(count / rowsPerPage) - 1}
|
|
|
|
|
aria-label="next page"
|
2024-08-19 17:58:48 -04:00
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
level="secondary"
|
2024-08-19 18:49:14 -04:00
|
|
|
label=""
|
2024-08-19 17:58:48 -04:00
|
|
|
img={<RightArrowDouble />}
|
2024-08-19 17:34:10 -04:00
|
|
|
onClick={handleLastPageButtonClick}
|
|
|
|
|
disabled={page >= Math.ceil(count / rowsPerPage) - 1}
|
|
|
|
|
aria-label="last page"
|
2024-08-19 17:58:48 -04:00
|
|
|
/>
|
2024-08-19 17:34:10 -04:00
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-07-12 14:30:39 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* BasicTable Component
|
|
|
|
|
* Renders a table with optional pagination.
|
|
|
|
|
*
|
|
|
|
|
* @component
|
|
|
|
|
* @param {Object} props - Component props.
|
2024-07-16 13:21:45 -07:00
|
|
|
* @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.
|
2024-07-16 13:30:11 -07:00
|
|
|
* @param {function} props.data.rows.data.handleClick - Function to call when the row is clicked.
|
2024-07-16 13:21:45 -07:00
|
|
|
* @param {boolean} [props.paginated=false] - Flag to enable pagination.
|
2024-07-21 09:22:00 -07:00
|
|
|
* @param {boolean} [props.reversed=false] - Flag to enable reverse order.
|
2024-07-23 17:39:26 -04:00
|
|
|
* @param {number} props.rowsPerPage- Number of rows per page (table).
|
2024-07-12 14:30:39 -07:00
|
|
|
*
|
|
|
|
|
* @example
|
2024-07-16 13:21:45 -07:00
|
|
|
* 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: <div>Data for Row 1 Col 1</div> },
|
|
|
|
|
* { id: 2, data: <div>Data for Row 1 Col 2</div> },
|
|
|
|
|
* { id: 3, data: <div>Data for Row 1 Col 3</div> },
|
|
|
|
|
* { id: 4, data: <div>Data for Row 1 Col 4</div> },
|
|
|
|
|
* ],
|
|
|
|
|
* },
|
|
|
|
|
* {
|
|
|
|
|
* id: 2,
|
|
|
|
|
* data: [
|
|
|
|
|
* { id: 5, data: <div>Data for Row 2 Col 1</div> },
|
|
|
|
|
* { id: 6, data: <div>Data for Row 2 Col 2</div> },
|
|
|
|
|
* { id: 7, data: <div>Data for Row 2 Col 3</div> },
|
|
|
|
|
* { id: 8, data: <div>Data for Row 2 Col 4</div> },
|
|
|
|
|
* ],
|
|
|
|
|
* },
|
|
|
|
|
* ],
|
|
|
|
|
* };
|
2024-07-12 14:30:39 -07:00
|
|
|
*
|
2024-07-16 13:21:45 -07:00
|
|
|
* <BasicTable data={data} rows={rows} paginated={true} />
|
2024-07-12 14:30:39 -07:00
|
|
|
*/
|
|
|
|
|
|
2024-08-19 18:49:14 -04:00
|
|
|
const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
|
2024-07-12 14:30:39 -07:00
|
|
|
// Add headers to props validation
|
|
|
|
|
|
|
|
|
|
const [page, setPage] = useState(0);
|
2024-08-19 17:34:10 -04:00
|
|
|
const [rowsPerPage, setRowsPerPage] = useState(rows);
|
2024-07-12 14:30:39 -07:00
|
|
|
|
2024-08-07 22:46:21 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
setPage(0);
|
|
|
|
|
}, [data]);
|
|
|
|
|
|
2024-07-12 14:30:39 -07:00
|
|
|
const handleChangePage = (event, newPage) => {
|
|
|
|
|
setPage(newPage);
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-19 17:34:10 -04:00
|
|
|
const handleChangeRowsPerPage = (event) => {
|
|
|
|
|
setRowsPerPage(parseInt(event.target.value, 10));
|
|
|
|
|
setPage(0);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-16 14:39:19 -07:00
|
|
|
let displayData = [];
|
|
|
|
|
|
|
|
|
|
if (data && data.rows) {
|
2024-07-21 09:22:00 -07:00
|
|
|
let rows = reversed ? [...data.rows].reverse() : data.rows;
|
2024-07-16 14:39:19 -07:00
|
|
|
displayData = paginated
|
2024-07-21 09:22:00 -07:00
|
|
|
? rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
|
|
|
|
|
: rows;
|
2024-07-16 14:39:19 -07:00
|
|
|
}
|
2024-07-12 14:30:39 -07:00
|
|
|
|
2024-07-16 14:05:38 -07:00
|
|
|
if (!data || !data.cols || !data.rows) {
|
|
|
|
|
return <div>No data</div>;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 14:30:39 -07:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<TableContainer component={Paper}>
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
2024-07-16 13:21:45 -07:00
|
|
|
{data.cols.map((col) => (
|
|
|
|
|
<TableCell key={col.id}>{col.name}</TableCell>
|
|
|
|
|
))}
|
2024-07-12 14:30:39 -07:00
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
2024-07-16 13:27:58 -07:00
|
|
|
{displayData.map((row) => {
|
|
|
|
|
return (
|
|
|
|
|
<TableRow
|
2024-08-02 10:53:24 -07:00
|
|
|
sx={{
|
|
|
|
|
cursor: row.handleClick ? "pointer" : "default",
|
|
|
|
|
}}
|
2024-07-16 13:27:58 -07:00
|
|
|
key={row.id}
|
|
|
|
|
onClick={row.handleClick ? row.handleClick : null}
|
|
|
|
|
>
|
|
|
|
|
{row.data.map((cell) => {
|
|
|
|
|
return <TableCell key={cell.id}>{cell.data}</TableCell>;
|
|
|
|
|
})}
|
|
|
|
|
</TableRow>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-07-12 14:30:39 -07:00
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</TableContainer>
|
2024-08-19 17:34:10 -04:00
|
|
|
<TablePagination
|
|
|
|
|
component="div"
|
|
|
|
|
count={data.rows.length}
|
|
|
|
|
page={page}
|
|
|
|
|
onPageChange={handleChangePage}
|
|
|
|
|
rowsPerPage={rowsPerPage}
|
|
|
|
|
rowsPerPageOptions={[5, 10, 15, 25]}
|
|
|
|
|
onRowsPerPageChange={handleChangeRowsPerPage}
|
|
|
|
|
ActionsComponent={TablePaginationActions}
|
2024-08-19 18:49:14 -04:00
|
|
|
labelRowsPerPage="Rows per page"
|
2024-08-19 17:58:48 -04:00
|
|
|
labelDisplayedRows={({ page, count }) =>
|
|
|
|
|
`Page ${page + 1} of ${Math.max(0, Math.ceil(count / rowsPerPage))}`
|
|
|
|
|
}
|
2024-08-19 18:49:14 -04:00
|
|
|
slotProps={{
|
|
|
|
|
select: {
|
|
|
|
|
MenuProps: {
|
|
|
|
|
PaperProps: {
|
|
|
|
|
className: "pagination-dropdown",
|
|
|
|
|
},
|
|
|
|
|
transformOrigin: { vertical: "bottom", horizontal: "left" },
|
|
|
|
|
anchorOrigin: { vertical: "top", horizontal: "left" },
|
|
|
|
|
sx: { mt: "-4px" },
|
|
|
|
|
},
|
|
|
|
|
inputProps: { id: "pagination-dropdown" },
|
|
|
|
|
IconComponent: SelectorVertical,
|
|
|
|
|
sx: {
|
|
|
|
|
ml: "8px",
|
|
|
|
|
mr: "25px",
|
|
|
|
|
minWidth: "50px",
|
|
|
|
|
textAlign: "left",
|
|
|
|
|
"&.Mui-focused > div": { backgroundColor: "white" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}}
|
2024-08-19 17:34:10 -04:00
|
|
|
/>
|
2024-07-12 14:30:39 -07:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BasicTable.propTypes = {
|
2024-07-16 13:21:45 -07:00
|
|
|
data: PropTypes.object.isRequired,
|
2024-07-12 14:30:39 -07:00
|
|
|
paginated: PropTypes.bool,
|
2024-07-21 09:22:00 -07:00
|
|
|
reversed: PropTypes.bool,
|
2024-07-23 17:39:26 -04:00
|
|
|
rowPerPage: PropTypes.number,
|
2024-07-12 14:30:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BasicTable;
|