/*
Copyright (C) 2017 Cloudbase Solutions SRL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
import React, { Component, PropTypes } from 'react';
import Collapse from 'react-collapse';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Table.scss';
class Table extends Component {
static defaultProps = {
headerItems: [],
listItems: [],
show: true
}
static propTypes = {
headerItems: PropTypes.array,
listItems: PropTypes.array,
customClassName: PropTypes.string,
show: PropTypes.bool
}
constructor(props) {
super(props)
this.state = {
openState: []
}
}
componentWillReceiveProps(newProps) {
let openState = []
for (let i in newProps.listItems) {
openState.push(newProps.listItems[i].openState)
}
this.setState({ openState: openState })
}
toggleDrawer(index) {
let newOpenState = this.state.openState
let toggled = !newOpenState[index]
for (let i in newOpenState) {
newOpenState[i] = false
}
newOpenState[index] = toggled
this.setState({ openState: newOpenState })
}
rowMouseDown(e) {
this.dragStartPosition = {x: e.screenX, y: e.screenY};
}
rowMouseUp(e, index) {
this.dragStartPosition = this.dragStartPosition || {x: e.screenX, y: e.screenY};
// If a drag operation has been initiated (i.e. text selection), don't call toggleDrawer
if (Math.abs(this.dragStartPosition.x - e.screenX) < 3 && Math.abs(this.dragStartPosition.y - e.screenY) < 3) {
this.toggleDrawer(index);
}
this.dragStartPosition = null;
}
render() {
let headerItems = this.props.headerItems.map((item, index) =>
{item.label}
)
let listItems = (No results
)
if (this.props.listItems) {
listItems = this.props.listItems.map((listItem, index) => {
let row = this.props.headerItems.map((headerItem) =>
(
{listItem[headerItem.key]}
)
)
let detailView = null
if (listItem.detailView) {
detailView = (
{listItem.detailView}
)
}
return (
this.rowMouseDown(e)}
onMouseUp={e => this.rowMouseUp(e, index)}
>
{row} {detailView}
)
}, this)
}
return (
{headerItems}
{listItems}
);
}
}
export default withStyles(Table, s);