-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lorenz Henk
committed
Feb 21, 2020
1 parent
264684b
commit 782ae8e
Showing
3 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import Box from '@material-ui/core/Box'; | ||
import Collapse from '@material-ui/core/Collapse'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import Table from '@material-ui/core/Table'; | ||
import TableBody from '@material-ui/core/TableBody'; | ||
import TableCell from '@material-ui/core/TableCell'; | ||
import TableContainer from '@material-ui/core/TableContainer'; | ||
import TableHead from '@material-ui/core/TableHead'; | ||
import TableRow from '@material-ui/core/TableRow'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Paper from '@material-ui/core/Paper'; | ||
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown'; | ||
import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp'; | ||
|
||
const useRowStyles = makeStyles({ | ||
root: { | ||
'& > *': { | ||
borderBottom: 'unset', | ||
}, | ||
}, | ||
}); | ||
|
||
const history = [ | ||
{ date: '2020-01-05', customerId: '11091700', amount: 3 }, | ||
{ date: '2020-01-02', amount: 1 }, | ||
]; | ||
|
||
function createData(name, calories, fat, carbs, protein, price) { | ||
return { | ||
name, | ||
calories, | ||
fat, | ||
carbs, | ||
protein, | ||
price, | ||
history, | ||
}; | ||
} | ||
|
||
function Row({ row }) { | ||
const [open, setOpen] = useState(false); | ||
const { root } = useRowStyles(); | ||
|
||
return ( | ||
<> | ||
<TableRow className={root}> | ||
<TableCell> | ||
<IconButton size="small" onClick={() => setOpen(!open)}> | ||
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />} | ||
</IconButton> | ||
</TableCell> | ||
<TableCell component="th" scope="row"> | ||
{row.name} | ||
</TableCell> | ||
<TableCell align="right">{row.calories}</TableCell> | ||
<TableCell align="right">{row.fat}</TableCell> | ||
<TableCell align="right">{row.carbs}</TableCell> | ||
<TableCell align="right">{row.protein}</TableCell> | ||
</TableRow> | ||
<TableRow> | ||
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}> | ||
<Collapse in={open} timeout="auto" unmountOnExit> | ||
<Box margin={1}> | ||
<Typography variant="h6" gutterBottom> | ||
History | ||
</Typography> | ||
<Table size="small" aria-label="purchases"> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell>Date</TableCell> | ||
<TableCell>Customer</TableCell> | ||
<TableCell align="right">Amount</TableCell> | ||
<TableCell align="right">Total price ($)</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{row.history.map(historyRow => ( | ||
<TableRow key={historyRow.date}> | ||
<TableCell component="th" scope="row"> | ||
{historyRow.date} | ||
</TableCell> | ||
<TableCell>{historyRow.customerId || 'Anonymous'}</TableCell> | ||
<TableCell align="right">{historyRow.amount}</TableCell> | ||
<TableCell align="right"> | ||
{Math.round(historyRow.amount * row.price * 100) / 100} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</Box> | ||
</Collapse> | ||
</TableCell> | ||
</TableRow> | ||
</> | ||
); | ||
} | ||
|
||
Row.propTypes = { | ||
row: PropTypes.shape({ | ||
calories: PropTypes.number.isRequired, | ||
carbs: PropTypes.number.isRequired, | ||
fat: PropTypes.number.isRequired, | ||
history: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
amount: PropTypes.number.isRequired, | ||
customerId: PropTypes.string.isRequired, | ||
date: PropTypes.string.isRequired, | ||
}), | ||
).isRequired, | ||
name: PropTypes.string.isRequired, | ||
price: PropTypes.number.isRequired, | ||
protein: PropTypes.number.isRequired, | ||
}).isRequired, | ||
}; | ||
|
||
const rows = [ | ||
createData('Frozen yoghurt', 159, 6.0, 24, 4.0, 3.99), | ||
createData('Ice cream sandwich', 237, 9.0, 37, 4.3, 4.99), | ||
createData('Eclair', 262, 16.0, 24, 6.0, 3.79), | ||
createData('Cupcake', 305, 3.7, 67, 4.3, 2.5), | ||
createData('Gingerbread', 356, 16.0, 49, 3.9, 1.5), | ||
]; | ||
|
||
export default function SimpleTable() { | ||
return ( | ||
<TableContainer component={Paper}> | ||
<Table aria-label="simple table"> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell /> | ||
<TableCell>Dessert (100g serving)</TableCell> | ||
<TableCell align="right">Calories</TableCell> | ||
<TableCell align="right">Fat (g)</TableCell> | ||
<TableCell align="right">Carbs (g)</TableCell> | ||
<TableCell align="right">Protein (g)</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.map(row => ( | ||
<Row key={row.name} row={row} /> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import React, { useState } from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import Box from '@material-ui/core/Box'; | ||
import Collapse from '@material-ui/core/Collapse'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import Table from '@material-ui/core/Table'; | ||
import TableBody from '@material-ui/core/TableBody'; | ||
import TableCell from '@material-ui/core/TableCell'; | ||
import TableContainer from '@material-ui/core/TableContainer'; | ||
import TableHead from '@material-ui/core/TableHead'; | ||
import TableRow from '@material-ui/core/TableRow'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Paper from '@material-ui/core/Paper'; | ||
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown'; | ||
import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp'; | ||
|
||
const useRowStyles = makeStyles({ | ||
root: { | ||
'& > *': { | ||
borderBottom: 'unset', | ||
}, | ||
}, | ||
}); | ||
|
||
const history = [ | ||
{ date: '2020-01-05', customerId: '11091700', amount: 3 }, | ||
{ date: '2020-01-02', amount: 1 }, | ||
]; | ||
|
||
function createData( | ||
name: string, | ||
calories: number, | ||
fat: number, | ||
carbs: number, | ||
protein: number, | ||
price: number, | ||
) { | ||
return { | ||
name, | ||
calories, | ||
fat, | ||
carbs, | ||
protein, | ||
price, | ||
history, | ||
}; | ||
} | ||
|
||
function Row({ row }: { row: ReturnType<typeof createData> }) { | ||
const [open, setOpen] = useState(false); | ||
const { root } = useRowStyles(); | ||
|
||
return ( | ||
<> | ||
<TableRow className={root}> | ||
<TableCell> | ||
<IconButton size="small" onClick={() => setOpen(!open)}> | ||
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />} | ||
</IconButton> | ||
</TableCell> | ||
<TableCell component="th" scope="row"> | ||
{row.name} | ||
</TableCell> | ||
<TableCell align="right">{row.calories}</TableCell> | ||
<TableCell align="right">{row.fat}</TableCell> | ||
<TableCell align="right">{row.carbs}</TableCell> | ||
<TableCell align="right">{row.protein}</TableCell> | ||
</TableRow> | ||
<TableRow> | ||
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}> | ||
<Collapse in={open} timeout="auto" unmountOnExit> | ||
<Box margin={1}> | ||
<Typography variant="h6" gutterBottom> | ||
History | ||
</Typography> | ||
<Table size="small" aria-label="purchases"> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell>Date</TableCell> | ||
<TableCell>Customer</TableCell> | ||
<TableCell align="right">Amount</TableCell> | ||
<TableCell align="right">Total price ($)</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{row.history.map(historyRow => ( | ||
<TableRow key={historyRow.date}> | ||
<TableCell component="th" scope="row"> | ||
{historyRow.date} | ||
</TableCell> | ||
<TableCell>{historyRow.customerId || 'Anonymous'}</TableCell> | ||
<TableCell align="right">{historyRow.amount}</TableCell> | ||
<TableCell align="right"> | ||
{Math.round(historyRow.amount * row.price * 100) / 100} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</Box> | ||
</Collapse> | ||
</TableCell> | ||
</TableRow> | ||
</> | ||
); | ||
} | ||
|
||
const rows = [ | ||
createData('Frozen yoghurt', 159, 6.0, 24, 4.0, 3.99), | ||
createData('Ice cream sandwich', 237, 9.0, 37, 4.3, 4.99), | ||
createData('Eclair', 262, 16.0, 24, 6.0, 3.79), | ||
createData('Cupcake', 305, 3.7, 67, 4.3, 2.5), | ||
createData('Gingerbread', 356, 16.0, 49, 3.9, 1.5), | ||
]; | ||
|
||
export default function SimpleTable() { | ||
return ( | ||
<TableContainer component={Paper}> | ||
<Table aria-label="simple table"> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell /> | ||
<TableCell>Dessert (100g serving)</TableCell> | ||
<TableCell align="right">Calories</TableCell> | ||
<TableCell align="right">Fat (g)</TableCell> | ||
<TableCell align="right">Carbs (g)</TableCell> | ||
<TableCell align="right">Protein (g)</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.map(row => ( | ||
<Row key={row.name} row={row} /> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters