This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12ff0f3
commit c5ab79e
Showing
4 changed files
with
170 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import React, {useState} from 'react'; | ||
import {map, get} from 'lodash/fp'; | ||
import { | ||
TableBody, | ||
Table, | ||
TableHead, | ||
TableRow, | ||
TableCell, | ||
TableSortLabel, | ||
} from '@material-ui/core'; | ||
|
||
import HelpLink from '../../HelpLink'; | ||
import useStyles from './styles'; | ||
|
||
function desc(a, b, orderBy) { | ||
const getA = get(orderBy)(a); | ||
const getB = get(orderBy)(b); | ||
if (getB < getA) { | ||
return -1; | ||
} | ||
if (getB > getA) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
function stableSort(array, cmp) { | ||
const stabilizedThis = map((el, index) => [el, index])(array); | ||
stabilizedThis.sort((a, b) => { | ||
const order = cmp(a[0], b[0]); | ||
if (order !== 0) return order; | ||
return a[1] - b[1]; | ||
}); | ||
return map(el => el[0])(stabilizedThis); | ||
} | ||
|
||
function getSorting(order, orderBy) { | ||
return order === 'desc' | ||
? (a, b) => desc(a, b, orderBy) | ||
: (a, b) => -desc(a, b, orderBy); | ||
} | ||
|
||
const EnhancedTableHead = props => { | ||
const classes = useStyles(); | ||
const {fields, order, orderBy, onRequestSort, text, term} = props; | ||
|
||
const createSortHandler = property => event => { | ||
onRequestSort(event, property); | ||
}; | ||
|
||
return ( | ||
<TableHead className={classes.head}> | ||
<TableRow> | ||
{map( | ||
headCell => ( | ||
<TableCell | ||
key={headCell.id} | ||
align={headCell.align || 'left'} | ||
padding={headCell.disablePadding ? 'none' : 'default'} | ||
sortDirection={orderBy === headCell.id ? order : false} | ||
> | ||
<TableSortLabel | ||
active={orderBy === headCell.id} | ||
direction={order} | ||
onClick={createSortHandler(headCell.id)} | ||
> | ||
{headCell.label} | ||
<HelpLink text={text} term={term} /> | ||
</TableSortLabel> | ||
</TableCell> | ||
), | ||
fields, | ||
)} | ||
</TableRow> | ||
</TableHead> | ||
); | ||
}; | ||
|
||
const EnhancedTable = props => { | ||
const classes = useStyles(); | ||
const {data = [], fields, renderRow, initialSort = 'name'} = props; | ||
const [order, setOrder] = useState('asc'); | ||
const [orderBy, setOrderBy] = React.useState(initialSort); | ||
const handleRequestSort = (event, property) => { | ||
const isDesc = orderBy === property && order === 'desc'; | ||
setOrder(isDesc ? 'asc' : 'desc'); | ||
setOrderBy(property); | ||
}; | ||
|
||
return ( | ||
<Table className={classes.root}> | ||
<EnhancedTableHead | ||
classes={classes} | ||
order={order} | ||
orderBy={orderBy} | ||
onRequestSort={handleRequestSort} | ||
fields={fields} | ||
/> | ||
<TableBody> | ||
{map(renderRow)(stableSort(data, getSorting(order, orderBy)))} | ||
</TableBody> | ||
</Table> | ||
); | ||
}; | ||
|
||
EnhancedTable.propTypes = {}; | ||
|
||
export default EnhancedTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {makeStyles} from '@material-ui/core'; | ||
|
||
export default makeStyles(() => ({ | ||
head: { | ||
border: '1px solid #979797', | ||
'& th': { | ||
textTransform: 'uppercase', | ||
fontSize: 15, | ||
letterSpacing: 2, | ||
fontWeight: 'bold', | ||
borderBottom: 'none', | ||
}, | ||
}, | ||
root: { | ||
'& td': { | ||
maxWidth: 1, | ||
overflow: 'hidden', | ||
textOverflow: 'ellipsis', | ||
}, | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters