-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Table] Add sticky header support (#17139)
- Loading branch information
Showing
10 changed files
with
337 additions
and
10 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
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
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,136 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import Paper from '@material-ui/core/Paper'; | ||
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 TablePagination from '@material-ui/core/TablePagination'; | ||
import TableRow from '@material-ui/core/TableRow'; | ||
|
||
const columns = [ | ||
{ id: 'name', label: 'Name', minWidth: 200 }, | ||
{ id: 'code', label: 'ISO\u00a0Code', minWidth: 100 }, | ||
{ | ||
id: 'population', | ||
label: 'Population', | ||
minWidth: 120, | ||
align: 'right', | ||
format: value => value.toLocaleString(), | ||
}, | ||
{ | ||
id: 'size', | ||
label: 'Size\u00a0(km\u00b2)', | ||
minWidth: 120, | ||
align: 'right', | ||
format: value => value.toLocaleString(), | ||
}, | ||
{ | ||
id: 'density', | ||
label: 'Density', | ||
minWidth: 120, | ||
align: 'right', | ||
format: value => value.toFixed(2), | ||
}, | ||
]; | ||
|
||
function createData(name, code, population, size) { | ||
const density = population / size; | ||
return { name, code, population, size, density }; | ||
} | ||
|
||
const rows = [ | ||
createData('India', 'IN', 1324171354, 3287263), | ||
createData('China', 'CN', 1403500365, 9596961), | ||
createData('Italy', 'IT', 60483973, 301340), | ||
createData('United States', 'US', 327167434, 9833520), | ||
createData('Canada', 'CA', 37602103, 9984670), | ||
createData('Australia', 'AU', 25475400, 7692024), | ||
createData('Germany', 'DE', 83019200, 357578), | ||
createData('Ireland', 'IE', 4857000, 70273), | ||
createData('Mexico', 'MX', 126577691, 1972550), | ||
createData('Japan', 'JP', 126317000, 377973), | ||
createData('France', 'FR', 67022000, 640679), | ||
createData('United Kingdom', 'GB', 67545757, 242495), | ||
createData('Russia', 'RU', 146793744, 17098246), | ||
createData('Nigeria', 'NG', 200962417, 923768), | ||
createData('Brazil', 'BR', 210147125, 8515767), | ||
]; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
width: '100%', | ||
}, | ||
tableWrapper: { | ||
maxHeight: 407, | ||
overflow: 'auto', | ||
}, | ||
}); | ||
|
||
export default function StickyHeadTable() { | ||
const classes = useStyles(); | ||
const [page, setPage] = React.useState(0); | ||
const [rowsPerPage, setRowsPerPage] = React.useState(10); | ||
|
||
function handleChangePage(event, newPage) { | ||
setPage(newPage); | ||
} | ||
|
||
function handleChangeRowsPerPage(event) { | ||
setRowsPerPage(+event.target.value); | ||
setPage(0); | ||
} | ||
|
||
return ( | ||
<Paper className={classes.root}> | ||
<div className={classes.tableWrapper}> | ||
<Table stickyHeader> | ||
<TableHead> | ||
<TableRow> | ||
{columns.map(column => ( | ||
<TableCell | ||
key={column.id} | ||
align={column.align} | ||
style={{ minWidth: column.minWidth }} | ||
> | ||
{column.label} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map(row => { | ||
return ( | ||
<TableRow hover role="checkbox" tabIndex={-1} key={row.code}> | ||
{columns.map(column => { | ||
const value = row[column.id]; | ||
return ( | ||
<TableCell key={column.id} align={column.align}> | ||
{column.format && typeof value === 'number' ? column.format(value) : value} | ||
</TableCell> | ||
); | ||
})} | ||
</TableRow> | ||
); | ||
})} | ||
</TableBody> | ||
</Table> | ||
</div> | ||
<TablePagination | ||
rowsPerPageOptions={[10, 25, 100]} | ||
component="div" | ||
count={rows.length} | ||
rowsPerPage={rowsPerPage} | ||
page={page} | ||
backIconButtonProps={{ | ||
'aria-label': 'previous page', | ||
}} | ||
nextIconButtonProps={{ | ||
'aria-label': 'next page', | ||
}} | ||
onChangePage={handleChangePage} | ||
onChangeRowsPerPage={handleChangeRowsPerPage} | ||
/> | ||
</Paper> | ||
); | ||
} |
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,152 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import Paper from '@material-ui/core/Paper'; | ||
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 TablePagination from '@material-ui/core/TablePagination'; | ||
import TableRow from '@material-ui/core/TableRow'; | ||
|
||
interface Column { | ||
id: 'name' | 'code' | 'population' | 'size' | 'density'; | ||
label: string; | ||
minWidth?: number; | ||
align?: 'right'; | ||
format?: (value: number) => string; | ||
} | ||
|
||
const columns: Column[] = [ | ||
{ id: 'name', label: 'Name', minWidth: 200 }, | ||
{ id: 'code', label: 'ISO\u00a0Code', minWidth: 100 }, | ||
{ | ||
id: 'population', | ||
label: 'Population', | ||
minWidth: 120, | ||
align: 'right', | ||
format: (value: number) => value.toLocaleString(), | ||
}, | ||
{ | ||
id: 'size', | ||
label: 'Size\u00a0(km\u00b2)', | ||
minWidth: 120, | ||
align: 'right', | ||
format: (value: number) => value.toLocaleString(), | ||
}, | ||
{ | ||
id: 'density', | ||
label: 'Density', | ||
minWidth: 120, | ||
align: 'right', | ||
format: (value: number) => value.toFixed(2), | ||
}, | ||
]; | ||
|
||
interface Data { | ||
name: string; | ||
code: string; | ||
population: number; | ||
size: number; | ||
density: number; | ||
} | ||
|
||
function createData(name: string, code: string, population: number, size: number): Data { | ||
const density = population / size; | ||
return { name, code, population, size, density }; | ||
} | ||
|
||
const rows = [ | ||
createData('India', 'IN', 1324171354, 3287263), | ||
createData('China', 'CN', 1403500365, 9596961), | ||
createData('Italy', 'IT', 60483973, 301340), | ||
createData('United States', 'US', 327167434, 9833520), | ||
createData('Canada', 'CA', 37602103, 9984670), | ||
createData('Australia', 'AU', 25475400, 7692024), | ||
createData('Germany', 'DE', 83019200, 357578), | ||
createData('Ireland', 'IE', 4857000, 70273), | ||
createData('Mexico', 'MX', 126577691, 1972550), | ||
createData('Japan', 'JP', 126317000, 377973), | ||
createData('France', 'FR', 67022000, 640679), | ||
createData('United Kingdom', 'GB', 67545757, 242495), | ||
createData('Russia', 'RU', 146793744, 17098246), | ||
createData('Nigeria', 'NG', 200962417, 923768), | ||
createData('Brazil', 'BR', 210147125, 8515767), | ||
]; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
width: '100%', | ||
}, | ||
tableWrapper: { | ||
maxHeight: 407, | ||
overflow: 'auto', | ||
}, | ||
}); | ||
|
||
export default function StickyHeadTable() { | ||
const classes = useStyles(); | ||
const [page, setPage] = React.useState(0); | ||
const [rowsPerPage, setRowsPerPage] = React.useState(10); | ||
|
||
function handleChangePage(event: unknown, newPage: number) { | ||
setPage(newPage); | ||
} | ||
|
||
function handleChangeRowsPerPage(event: React.ChangeEvent<HTMLInputElement>) { | ||
setRowsPerPage(+event.target.value); | ||
setPage(0); | ||
} | ||
|
||
return ( | ||
<Paper className={classes.root}> | ||
<div className={classes.tableWrapper}> | ||
<Table stickyHeader> | ||
<TableHead> | ||
<TableRow> | ||
{columns.map(column => ( | ||
<TableCell | ||
key={column.id} | ||
align={column.align} | ||
style={{ minWidth: column.minWidth }} | ||
> | ||
{column.label} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map(row => { | ||
return ( | ||
<TableRow hover role="checkbox" tabIndex={-1} key={row.code}> | ||
{columns.map(column => { | ||
const value = row[column.id]; | ||
return ( | ||
<TableCell key={column.id} align={column.align}> | ||
{column.format && typeof value === 'number' ? column.format(value) : value} | ||
</TableCell> | ||
); | ||
})} | ||
</TableRow> | ||
); | ||
})} | ||
</TableBody> | ||
</Table> | ||
</div> | ||
<TablePagination | ||
rowsPerPageOptions={[10, 25, 100]} | ||
component="div" | ||
count={rows.length} | ||
rowsPerPage={rowsPerPage} | ||
page={page} | ||
backIconButtonProps={{ | ||
'aria-label': 'previous page', | ||
}} | ||
nextIconButtonProps={{ | ||
'aria-label': 'next page', | ||
}} | ||
onChangePage={handleChangePage} | ||
onChangeRowsPerPage={handleChangeRowsPerPage} | ||
/> | ||
</Paper> | ||
); | ||
} |
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
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
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
Oops, something went wrong.