Newer
Older
import React from 'react'
import { withStyles } from '@material-ui/core/styles'
piotr.kupczyk@id.ethz.ch
committed
import TableCell from '@material-ui/core/TableCell'
piotr.kupczyk@id.ethz.ch
committed
import TableSortLabel from '@material-ui/core/TableSortLabel'
piotr.kupczyk@id.ethz.ch
committed
import GridSortingOptions from '@src/js/components/common/grid/GridSortingOptions.js'
import logger from '@src/js/common/logger.js'
const styles = theme => ({
piotr.kupczyk@id.ethz.ch
committed
cell: {
backgroundColor: theme.palette.background.primary,
borderColor: theme.palette.border.secondary,
fontWeight: 'bold',
piotr.kupczyk@id.ethz.ch
committed
paddingTop: theme.spacing(1),
paddingBottom: theme.spacing(1),
paddingLeft: 0,
paddingRight: theme.spacing(2)
piotr.kupczyk@id.ethz.ch
committed
},
sortIndex: {
color: theme.typography.label.color,
position: 'absolute',
right: 0,
paddingTop: '10px',
fontSize: theme.typography.label.fontSize
}
})
class GridHeader extends React.PureComponent {
piotr.kupczyk@id.ethz.ch
committed
constructor(props) {
super(props)
piotr.kupczyk@id.ethz.ch
committed
this.handleClick = this.handleClick.bind(this)
piotr.kupczyk@id.ethz.ch
committed
}
piotr.kupczyk@id.ethz.ch
committed
handleClick(event) {
piotr.kupczyk@id.ethz.ch
committed
const { column, onSortChange } = this.props
if (onSortChange) {
piotr.kupczyk@id.ethz.ch
committed
onSortChange(column, event.ctrlKey || event.metaKey)
piotr.kupczyk@id.ethz.ch
committed
}
}
render() {
logger.log(logger.DEBUG, 'GridHeader.render')
piotr.kupczyk@id.ethz.ch
committed
const { column, sortCount, sortIndex, sortDirection, className, classes } =
piotr.kupczyk@id.ethz.ch
committed
piotr.kupczyk@id.ethz.ch
committed
if (column.sortable) {
piotr.kupczyk@id.ethz.ch
committed
const active = sortIndex !== null && sortDirection !== null
piotr.kupczyk@id.ethz.ch
committed
return (
<TableCell
classes={{
piotr.kupczyk@id.ethz.ch
committed
root: `${className} ${classes.cell}`
piotr.kupczyk@id.ethz.ch
committed
<TableSortLabel
active={active}
piotr.kupczyk@id.ethz.ch
committed
direction={active ? sortDirection : GridSortingOptions.ASC}
piotr.kupczyk@id.ethz.ch
committed
onClick={this.handleClick}
>
{column.label}
piotr.kupczyk@id.ethz.ch
committed
{sortCount > 1 && sortIndex !== null && (
<span className={classes.sortIndex}>{sortIndex + 1}</span>
)}
piotr.kupczyk@id.ethz.ch
committed
</TableSortLabel>
piotr.kupczyk@id.ethz.ch
committed
</TableCell>
)
piotr.kupczyk@id.ethz.ch
committed
} else {
piotr.kupczyk@id.ethz.ch
committed
return (
<TableCell
classes={{
piotr.kupczyk@id.ethz.ch
committed
root: `${className} ${classes.cell}`
}}
>
{column.label}
</TableCell>
piotr.kupczyk@id.ethz.ch
committed
)
piotr.kupczyk@id.ethz.ch
committed
}
}
}
export default withStyles(styles)(GridHeader)