Adding Pagination to Table component

This commit is contained in:
apanizo 2018-10-18 13:49:12 +02:00
parent 1e59ef9bbd
commit 53b169bc42
2 changed files with 86 additions and 28 deletions

View File

@ -6,11 +6,11 @@ import TableBody from '@material-ui/core/TableBody'
import TableRow from '@material-ui/core/TableRow' import TableRow from '@material-ui/core/TableRow'
import TableCell from '@material-ui/core/TableCell' import TableCell from '@material-ui/core/TableCell'
import TableHead, { type Column } from '~/components/Table/TableHead' import TableHead, { type Column } from '~/components/Table/TableHead'
import TablePagination from '@material-ui/core/TablePagination'
import { type Order, stableSort, getSorting } from '~/components/Table/sorting' import { type Order, stableSort, getSorting } from '~/components/Table/sorting'
type Props<K> = { type Props<K> = {
label: string, label: string,
rowsPerPage?: number,
defaultOrderBy: string, defaultOrderBy: string,
columns: List<Column>, columns: List<Column>,
data: Array<K>, data: Array<K>,
@ -21,6 +21,7 @@ type State = {
order: Order, order: Order,
orderBy: string, orderBy: string,
orderProp: boolean, orderProp: boolean,
rowsPerPage: number,
} }
class GnoTable<K> extends React.Component<Props<K>, State> { class GnoTable<K> extends React.Component<Props<K>, State> {
@ -29,6 +30,7 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
order: 'desc', order: 'desc',
orderBy: this.props.defaultOrderBy, orderBy: this.props.defaultOrderBy,
orderProp: false, orderProp: false,
rowsPerPage: 5,
} }
onSort = (property: string, orderProp: boolean) => { onSort = (property: string, orderProp: boolean) => {
@ -42,15 +44,34 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
this.setState({ order, orderBy, orderProp }) this.setState({ order, orderBy, orderProp })
} }
handleChangePage = (page: number) => {
this.setState({ page })
}
handleChangeRowsPerPage = (event: SyntheticInputEvent<HTMLInputElement>) => {
const rowsPerPage = Number(event.target.value)
this.setState({ rowsPerPage })
}
render() { render() {
const { const {
data, label, columns, rowsPerPage = 5, data, label, columns,
} = this.props } = this.props
const { const {
order, orderBy, page, orderProp, order, orderBy, page, orderProp, rowsPerPage,
} = this.state } = this.state
const backProps = {
'aria-label': 'Previous Page',
}
const nextProps = {
'aria-label': 'Next Page',
}
return ( return (
<React.Fragment>
<Table aria-labelledby={label}> <Table aria-labelledby={label}>
<TableHead <TableHead
columns={columns} columns={columns}
@ -77,6 +98,17 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
))} ))}
</TableBody> </TableBody>
</Table> </Table>
<TablePagination
component="div"
count={data.length}
rowsPerPage={rowsPerPage}
page={page}
backIconButtonProps={backProps}
nextIconButtonProps={nextProps}
onChangePage={this.handleChangePage}
onChangeRowsPerPage={this.handleChangeRowsPerPage}
/>
</React.Fragment>
) )
} }
} }

View File

@ -131,6 +131,32 @@ export default createMuiTheme({
fontWeight: bolderFont, fontWeight: bolderFont,
}, },
}, },
MuiTablePagination: {
toolbar: {
'& > span:nth-child(2)': {
order: 1,
},
},
selectIcon: {
height: '100%',
top: '0px',
},
caption: {
fontFamily: 'Roboto Mono, monospace',
letterSpacing: '-0.5px',
order: 3,
color: disabled,
},
input: {
order: 2,
width: '60px',
padding: `0 ${md} 0 0`,
},
actions: {
order: 4,
color: disabled,
},
},
}, },
palette, palette,
}) })