Applying option to fixed elements on top of table when sorting
This commit is contained in:
parent
de8031a246
commit
70dd3767b4
|
@ -15,6 +15,8 @@ type Props<K> = {
|
||||||
data: Array<K>,
|
data: Array<K>,
|
||||||
classes: Object,
|
classes: Object,
|
||||||
children: Function,
|
children: Function,
|
||||||
|
size: number,
|
||||||
|
defaultFixed?: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
|
@ -46,8 +48,9 @@ const styles = {
|
||||||
class GnoTable<K> extends React.Component<Props<K>, State> {
|
class GnoTable<K> extends React.Component<Props<K>, State> {
|
||||||
state = {
|
state = {
|
||||||
page: 0,
|
page: 0,
|
||||||
order: 'desc',
|
order: 'asc',
|
||||||
orderBy: this.props.defaultOrderBy,
|
orderBy: this.props.defaultOrderBy,
|
||||||
|
fixed: !!this.props.defaultFixed,
|
||||||
orderProp: false,
|
orderProp: false,
|
||||||
rowsPerPage: 5,
|
rowsPerPage: 5,
|
||||||
}
|
}
|
||||||
|
@ -60,7 +63,9 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
|
||||||
order = 'asc'
|
order = 'asc'
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setState({ order, orderBy, orderProp })
|
this.setState(() => ({
|
||||||
|
order, orderBy, orderProp, fixed: false,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
handleChangePage = (page: number) => {
|
handleChangePage = (page: number) => {
|
||||||
|
@ -74,11 +79,11 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
data, label, columns, classes, children,
|
data, label, columns, classes, children, size,
|
||||||
} = this.props
|
} = this.props
|
||||||
|
|
||||||
const {
|
const {
|
||||||
order, orderBy, page, orderProp, rowsPerPage,
|
order, orderBy, page, orderProp, rowsPerPage, fixed,
|
||||||
} = this.state
|
} = this.state
|
||||||
|
|
||||||
const backProps = {
|
const backProps = {
|
||||||
|
@ -95,7 +100,7 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
|
||||||
input: classes.white,
|
input: classes.white,
|
||||||
}
|
}
|
||||||
|
|
||||||
const sortedData = stableSort(data, getSorting(order, orderBy, orderProp))
|
const sortedData = stableSort(data, getSorting(order, orderBy, orderProp), fixed)
|
||||||
.slice(page * rowsPerPage, ((page * rowsPerPage) + rowsPerPage))
|
.slice(page * rowsPerPage, ((page * rowsPerPage) + rowsPerPage))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -113,7 +118,7 @@ class GnoTable<K> extends React.Component<Props<K>, State> {
|
||||||
</Table>
|
</Table>
|
||||||
<TablePagination
|
<TablePagination
|
||||||
component="div"
|
component="div"
|
||||||
count={data.length}
|
count={size}
|
||||||
rowsPerPage={rowsPerPage}
|
rowsPerPage={rowsPerPage}
|
||||||
page={page}
|
page={page}
|
||||||
backIconButtonProps={backProps}
|
backIconButtonProps={backProps}
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
|
export const FIXED = 'fixed'
|
||||||
|
export type SortRow = {
|
||||||
|
[FIXED]: boolean,
|
||||||
|
}
|
||||||
|
|
||||||
export const buildOrderFieldFrom = (attr: string) => `${attr}Order`
|
export const buildOrderFieldFrom = (attr: string) => `${attr}Order`
|
||||||
|
|
||||||
|
|
||||||
const desc = (a: Object, b: Object, orderBy: string, orderProp: boolean) => {
|
const desc = (a: Object, b: Object, orderBy: string, orderProp: boolean) => {
|
||||||
const order = orderProp ? buildOrderFieldFrom(orderBy) : orderBy
|
const order = orderProp ? buildOrderFieldFrom(orderBy) : orderBy
|
||||||
|
|
||||||
|
@ -15,7 +21,8 @@ const desc = (a: Object, b: Object, orderBy: string, orderProp: boolean) => {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
export const stableSort = (array: any, cmp: any) => {
|
export const stableSort = (array: any, cmp: any, fixed: boolean) => {
|
||||||
|
const fixedElems = fixed ? array.filter(elem => elem[FIXED]) : []
|
||||||
const stabilizedThis = array.map((el, index) => [el, index])
|
const stabilizedThis = array.map((el, index) => [el, index])
|
||||||
|
|
||||||
stabilizedThis.sort((a, b) => {
|
stabilizedThis.sort((a, b) => {
|
||||||
|
@ -27,7 +34,9 @@ export const stableSort = (array: any, cmp: any) => {
|
||||||
return a[1] - b[1]
|
return a[1] - b[1]
|
||||||
})
|
})
|
||||||
|
|
||||||
return stabilizedThis.map(el => el[0])
|
const sortedElems = stabilizedThis.map(el => el[0])
|
||||||
|
|
||||||
|
return fixedElems.concat(sortedElems)
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Order = 'asc' | 'desc'
|
export type Order = 'asc' | 'desc'
|
||||||
|
|
|
@ -1,25 +1,33 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { List } from 'immutable'
|
import { List } from 'immutable'
|
||||||
import { buildOrderFieldFrom } from '~/components/Table/sorting'
|
import { buildOrderFieldFrom, FIXED, type SortRow } from '~/components/Table/sorting'
|
||||||
import { type Column } from '~/components/Table/TableHead'
|
import { type Column } from '~/components/Table/TableHead'
|
||||||
|
|
||||||
export const BALANCE_TABLE_ASSET_ID = 'asset'
|
export const BALANCE_TABLE_ASSET_ID = 'asset'
|
||||||
export const BALANCE_TABLE_BALANCE_ID = 'balance'
|
export const BALANCE_TABLE_BALANCE_ID = 'balance'
|
||||||
export const BALANCE_TABLE_VALUE_ID = 'value'
|
export const BALANCE_TABLE_VALUE_ID = 'value'
|
||||||
|
|
||||||
export type BalanceRow = {
|
export type BalanceRow = SortRow & {
|
||||||
asset: string,
|
asset: string,
|
||||||
balance: string,
|
balance: string,
|
||||||
value: string,
|
value: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getBalanceData = (): Array<BalanceRow> => [
|
export const getBalanceData = (): Array<BalanceRow> => [
|
||||||
|
{
|
||||||
|
[BALANCE_TABLE_ASSET_ID]: 'ABC Periodico',
|
||||||
|
[BALANCE_TABLE_BALANCE_ID]: '1.394 ABC',
|
||||||
|
[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)]: 1.394,
|
||||||
|
[BALANCE_TABLE_VALUE_ID]: '$3.1',
|
||||||
|
[buildOrderFieldFrom(BALANCE_TABLE_VALUE_ID)]: 3.1,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
[BALANCE_TABLE_ASSET_ID]: 'Ethereum',
|
[BALANCE_TABLE_ASSET_ID]: 'Ethereum',
|
||||||
[BALANCE_TABLE_BALANCE_ID]: '9.394 ETH',
|
[BALANCE_TABLE_BALANCE_ID]: '9.394 ETH',
|
||||||
[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)]: 9.394,
|
[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)]: 9.394,
|
||||||
[BALANCE_TABLE_VALUE_ID]: '$539.45',
|
[BALANCE_TABLE_VALUE_ID]: '$539.45',
|
||||||
[buildOrderFieldFrom(BALANCE_TABLE_VALUE_ID)]: 539.45,
|
[buildOrderFieldFrom(BALANCE_TABLE_VALUE_ID)]: 539.45,
|
||||||
|
[FIXED]: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
[BALANCE_TABLE_ASSET_ID]: 'Gnosis',
|
[BALANCE_TABLE_ASSET_ID]: 'Gnosis',
|
||||||
|
|
|
@ -162,6 +162,8 @@ class Balances extends React.Component<Props, State> {
|
||||||
defaultOrderBy={BALANCE_TABLE_ASSET_ID}
|
defaultOrderBy={BALANCE_TABLE_ASSET_ID}
|
||||||
columns={columns}
|
columns={columns}
|
||||||
data={filteredData}
|
data={filteredData}
|
||||||
|
size={filteredData.length}
|
||||||
|
defaultFixed
|
||||||
>
|
>
|
||||||
{(sortedData: Array<BalanceRow>) => sortedData.map((row: any, index: number) => (
|
{(sortedData: Array<BalanceRow>) => sortedData.map((row: any, index: number) => (
|
||||||
<TableRow tabIndex={-1} key={index} className={classes.hide}>
|
<TableRow tabIndex={-1} key={index} className={classes.hide}>
|
||||||
|
|
Loading…
Reference in New Issue