Newer
Older
import React from 'react'
import { withStyles } from '@material-ui/core/styles'
import TableRow from '@material-ui/core/TableRow'
piotr.kupczyk@id.ethz.ch
committed
import GridCell from '@src/js/components/common/grid/GridCell.jsx'
piotr.kupczyk@id.ethz.ch
committed
import GridMultiselectCell from '@src/js/components/common/grid/GridMultiselectCell.jsx'
import logger from '@src/js/common/logger.js'
piotr.kupczyk@id.ethz.ch
committed
const styles = theme => ({
row: {
backgroundColor: theme.palette.background.paper,
'&:hover': {
backgroundColor: '#f5f5f5'
},
'&:hover$selected': {
backgroundColor: '#e8f7fd'
}
},
clickable: {
cursor: 'pointer'
},
selectable: {
piotr.kupczyk@id.ethz.ch
committed
cursor: 'pointer'
piotr.kupczyk@id.ethz.ch
committed
},
selected: {
backgroundColor: '#e8f7fd'
},
multiselectable: {},
cell: {
backgroundColor: 'inherit',
'&$firstCell': {
paddingLeft: theme.spacing(2),
position: 'sticky',
left: 0,
zIndex: 100
},
'$multiselectable &$firstCell': {
piotr.kupczyk@id.ethz.ch
committed
left: '48px'
}
},
firstCell: {}
})
class GridRow extends React.PureComponent {
constructor(props) {
super(props)
piotr.kupczyk@id.ethz.ch
committed
this.handleClick = this.handleClick.bind(this)
this.handleDoubleClick = this.handleDoubleClick.bind(this)
piotr.kupczyk@id.ethz.ch
committed
this.handleMultiselect = this.handleMultiselect.bind(this)
piotr.kupczyk@id.ethz.ch
committed
handleClick() {
const { clickable, selectable, onClick, onSelect, row } = this.props
piotr.kupczyk@id.ethz.ch
committed
if (selectable && onSelect) {
onSelect(row)
}
piotr.kupczyk@id.ethz.ch
committed
if (clickable && onClick) {
onClick(row)
}
piotr.kupczyk@id.ethz.ch
committed
}
handleDoubleClick() {
const { doubleClickable, onDoubleClick, row } = this.props
if (doubleClickable && onDoubleClick) {
onDoubleClick(row)
}
}
piotr.kupczyk@id.ethz.ch
committed
handleMultiselect(event) {
event.preventDefault()
event.stopPropagation()
const { multiselectable, onMultiselect, row } = this.props
if (multiselectable && onMultiselect) {
onMultiselect(row)
}
}
render() {
logger.log(logger.DEBUG, 'GridRow.render')
const {
multiselectable,
columns,
row,
clickable,
selectable,
selected,
classes
} = this.props
piotr.kupczyk@id.ethz.ch
committed
if (columns.length === 0) {
return null
}
const rowClasses = [classes.row]
if (multiselectable) {
rowClasses.push(classes.multiselectable)
}
if (selectable) {
rowClasses.push(classes.selectable)
}
if (selected) {
rowClasses.push(classes.selected)
}
if (clickable) {
rowClasses.push(classes.clickable)
}
return (
<TableRow
key={row.id}
piotr.kupczyk@id.ethz.ch
committed
onClick={this.handleClick}
onDoubleClick={this.handleDoubleClick}
classes={{ root: rowClasses.join(' ') }}
piotr.kupczyk@id.ethz.ch
committed
{this.renderMultiselect()}
{columns.map((column, columnIndex) =>
this.renderCell(column, columnIndex, row)
)}
</TableRow>
)
}
piotr.kupczyk@id.ethz.ch
committed
renderCell(column, columnIndex, row) {
piotr.kupczyk@id.ethz.ch
committed
const { heights, classes } = this.props
const cellClasses = [classes.cell]
if (columnIndex === 0) {
cellClasses.push(classes.firstCell)
}
return (
<GridCell
key={column.name}
row={row}
piotr.kupczyk@id.ethz.ch
committed
height={heights ? heights[column.name] : undefined}
column={column}
piotr.kupczyk@id.ethz.ch
committed
className={cellClasses.join(' ')}
piotr.kupczyk@id.ethz.ch
committed
onMeasured={this.props.onMeasured}
/>
)
}
piotr.kupczyk@id.ethz.ch
committed
renderMultiselect() {
piotr.kupczyk@id.ethz.ch
committed
const { multiselectable, multiselected } = this.props
piotr.kupczyk@id.ethz.ch
committed
piotr.kupczyk@id.ethz.ch
committed
if (multiselectable) {
piotr.kupczyk@id.ethz.ch
committed
return (
piotr.kupczyk@id.ethz.ch
committed
<GridMultiselectCell
value={multiselected}
onClick={this.handleMultiselect}
/>
piotr.kupczyk@id.ethz.ch
committed
)
} else {
return null
}
}
}
export default withStyles(styles)(GridRow)