|
| 1 | +import React from 'react'; |
| 2 | +import { styled } from '@mui/material/styles'; |
| 3 | +import { |
| 4 | + TableContainer, |
| 5 | + Paper, |
| 6 | + Table, |
| 7 | + TableHead, |
| 8 | + TableRow, |
| 9 | + TableCell, |
| 10 | + TableBody, |
| 11 | + TableFooter, |
| 12 | + TablePagination, |
| 13 | + tableCellClasses, |
| 14 | +} from '@mui/material'; |
| 15 | +import TablePaginationActions from '@mui/material/TablePagination/TablePaginationActions'; |
| 16 | + |
| 17 | +function createData(name: string, calories: number, fat: number, carbs: number, protein: number) { |
| 18 | + return { name, calories, fat, carbs, protein }; |
| 19 | +} |
| 20 | + |
| 21 | +const rows = [ |
| 22 | + createData('Frozen yoghurt', 159, 6.0, 24, 4.0), |
| 23 | + createData('Ice cream sandwich', 237, 9.0, 37, 4.3), |
| 24 | + createData('Eclair', 262, 16.0, 24, 6.0), |
| 25 | + createData('Cupcake', 305, 3.7, 67, 4.3), |
| 26 | + createData('Gingerbread', 356, 16.0, 49, 3.9), |
| 27 | +]; |
| 28 | + |
| 29 | +const CustomTableCell = styled(TableCell)(({ theme }) => ({ |
| 30 | + [`&.${tableCellClasses.head}`]: { |
| 31 | + backgroundColor: theme.palette.secondary[100], |
| 32 | + color: theme.palette.common.black, |
| 33 | + borderBottom: `1px solid rgba(0,0,0, 0.2)`, |
| 34 | + }, |
| 35 | +})); |
| 36 | + |
| 37 | +const ItemsTable = () => { |
| 38 | + const [page, setPage] = React.useState(0); |
| 39 | + const [rowsPerPage, setRowsPerPage] = React.useState(5); |
| 40 | + |
| 41 | + // Avoid a layout jump when reaching the last page with empty rows. |
| 42 | + const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - rows.length) : 0; |
| 43 | + |
| 44 | + const handleChangePage = (event: React.MouseEvent<HTMLButtonElement> | null, newPage: number) => { |
| 45 | + setPage(newPage); |
| 46 | + }; |
| 47 | + |
| 48 | + const handleChangeRowsPerPage = ( |
| 49 | + event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement> |
| 50 | + ) => { |
| 51 | + setRowsPerPage(parseInt(event.target.value, 10)); |
| 52 | + setPage(0); |
| 53 | + }; |
| 54 | + return ( |
| 55 | + <TableContainer component={Paper} elevation={2}> |
| 56 | + <Table sx={{ minWidth: 650 }} aria-label='simple table'> |
| 57 | + <TableHead> |
| 58 | + <TableRow> |
| 59 | + <CustomTableCell>Dessert (100g serving)</CustomTableCell> |
| 60 | + <CustomTableCell align='right'>Calories</CustomTableCell> |
| 61 | + <CustomTableCell align='right'>Fat (g)</CustomTableCell> |
| 62 | + <CustomTableCell align='right'>Carbs (g)</CustomTableCell> |
| 63 | + <CustomTableCell align='right'>Protein (g)</CustomTableCell> |
| 64 | + </TableRow> |
| 65 | + </TableHead> |
| 66 | + <TableBody> |
| 67 | + {rows.map((row) => ( |
| 68 | + <TableRow |
| 69 | + key={row.name} |
| 70 | + sx={{ |
| 71 | + '&:last-child th, &:last-child td': { |
| 72 | + borderBottom: '1px solid rgba(0,0,0,0.2)', |
| 73 | + padding: '1.5rem 2.25rem', |
| 74 | + }, |
| 75 | + 'th, td': { padding: '1.5rem 2.25rem', border: 0 }, |
| 76 | + }} |
| 77 | + > |
| 78 | + <CustomTableCell component='th' scope='row'> |
| 79 | + {row.name} |
| 80 | + </CustomTableCell> |
| 81 | + <CustomTableCell align='right'>{row.calories}</CustomTableCell> |
| 82 | + <CustomTableCell align='right'>{row.fat}</CustomTableCell> |
| 83 | + <CustomTableCell align='right'>{row.carbs}</CustomTableCell> |
| 84 | + <CustomTableCell align='right'>{row.protein}</CustomTableCell> |
| 85 | + </TableRow> |
| 86 | + ))} |
| 87 | + </TableBody> |
| 88 | + </Table> |
| 89 | + </TableContainer> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +export default ItemsTable; |
0 commit comments