|
@@ -0,0 +1,145 @@
|
|
|
+import React, { Component } from 'react'
|
|
|
+import Paper from 'material-ui/Paper'
|
|
|
+import Table, { TableHead, TableBody, TableRow, TableCell } from 'material-ui/Table'
|
|
|
+import Checkbox from 'material-ui/Checkbox'
|
|
|
+import { isEqual, size, indexOf, map, get, pick, concat, difference, slice } from 'lodash'
|
|
|
+import PropTypes from 'prop-types'
|
|
|
+import { withStyles } from 'material-ui/styles'
|
|
|
+
|
|
|
+const styles = theme => ({
|
|
|
+ root: {
|
|
|
+ width: '100%'
|
|
|
+ },
|
|
|
+ checkColumn: {
|
|
|
+ width: 50
|
|
|
+ },
|
|
|
+ tableRow: {
|
|
|
+ '&:hover': {
|
|
|
+ backgroundColor: theme.palette.grey[200],
|
|
|
+ cursor: 'pointer'
|
|
|
+ }
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+class DataTable extends Component {
|
|
|
+
|
|
|
+ constructor(props) {
|
|
|
+ super(props)
|
|
|
+
|
|
|
+ this.state = {
|
|
|
+ selectedRows: [],
|
|
|
+ isSelectable: props.isSelectable
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param {*} e
|
|
|
+ */
|
|
|
+ selectAll(e) {
|
|
|
+ const { selectedRows } = this.state
|
|
|
+ const { rows } = this.props
|
|
|
+
|
|
|
+ this.setState({
|
|
|
+ ...this.state,
|
|
|
+ selectedRows: isEqual(size(selectedRows), size(rows)) ? [] : map(rows, r => {
|
|
|
+ return get(pick(r, 'id'), 'id')
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param {*} e
|
|
|
+ * @param {*} id
|
|
|
+ */
|
|
|
+ select(e, id) {
|
|
|
+ const { selectedRows } = this.state
|
|
|
+ let index = indexOf(selectedRows, id)
|
|
|
+
|
|
|
+ this.setState({
|
|
|
+ ...this.state,
|
|
|
+ selectedRows: isEqual(index, -1) ? concat(selectedRows, id) : difference(selectedRows, slice(selectedRows, index, index + 1))
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ clearSelection() {
|
|
|
+ this.setState({
|
|
|
+ ...this.state,
|
|
|
+ selectedRows: []
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ render() {
|
|
|
+ const { isSelectable, selectedRows } = this.state
|
|
|
+ const { classes, gutterTop, columns, rows } = this.props
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Paper className={classes.root} style={ gutterTop ? {marginTop: 20} : {}}>
|
|
|
+ <Table>
|
|
|
+ <TableHead>
|
|
|
+ <TableRow>
|
|
|
+ {isSelectable && (
|
|
|
+ <TableCell
|
|
|
+ padding='checkbox'
|
|
|
+ children={
|
|
|
+ <Checkbox
|
|
|
+ color='primary'
|
|
|
+ checked={isEqual(size(selectedRows), size(rows))}
|
|
|
+ onClick={e => this.selectAll(e)}
|
|
|
+ />
|
|
|
+ }
|
|
|
+ />
|
|
|
+ )}
|
|
|
+ {columns.map(c => <TableCell key={c.key} children={c.title} />)}
|
|
|
+ </TableRow>
|
|
|
+ </TableHead>
|
|
|
+ <TableBody>
|
|
|
+ {rows.map(r =>
|
|
|
+ <TableRow key={r.id} className={classes.tableRow}>
|
|
|
+ {isSelectable && (
|
|
|
+ <TableCell
|
|
|
+ padding='checkbox'
|
|
|
+ children={
|
|
|
+ <Checkbox
|
|
|
+ color='primary'
|
|
|
+ checked={!isEqual(indexOf(selectedRows, r.id), -1)}
|
|
|
+ onClick={e => this.select(e, r.id)}
|
|
|
+ />
|
|
|
+ }
|
|
|
+ />
|
|
|
+ )}
|
|
|
+ {columns.map(c =>
|
|
|
+ <TableCell key={c.key} children={!c.options ? r[c.key] : c.options[c.key].text} />
|
|
|
+ )}
|
|
|
+ </TableRow>
|
|
|
+ )}
|
|
|
+ </TableBody>
|
|
|
+ </Table>
|
|
|
+ </Paper>
|
|
|
+ )
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+DataTable.propTypes = {
|
|
|
+ columns: PropTypes.array.isRequired,
|
|
|
+ rows: PropTypes.array.isRequired,
|
|
|
+ onSelect: PropTypes.func.isRequired,
|
|
|
+ isSelectable: PropTypes.bool,
|
|
|
+ gutterTop: PropTypes.bool
|
|
|
+}
|
|
|
+
|
|
|
+DataTable.defaultProps = {
|
|
|
+ isSelectable: false,
|
|
|
+ gutterTop: true
|
|
|
+}
|
|
|
+
|
|
|
+DataTable = withStyles(styles)(DataTable)
|
|
|
+
|
|
|
+export default DataTable
|