2015-11-03 09:23:23 +03:00
|
|
|
import React from 'react';
|
|
|
|
import Head from './components/head';
|
|
|
|
import Row from './components/row';
|
|
|
|
import style from './style';
|
2015-11-04 08:33:12 +03:00
|
|
|
import utils from '../utils';
|
2015-11-03 09:23:23 +03:00
|
|
|
|
|
|
|
class Table extends React.Component {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
className: React.PropTypes.string,
|
|
|
|
dataSource: React.PropTypes.array,
|
|
|
|
model: React.PropTypes.object,
|
|
|
|
heading: React.PropTypes.bool,
|
|
|
|
onChange: React.PropTypes.func,
|
2015-11-04 15:43:32 +03:00
|
|
|
onSelect: React.PropTypes.func
|
2015-11-03 09:23:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
|
|
|
dataSource: [],
|
|
|
|
heading: true
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
2015-11-04 09:01:30 +03:00
|
|
|
dataSource: utils.cloneObject(this.props.dataSource),
|
2015-11-04 12:16:31 +03:00
|
|
|
selected: false,
|
|
|
|
selected_rows: []
|
2015-11-03 09:23:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
componentWillReceiveProps = (next_props) => {
|
|
|
|
if (next_props.dataSource) {
|
2015-11-04 08:33:12 +03:00
|
|
|
this.setState({dataSource: utils.cloneObject(next_props.datasSource)});
|
2015-11-03 09:23:23 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
handleRowChange = (event, row, key, value) => {
|
2015-11-04 15:43:32 +03:00
|
|
|
const dataSource = this.state.dataSource;
|
2015-11-03 09:23:23 +03:00
|
|
|
dataSource[row.props.index][key] = value;
|
|
|
|
this.setState({ dataSource: dataSource });
|
|
|
|
if (this.props.onChange) {
|
|
|
|
this.props.onChange(event, this, row);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-07 05:01:07 +03:00
|
|
|
handleRowSelect = (event, instance) => {
|
2015-11-04 12:16:31 +03:00
|
|
|
if (this.props.onSelect) {
|
|
|
|
const index = instance.props.index;
|
2015-11-07 05:01:07 +03:00
|
|
|
const selected_rows = this.state.selected_rows;
|
|
|
|
const selected = selected_rows.indexOf(index) === -1;
|
2015-11-04 12:16:31 +03:00
|
|
|
if (selected) {
|
|
|
|
selected_rows.push(index);
|
|
|
|
this.props.onSelect(event, instance.props.data);
|
|
|
|
} else {
|
|
|
|
delete selected_rows[selected_rows.indexOf(index)];
|
|
|
|
}
|
|
|
|
this.setState({ selected_rows: selected_rows });
|
2015-11-03 09:23:23 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-07 05:01:07 +03:00
|
|
|
handleRowsSelect = (event) => {
|
|
|
|
this.setState({ selected: !this.state.selected });
|
2015-11-04 08:33:12 +03:00
|
|
|
};
|
|
|
|
|
2015-11-03 09:23:23 +03:00
|
|
|
isChanged = (data, base) => {
|
|
|
|
let changed = false;
|
2015-11-04 15:50:53 +03:00
|
|
|
Object.keys(data).map((key) => {
|
2015-11-03 09:23:23 +03:00
|
|
|
if (data[key] !== base[key]) {
|
|
|
|
changed = true;
|
|
|
|
}
|
2015-11-04 15:43:32 +03:00
|
|
|
});
|
2015-11-03 09:23:23 +03:00
|
|
|
return changed;
|
|
|
|
};
|
|
|
|
|
2015-11-04 08:33:12 +03:00
|
|
|
renderHead () {
|
|
|
|
if (this.props.heading) {
|
|
|
|
return (
|
|
|
|
<Head
|
|
|
|
model={this.props.model}
|
|
|
|
onSelect={this.props.onSelect ? this.handleRowsSelect : null}
|
2015-11-07 05:01:07 +03:00
|
|
|
selected={this.state.selected}
|
2015-11-04 08:33:12 +03:00
|
|
|
/>
|
2015-11-04 15:43:32 +03:00
|
|
|
);
|
2015-11-04 08:33:12 +03:00
|
|
|
}
|
2015-11-04 15:43:32 +03:00
|
|
|
}
|
2015-11-04 08:33:12 +03:00
|
|
|
|
|
|
|
renderBody () {
|
|
|
|
return (
|
|
|
|
<tbody>
|
|
|
|
{
|
|
|
|
this.state.dataSource.map((data, index) => {
|
|
|
|
return (
|
|
|
|
<Row
|
|
|
|
key={index}
|
|
|
|
index={index}
|
|
|
|
changed={this.isChanged(data, this.props.dataSource[index])}
|
|
|
|
data={data}
|
|
|
|
model={this.props.model}
|
|
|
|
onChange={this.props.onChange ? this.handleRowChange : null}
|
|
|
|
onSelect={this.props.onSelect ? this.handleRowSelect : null}
|
2015-11-04 15:43:32 +03:00
|
|
|
selected={this.state.selected || this.state.selected_rows.indexOf(index) !== -1}
|
2015-11-04 08:33:12 +03:00
|
|
|
/>
|
2015-11-04 15:43:32 +03:00
|
|
|
);
|
2015-11-04 08:33:12 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
</tbody>
|
2015-11-04 15:43:32 +03:00
|
|
|
);
|
2015-11-04 08:33:12 +03:00
|
|
|
}
|
2015-11-03 09:23:23 +03:00
|
|
|
|
2015-11-04 08:33:12 +03:00
|
|
|
render () {
|
|
|
|
const className = `${this.props.className} ${style.root}`;
|
2015-11-03 09:23:23 +03:00
|
|
|
return (
|
|
|
|
<table data-component-table className={className}>
|
2015-11-04 08:33:12 +03:00
|
|
|
{ this.renderHead() }
|
|
|
|
{ this.renderBody() }
|
2015-11-03 09:23:23 +03:00
|
|
|
</table>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue () {
|
|
|
|
return this.state.dataSource;
|
|
|
|
}
|
2015-11-04 12:16:31 +03:00
|
|
|
|
|
|
|
getSelected () {
|
2015-11-04 15:43:32 +03:00
|
|
|
const rows = [];
|
2015-11-04 12:16:31 +03:00
|
|
|
this.state.dataSource.map((row, index) => {
|
2015-11-04 15:43:32 +03:00
|
|
|
if (this.state.selected_rows.indexOf(index) !== -1) rows.push(row);
|
2015-11-04 12:16:31 +03:00
|
|
|
});
|
|
|
|
return rows;
|
|
|
|
}
|
2015-11-03 09:23:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Table;
|