|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { makeStyles } from '@material-ui/core/styles'; |
| 4 | +import Box from '@material-ui/core/Box'; |
| 5 | +import Collapse from '@material-ui/core/Collapse'; |
| 6 | +import IconButton from '@material-ui/core/IconButton'; |
| 7 | +import Table from '@material-ui/core/Table'; |
| 8 | +import TableBody from '@material-ui/core/TableBody'; |
| 9 | +import TableCell from '@material-ui/core/TableCell'; |
| 10 | +import TableContainer from '@material-ui/core/TableContainer'; |
| 11 | +import TableHead from '@material-ui/core/TableHead'; |
| 12 | +import TableRow from '@material-ui/core/TableRow'; |
| 13 | +import Typography from '@material-ui/core/Typography'; |
| 14 | +import Paper from '@material-ui/core/Paper'; |
| 15 | +import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown'; |
| 16 | +import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp'; |
| 17 | + |
| 18 | +const useRowStyles = makeStyles({ |
| 19 | + root: { |
| 20 | + '& > *': { |
| 21 | + borderBottom: 'unset', |
| 22 | + }, |
| 23 | + }, |
| 24 | +}); |
| 25 | + |
| 26 | +function createData(name, calories, fat, carbs, protein, price) { |
| 27 | + return { |
| 28 | + name, |
| 29 | + calories, |
| 30 | + fat, |
| 31 | + carbs, |
| 32 | + protein, |
| 33 | + price, |
| 34 | + history: [ |
| 35 | + { date: '2020-01-05', customerId: '11091700', amount: 3 }, |
| 36 | + { date: '2020-01-02', customerId: 'Anonymous', amount: 1 }, |
| 37 | + ], |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function Row(props) { |
| 42 | + const { row } = props; |
| 43 | + const [open, setOpen] = React.useState(false); |
| 44 | + const classes = useRowStyles(); |
| 45 | + |
| 46 | + return ( |
| 47 | + <React.Fragment> |
| 48 | + <TableRow className={classes.root}> |
| 49 | + <TableCell> |
| 50 | + <IconButton aria-label="expand row" size="small" onClick={() => setOpen(!open)}> |
| 51 | + {open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />} |
| 52 | + </IconButton> |
| 53 | + </TableCell> |
| 54 | + <TableCell component="th" scope="row"> |
| 55 | + {row.name} |
| 56 | + </TableCell> |
| 57 | + <TableCell align="right">{row.calories}</TableCell> |
| 58 | + <TableCell align="right">{row.fat}</TableCell> |
| 59 | + <TableCell align="right">{row.carbs}</TableCell> |
| 60 | + <TableCell align="right">{row.protein}</TableCell> |
| 61 | + </TableRow> |
| 62 | + <TableRow> |
| 63 | + <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}> |
| 64 | + <Collapse in={open} timeout="auto" unmountOnExit> |
| 65 | + <Box margin={1}> |
| 66 | + <Typography variant="h6" gutterBottom component="div"> |
| 67 | + History |
| 68 | + </Typography> |
| 69 | + <Table size="small" aria-label="purchases"> |
| 70 | + <TableHead> |
| 71 | + <TableRow> |
| 72 | + <TableCell>Date</TableCell> |
| 73 | + <TableCell>Customer</TableCell> |
| 74 | + <TableCell align="right">Amount</TableCell> |
| 75 | + <TableCell align="right">Total price ($)</TableCell> |
| 76 | + </TableRow> |
| 77 | + </TableHead> |
| 78 | + <TableBody> |
| 79 | + {row.history.map((historyRow) => ( |
| 80 | + <TableRow key={historyRow.date}> |
| 81 | + <TableCell component="th" scope="row"> |
| 82 | + {historyRow.date} |
| 83 | + </TableCell> |
| 84 | + <TableCell>{historyRow.customerId}</TableCell> |
| 85 | + <TableCell align="right">{historyRow.amount}</TableCell> |
| 86 | + <TableCell align="right"> |
| 87 | + {Math.round(historyRow.amount * row.price * 100) / 100} |
| 88 | + </TableCell> |
| 89 | + </TableRow> |
| 90 | + ))} |
| 91 | + </TableBody> |
| 92 | + </Table> |
| 93 | + </Box> |
| 94 | + </Collapse> |
| 95 | + </TableCell> |
| 96 | + </TableRow> |
| 97 | + </React.Fragment> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +Row.propTypes = { |
| 102 | + row: PropTypes.shape({ |
| 103 | + calories: PropTypes.number.isRequired, |
| 104 | + carbs: PropTypes.number.isRequired, |
| 105 | + fat: PropTypes.number.isRequired, |
| 106 | + history: PropTypes.arrayOf( |
| 107 | + PropTypes.shape({ |
| 108 | + amount: PropTypes.number.isRequired, |
| 109 | + customerId: PropTypes.string.isRequired, |
| 110 | + date: PropTypes.string.isRequired, |
| 111 | + }), |
| 112 | + ).isRequired, |
| 113 | + name: PropTypes.string.isRequired, |
| 114 | + price: PropTypes.number.isRequired, |
| 115 | + protein: PropTypes.number.isRequired, |
| 116 | + }).isRequired, |
| 117 | +}; |
| 118 | + |
| 119 | +const rows = [ |
| 120 | + createData('Frozen yoghurt', 159, 6.0, 24, 4.0, 3.99), |
| 121 | + createData('Ice cream sandwich', 237, 9.0, 37, 4.3, 4.99), |
| 122 | + createData('Eclair', 262, 16.0, 24, 6.0, 3.79), |
| 123 | + createData('Cupcake', 305, 3.7, 67, 4.3, 2.5), |
| 124 | + createData('Gingerbread', 356, 16.0, 49, 3.9, 1.5), |
| 125 | +]; |
| 126 | + |
| 127 | +export default function CollapsibleTable() { |
| 128 | + return ( |
| 129 | + <TableContainer component={Paper}> |
| 130 | + <Table aria-label="collapsible table"> |
| 131 | + <TableHead> |
| 132 | + <TableRow> |
| 133 | + <TableCell /> |
| 134 | + <TableCell>Dessert (100g serving)</TableCell> |
| 135 | + <TableCell align="right">Calories</TableCell> |
| 136 | + <TableCell align="right">Fat (g)</TableCell> |
| 137 | + <TableCell align="right">Carbs (g)</TableCell> |
| 138 | + <TableCell align="right">Protein (g)</TableCell> |
| 139 | + </TableRow> |
| 140 | + </TableHead> |
| 141 | + <TableBody> |
| 142 | + {rows.map((row) => ( |
| 143 | + <Row key={row.name} row={row} /> |
| 144 | + ))} |
| 145 | + </TableBody> |
| 146 | + </Table> |
| 147 | + </TableContainer> |
| 148 | + ); |
| 149 | +} |
0 commit comments