From 782ae8eff567e54bfac71e71911743e3dedf0a01 Mon Sep 17 00:00:00 2001 From: Lorenz Henk Date: Fri, 21 Feb 2020 11:36:07 +0100 Subject: [PATCH] Add collapsible table demo --- .../components/tables/CollapsibleTable.js | 150 ++++++++++++++++++ .../components/tables/CollapsibleTable.tsx | 138 ++++++++++++++++ docs/src/pages/components/tables/tables.md | 8 + 3 files changed, 296 insertions(+) create mode 100644 docs/src/pages/components/tables/CollapsibleTable.js create mode 100644 docs/src/pages/components/tables/CollapsibleTable.tsx diff --git a/docs/src/pages/components/tables/CollapsibleTable.js b/docs/src/pages/components/tables/CollapsibleTable.js new file mode 100644 index 00000000000000..eec7ff3b65cf1f --- /dev/null +++ b/docs/src/pages/components/tables/CollapsibleTable.js @@ -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 ( + <> + + + setOpen(!open)}> + {open ? : } + + + + {row.name} + + {row.calories} + {row.fat} + {row.carbs} + {row.protein} + + + + + + + History + + + + + Date + Customer + Amount + Total price ($) + + + + {row.history.map(historyRow => ( + + + {historyRow.date} + + {historyRow.customerId || 'Anonymous'} + {historyRow.amount} + + {Math.round(historyRow.amount * row.price * 100) / 100} + + + ))} + +
+
+
+
+
+ + ); +} + +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 ( + + + + + + Dessert (100g serving) + Calories + Fat (g) + Carbs (g) + Protein (g) + + + + {rows.map(row => ( + + ))} + +
+
+ ); +} diff --git a/docs/src/pages/components/tables/CollapsibleTable.tsx b/docs/src/pages/components/tables/CollapsibleTable.tsx new file mode 100644 index 00000000000000..e8407fdd7a9453 --- /dev/null +++ b/docs/src/pages/components/tables/CollapsibleTable.tsx @@ -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 }) { + const [open, setOpen] = useState(false); + const { root } = useRowStyles(); + + return ( + <> + + + setOpen(!open)}> + {open ? : } + + + + {row.name} + + {row.calories} + {row.fat} + {row.carbs} + {row.protein} + + + + + + + History + + + + + Date + Customer + Amount + Total price ($) + + + + {row.history.map(historyRow => ( + + + {historyRow.date} + + {historyRow.customerId || 'Anonymous'} + {historyRow.amount} + + {Math.round(historyRow.amount * row.price * 100) / 100} + + + ))} + +
+
+
+
+
+ + ); +} + +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 ( + + + + + + Dessert (100g serving) + Calories + Fat (g) + Carbs (g) + Protein (g) + + + + {rows.map(row => ( + + ))} + +
+
+ ); +} diff --git a/docs/src/pages/components/tables/tables.md b/docs/src/pages/components/tables/tables.md index 973184a4ddda2a..a9b4db0f1acb47 100644 --- a/docs/src/pages/components/tables/tables.md +++ b/docs/src/pages/components/tables/tables.md @@ -62,6 +62,7 @@ You should either provide an array of: ```jsx ``` + - **objects**, the `value` and `label` keys will be used respectively for the value and label of the option (useful for language strings such as 'All'). ```jsx @@ -82,6 +83,13 @@ It leverages the `stickyHeader` prop (⚠️ no IE 11 support). {{"demo": "pages/components/tables/StickyHeadTable.js", "bg": true}} +## Collapsible Table + +An example of a table with expandable rows, revealing more information. +It utilizes the [`Collapse`](/api/collapse) component. + +{{"demo": "pages/components/tables/CollapsibleTable.js", "bg": true}} + ## Spanning Table A simple example with spanning rows & columns.