import React from 'react'; import PropTypes from 'prop-types'; import { withStyles } from '@material-ui/core/styles'; import classNames from 'classnames'; import DeleteIcon from '@material-ui/icons/Delete'; import EditIcon from '@material-ui/icons/BorderColor'; import DoneIcon from '@material-ui/icons/Done'; import css from 'ba-styles/Table.scss'; import { TableCell, IconButton } from '@material-ui/core'; import EditableCell from './EditableCell'; import SelectableCell from './SelectableCell'; import ToggleCell from './ToggleCell'; import DatePickerCell from './DatePickerCell'; import TimePickerCell from './TimePickerCell'; const styles = theme => ({ button: { margin: theme.spacing(1), }, }); class Row extends React.Component { render() { const { classes, anchor, item, removeRow, updateRow, editRow, finishEditRow, branch } = this.props; const eventDel = () => { removeRow(item, branch); }; const eventEdit = () => { editRow(item, branch); }; const eventDone = () => { finishEditRow(item, branch); }; const renderCell = dataArray => dataArray.map((itemCell, index) => { if (itemCell.name !== 'action' && !itemCell.hidden) { const inputType = anchor[index].type; switch (inputType) { case 'selection': return ( updateRow(event, branch)} cellData={{ type: itemCell.name, value: item.get(itemCell.name), id: item.get('id'), }} edited={item.get('edited')} key={index.toString()} options={anchor[index].options} branch={branch} /> ); case 'toggle': return ( updateRow(event, branch)} cellData={{ type: itemCell.name, value: item.get(itemCell.name), id: item.get('id'), }} edited={item.get('edited')} key={index.toString()} branch={branch} /> ); case 'date': return ( updateRow(event, branch)} cellData={{ type: itemCell.name, value: item.get(itemCell.name), id: item.get('id'), }} edited={item.get('edited')} key={index.toString()} branch={branch} /> ); case 'time': return ( updateRow(event, branch)} cellData={{ type: itemCell.name, value: item.get(itemCell.name), id: item.get('id'), }} edited={item.get('edited')} key={index.toString()} branch={branch} /> ); default: return ( updateRow(event, branch)} cellData={{ type: itemCell.name, value: item.get(itemCell.name), id: item.get('id'), }} edited={item.get('edited')} key={index.toString()} inputType={inputType} branch={branch} /> ); } } return false; }); return ( {renderCell(anchor)} eventEdit(this)} className={classNames((item.get('edited') ? css.hideAction : ''), classes.button)} aria-label="Edit" > eventDone(this)} color="secondary" className={classNames((!item.get('edited') ? css.hideAction : ''), classes.button)} aria-label="Done" > eventDel(this)} className={classes.button} aria-label="Delete" > ); } } Row.propTypes = { classes: PropTypes.object.isRequired, anchor: PropTypes.array.isRequired, item: PropTypes.object.isRequired, removeRow: PropTypes.func.isRequired, updateRow: PropTypes.func.isRequired, editRow: PropTypes.func.isRequired, finishEditRow: PropTypes.func.isRequired, branch: PropTypes.string.isRequired }; export default withStyles(styles)(Row);