|
| 1 | +import TablePaginationActions from '@material-ui/core/TablePagination/TablePaginationActions'; |
| 2 | +import React from 'react'; |
| 3 | +import TableBody from '@material-ui/core/TableBody'; |
| 4 | +import Table from '@material-ui/core/Table'; |
| 5 | +import TableRow from '@material-ui/core/TableRow'; |
| 6 | +import TableCell from '@material-ui/core/TableCell'; |
| 7 | +import TableFooter from '@material-ui/core/TableFooter'; |
| 8 | +import TablePagination from '@material-ui/core/TablePagination'; |
| 9 | +import {userTableStyles} from '../../helpers/styles/styles'; |
| 10 | +import {withStyles} from '@material-ui/core'; |
| 11 | +import * as PropTypes from 'prop-types'; |
| 12 | +import EnhancedTableHead from '../EnhancedTableHeader/EnhancedTableHeader'; |
| 13 | + |
| 14 | +function desc(a, b, orderBy) { |
| 15 | + if (b[orderBy] < a[orderBy]) { |
| 16 | + return -1; |
| 17 | + } |
| 18 | + if (b[orderBy] > a[orderBy]) { |
| 19 | + return 1; |
| 20 | + } |
| 21 | + return 0; |
| 22 | +} |
| 23 | + |
| 24 | +function stableSort(array, cmp) { |
| 25 | + const stabilizedThis = array.map((el, index) => [el, index]); |
| 26 | + stabilizedThis.sort((a, b) => { |
| 27 | + const order = cmp(a[0], b[0]); |
| 28 | + if (order !== 0) return order; |
| 29 | + return a[1] - b[1]; |
| 30 | + }); |
| 31 | + return stabilizedThis.map(el => el[0]); |
| 32 | +} |
| 33 | + |
| 34 | +function getSorting(order, orderBy) { |
| 35 | + return order === 'desc' ? (a, b) => desc(a, b, orderBy) : (a, b) => -desc(a, b, orderBy); |
| 36 | +} |
| 37 | + |
| 38 | +const PaginationTable = props => { |
| 39 | + const {classes, rows, rowsPerPage, page, order, orderBy} = props; |
| 40 | + const filteredRows = rows.filter((_, index) => index !== 0); |
| 41 | + const emptyRows = rowsPerPage - Math.min(rowsPerPage, rows.length - page * rowsPerPage); |
| 42 | + return ( |
| 43 | + <Table className={classes.table}> |
| 44 | + <EnhancedTableHead numSelected={0} onRequestSort={props.handleRequestSort} order={order} orderBy={orderBy} rows={rows} /> |
| 45 | + <TableBody> |
| 46 | + {stableSort(filteredRows, getSorting(order, orderBy)) |
| 47 | + .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) |
| 48 | + .map(row => ( |
| 49 | + <TableRow key={row.id}> |
| 50 | + {Object.values(row).map(value => ( |
| 51 | + <TableCell component="th" scope="row"> |
| 52 | + {value} |
| 53 | + </TableCell> |
| 54 | + ))} |
| 55 | + </TableRow> |
| 56 | + ))} |
| 57 | + {emptyRows > 0 && ( |
| 58 | + <TableRow style={{height: 48 * emptyRows}}> |
| 59 | + <TableCell colSpan={6} /> |
| 60 | + </TableRow> |
| 61 | + )} |
| 62 | + </TableBody> |
| 63 | + <TableFooter> |
| 64 | + <TableRow> |
| 65 | + <TablePagination |
| 66 | + rowsPerPageOptions={[5, 10, 25]} |
| 67 | + colSpan={5} |
| 68 | + count={rows.length} |
| 69 | + rowsPerPage={rowsPerPage} |
| 70 | + page={page} |
| 71 | + SelectProps={{ |
| 72 | + native: true |
| 73 | + }} |
| 74 | + onChangePage={() => props.handleChangePage} |
| 75 | + onChangeRowsPerPage={() => props.handleChangeRowsPerPage} |
| 76 | + ActionsComponent={TablePaginationActions} |
| 77 | + /> |
| 78 | + </TableRow> |
| 79 | + </TableFooter> |
| 80 | + </Table> |
| 81 | + ); |
| 82 | +}; |
| 83 | + |
| 84 | +PaginationTable.propTypes = { |
| 85 | + classes: PropTypes.object.isRequired, |
| 86 | + rows: PropTypes.array.isRequired, |
| 87 | + rowsPerPage: PropTypes.number.isRequired, |
| 88 | + page: PropTypes.number.isRequired, |
| 89 | + order: PropTypes.any, |
| 90 | + orderBy: PropTypes.any, |
| 91 | + handleChangePage: PropTypes.func.isRequired, |
| 92 | + handleChangeRowsPerPage: PropTypes.func.isRequired, |
| 93 | + handleRequestSort: PropTypes.func.isRequired |
| 94 | +}; |
| 95 | + |
| 96 | +export default withStyles(userTableStyles)(PaginationTable); |
0 commit comments