Skip to content

Commit

Permalink
[Table] Add dense support
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik authored and oliviertassinari committed Mar 1, 2019
1 parent 6e43049 commit b12ac64
Show file tree
Hide file tree
Showing 23 changed files with 500 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ function CustomPaginationActionsTable() {
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell numeric>{row.calories}</TableCell>
<TableCell numeric>{row.fat}</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
</TableRow>
))}
{emptyRows > 0 && (
Expand Down
79 changes: 79 additions & 0 deletions docs/src/pages/demos/tables/DenseTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';

const styles = theme => ({
root: {
width: '100%',
},
paper: {
marginTop: theme.spacing(3),
width: '100%',
overflowX: 'auto',
marginBottom: theme.spacing(2),
},
table: {
minWidth: 650,
},
});

let id = 0;
function createData(name, calories, fat, carbs, protein) {
id += 1;
return { id, name, calories, fat, carbs, protein };
}

const rows = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];

function DenseTable(props) {
const { classes } = props;

return (
<div className={classes.root}>
<Paper className={classes.paper}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<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 => (
<TableRow key={row.id}>
<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>
))}
</TableBody>
</Table>
</Paper>
</div>
);
}

DenseTable.propTypes = {
classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(DenseTable);
152 changes: 87 additions & 65 deletions docs/src/pages/demos/tables/EnhancedTable.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Paper from '@material-ui/core/Paper';
import Checkbox from '@material-ui/core/Checkbox';
import IconButton from '@material-ui/core/IconButton';
import Tooltip from '@material-ui/core/Tooltip';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Switch from '@material-ui/core/Switch';
import DeleteIcon from '@material-ui/icons/Delete';
import FilterListIcon from '@material-ui/icons/FilterList';
import { lighten } from '@material-ui/core/styles/colorManipulator';
Expand Down Expand Up @@ -77,7 +79,7 @@ function EnhancedTableHead(props) {
row => (
<TableCell
key={row.id}
numeric={row.numeric}
align={row.numeric ? 'right' : 'left'}
padding={row.disablePadding ? 'none' : 'default'}
sortDirection={orderBy === row.id ? order : false}
>
Expand Down Expand Up @@ -108,6 +110,7 @@ EnhancedTableHead.propTypes = {

const useToolbarStyles = makeStyles(theme => ({
root: {
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(1),
},
highlight:
Expand Down Expand Up @@ -181,8 +184,12 @@ const useStyles = makeStyles(theme => ({
width: '100%',
marginTop: theme.spacing(3),
},
paper: {
width: '100%',
marginBottom: theme.spacing(2),
},
table: {
minWidth: 1020,
minWidth: 750,
},
tableWrapper: {
overflowX: 'auto',
Expand Down Expand Up @@ -210,6 +217,7 @@ function EnhancedTable() {
createData('Oreo', 437, 18.0, 63, 4.0),
]);
const [page, setPage] = React.useState(0);
const [dense, setDense] = React.useState(false);
const [rowsPerPage, setRowsPerPage] = React.useState(5);

function handleRequestSort(event, property) {
Expand Down Expand Up @@ -255,75 +263,89 @@ function EnhancedTable() {
setRowsPerPage(event.target.value);
}

function handleChangeDense(event) {
setDense(event.target.checked);
}

const isSelected = id => selected.indexOf(id) !== -1;

const emptyRows = rowsPerPage - Math.min(rowsPerPage, data.length - page * rowsPerPage);

return (
<Paper className={classes.root}>
<EnhancedTableToolbar numSelected={selected.length} />
<div className={classes.tableWrapper}>
<Table className={classes.table} aria-labelledby="tableTitle">
<EnhancedTableHead
numSelected={selected.length}
order={order}
orderBy={orderBy}
onSelectAllClick={handleSelectAllClick}
onRequestSort={handleRequestSort}
rowCount={data.length}
/>
<TableBody>
{stableSort(data, getSorting(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map(n => {
const isItemSelected = isSelected(n.id);
return (
<TableRow
hover
onClick={event => handleClick(event, n.id)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={n.id}
selected={isItemSelected}
>
<TableCell padding="checkbox">
<Checkbox checked={isItemSelected} />
</TableCell>
<TableCell component="th" scope="row" padding="none">
{n.name}
</TableCell>
<TableCell numeric>{n.calories}</TableCell>
<TableCell numeric>{n.fat}</TableCell>
<TableCell numeric>{n.carbs}</TableCell>
<TableCell numeric>{n.protein}</TableCell>
</TableRow>
);
})}
{emptyRows > 0 && (
<TableRow style={{ height: 49 * emptyRows }}>
<TableCell colSpan={6} />
</TableRow>
)}
</TableBody>
</Table>
</div>
<TablePagination
rowsPerPageOptions={[5, 10, 25]}
component="div"
count={data.length}
rowsPerPage={rowsPerPage}
page={page}
backIconButtonProps={{
'aria-label': 'Previous Page',
}}
nextIconButtonProps={{
'aria-label': 'Next Page',
}}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
<div className={classes.root}>
<Paper className={classes.paper}>
<EnhancedTableToolbar numSelected={selected.length} />
<div className={classes.tableWrapper}>
<Table
className={classes.table}
aria-labelledby="tableTitle"
size={dense ? 'small' : 'medium'}
>
<EnhancedTableHead
numSelected={selected.length}
order={order}
orderBy={orderBy}
onSelectAllClick={handleSelectAllClick}
onRequestSort={handleRequestSort}
rowCount={data.length}
/>
<TableBody>
{stableSort(data, getSorting(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map(n => {
const isItemSelected = isSelected(n.id);
return (
<TableRow
hover
onClick={event => handleClick(event, n.id)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={n.id}
selected={isItemSelected}
>
<TableCell padding="checkbox">
<Checkbox checked={isItemSelected} />
</TableCell>
<TableCell component="th" scope="row" padding="none">
{n.name}
</TableCell>
<TableCell align="right">{n.calories}</TableCell>
<TableCell align="right">{n.fat}</TableCell>
<TableCell align="right">{n.carbs}</TableCell>
<TableCell align="right">{n.protein}</TableCell>
</TableRow>
);
})}
{emptyRows > 0 && (
<TableRow style={{ height: 49 * emptyRows }}>
<TableCell colSpan={6} />
</TableRow>
)}
</TableBody>
</Table>
</div>
<TablePagination
rowsPerPageOptions={[5, 10, 25]}
component="div"
count={data.length}
rowsPerPage={rowsPerPage}
page={page}
backIconButtonProps={{
'aria-label': 'Previous Page',
}}
nextIconButtonProps={{
'aria-label': 'Next Page',
}}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
/>
</Paper>
<FormControlLabel
control={<Switch checked={dense} onChange={handleChangeDense} />}
label="Dense padding"
/>
</Paper>
</div>
);
}

Expand Down
Loading

0 comments on commit b12ac64

Please sign in to comment.