Refactor of Table component, customizing cells using a renderProp

This commit is contained in:
apanizo 2018-10-22 11:15:13 +02:00
parent db8cfdb174
commit 54802e3c56
3 changed files with 9 additions and 20 deletions

View File

@ -10,9 +10,10 @@ import { type Order } from '~/components/Table/sorting'
export type Column = { export type Column = {
id: string, id: string,
numeric: boolean, numeric: boolean,
order: boolean, order: boolean, // If data for sorting will be provided in a different attr
disablePadding: boolean, disablePadding: boolean,
label: string, label: string,
custom: boolean, // If content will be rendered by user manually
} }
type Props = { type Props = {

View File

@ -3,8 +3,6 @@ import * as React from 'react'
import { List } from 'immutable' import { List } from 'immutable'
import Table from '@material-ui/core/Table' import Table from '@material-ui/core/Table'
import TableBody from '@material-ui/core/TableBody' import TableBody from '@material-ui/core/TableBody'
import TableRow from '@material-ui/core/TableRow'
import TableCell from '@material-ui/core/TableCell'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import TablePagination from '@material-ui/core/TablePagination' 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'
@ -16,6 +14,7 @@ type Props<K> = {
columns: List<Column>, columns: List<Column>,
data: Array<K>, data: Array<K>,
classes: Object, classes: Object,
children: Function,
} }
type State = { type State = {
@ -75,7 +74,7 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
render() { render() {
const { const {
data, label, columns, classes, data, label, columns, classes, children,
} = this.props } = this.props
const { const {
@ -96,6 +95,9 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
input: classes.white, input: classes.white,
} }
const sortedData = stableSort(data, getSorting(order, orderBy, orderProp))
.slice(page * rowsPerPage, ((page * rowsPerPage) + rowsPerPage))
return ( return (
<React.Fragment> <React.Fragment>
<Table aria-labelledby={label} className={classes.root}> <Table aria-labelledby={label} className={classes.root}>
@ -106,22 +108,7 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
onSort={this.onSort} onSort={this.onSort}
/> />
<TableBody> <TableBody>
{stableSort(data, getSorting(order, orderBy, orderProp)) { children(sortedData) }
.slice(page * rowsPerPage, ((page * rowsPerPage) + rowsPerPage))
.map((row: any, index: number) => (
<TableRow
tabIndex={-1}
key={index}
>
{
columns.map((column: Column) => (
<TableCell key={column.id} numeric={column.numeric} component="th" scope="row">
{row[column.id]}
</TableCell>
))
}
</TableRow>
))}
</TableBody> </TableBody>
</Table> </Table>
<TablePagination <TablePagination

View File

@ -8,6 +8,7 @@ import { type Owner } from '~/routes/safe/store/model/owner'
import { sameAddress } from '~/logic/wallets/ethAddresses' import { sameAddress } from '~/logic/wallets/ethAddresses'
export const safesMapSelector = (state: GlobalState): Map<string, Safe> => state.safes export const safesMapSelector = (state: GlobalState): Map<string, Safe> => state.safes
const safesListSelector: Selector<GlobalState, {}, List<Safe>> = createSelector( const safesListSelector: Selector<GlobalState, {}, List<Safe>> = createSelector(
safesMapSelector, safesMapSelector,
(safes: Map<string, Safe>): List<Safe> => safes.toList(), (safes: Map<string, Safe>): List<Safe> => safes.toList(),