From 3891f3c1a5e13ac6b52a8ef4b556e80a517b403d Mon Sep 17 00:00:00 2001 From: apanizo Date: Thu, 18 Oct 2018 12:19:08 +0200 Subject: [PATCH] Adapting sorting to accept custom fields in Table component --- src/components/Table/TableHead.jsx | 27 ++++++++++----------------- src/components/Table/index.jsx | 16 ++++++++++------ src/components/Table/sorting.js | 16 +++++++++++----- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/components/Table/TableHead.jsx b/src/components/Table/TableHead.jsx index cd02a4a9..e3514a08 100644 --- a/src/components/Table/TableHead.jsx +++ b/src/components/Table/TableHead.jsx @@ -5,27 +5,26 @@ import TableCell from '@material-ui/core/TableCell' import TableHead from '@material-ui/core/TableHead' import TableRow from '@material-ui/core/TableRow' import TableSortLabel from '@material-ui/core/TableSortLabel' -import Tooltip from '@material-ui/core/Tooltip' import { type Order } from '~/components/Table/sorting' export type Column = { id: string, numeric: boolean, + order: boolean, disablePadding: boolean, label: string, - tooltip?: string, } type Props = { columns: List, orderBy: string, // id of one of the described columns order: Order, - onSort: (property: string) => void, + onSort: (property: string, orderAttr: boolean) => void, } class GnoTableHead extends React.PureComponent { - changeSort = (property: string) => () => { - this.props.onSort(property) + changeSort = (property: string, orderAttr: boolean) => () => { + this.props.onSort(property, orderAttr) } render() { @@ -41,19 +40,13 @@ class GnoTableHead extends React.PureComponent { padding={column.disablePadding ? 'none' : 'default'} sortDirection={orderBy === column.id ? order : false} > - - - {column.label} - - + {column.label} + ))} diff --git a/src/components/Table/index.jsx b/src/components/Table/index.jsx index 4c937fc0..c1cfbcc7 100644 --- a/src/components/Table/index.jsx +++ b/src/components/Table/index.jsx @@ -20,6 +20,7 @@ type State = { page: number, order: Order, orderBy: string, + orderProp: boolean, } class GnoTable extends React.Component, State> { @@ -27,9 +28,10 @@ class GnoTable extends React.Component, State> { page: 0, order: 'desc', orderBy: this.props.defaultOrderBy, + orderProp: false, } - onSort = (property: string) => { + onSort = (property: string, orderProp: boolean) => { const orderBy = property let order = 'desc' @@ -37,14 +39,16 @@ class GnoTable extends React.Component, State> { order = 'asc' } - this.setState({ order, orderBy }) + this.setState({ order, orderBy, orderProp }) } render() { const { data, label, columns, rowsPerPage = 5, } = this.props - const { order, orderBy, page } = this.state + const { + order, orderBy, page, orderProp, + } = this.state return ( @@ -55,12 +59,12 @@ class GnoTable extends React.Component, State> { onSort={this.onSort} /> - {stableSort(data, getSorting(order, orderBy)) + {stableSort(data, getSorting(order, orderBy, orderProp)) .slice(page * rowsPerPage, ((page * rowsPerPage) + rowsPerPage)) - .map((row: any) => ( + .map((row: any, index: number) => ( { columns.map((column: Column) => ( diff --git a/src/components/Table/sorting.js b/src/components/Table/sorting.js index 15efdaaf..75dc2255 100644 --- a/src/components/Table/sorting.js +++ b/src/components/Table/sorting.js @@ -1,11 +1,17 @@ // @flow -const desc = (a, b, orderBy) => { - if (b[orderBy] < a[orderBy]) { + +export const buildOrderFieldFrom = (attr: string) => `${attr}Order` + +const desc = (a: Object, b: Object, orderBy: string, orderProp: boolean) => { + const order = orderProp ? buildOrderFieldFrom(orderBy) : orderBy + + if (b[order] < a[order]) { return -1 } - if (b[orderBy] > a[orderBy]) { + if (b[order] > a[order]) { return 1 } + return 0 } @@ -26,5 +32,5 @@ export const stableSort = (array: any, cmp: any) => { export type Order = 'asc' | 'desc' -export const getSorting = (order: Order, orderBy: string) => - (order === 'desc' ? (a: any, b: any) => desc(a, b, orderBy) : (a: any, b: any) => -desc(a, b, orderBy)) +export const getSorting = (order: Order, orderBy: string, orderProp: boolean) => + (order === 'desc' ? (a: any, b: any) => desc(a, b, orderBy, orderProp) : (a: any, b: any) => -desc(a, b, orderBy, orderProp))