react-toolbox/components/table/Table.js

110 lines
2.9 KiB
JavaScript
Raw Normal View History

import React from 'react';
2016-05-26 21:37:57 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
import TableHead from './TableHead';
import TableRow from './TableRow';
class Table extends React.Component {
static propTypes = {
className: React.PropTypes.string,
heading: React.PropTypes.bool,
model: React.PropTypes.object,
2016-05-10 22:33:34 +03:00
multiSelectable: React.PropTypes.bool,
onChange: React.PropTypes.func,
onSelect: React.PropTypes.func,
selectable: React.PropTypes.bool,
selected: React.PropTypes.array,
2016-05-26 21:37:57 +03:00
source: React.PropTypes.array,
theme: React.PropTypes.shape({
table: React.PropTypes.string
})
};
static defaultProps = {
className: '',
heading: true,
selectable: true,
multiSelectable: true,
selected: [],
source: []
};
handleFullSelect = () => {
if (this.props.onSelect) {
const {source, selected} = this.props;
const newSelected = source.length === selected.length ? [] : source.map((i, idx) => idx);
this.props.onSelect(newSelected);
}
};
handleRowSelect = (index) => {
if (this.props.onSelect) {
const position = this.props.selected.indexOf(index);
let newSelected = [...this.props.selected];
2016-05-10 22:33:34 +03:00
if (position !== -1) { newSelected.splice(position, 1); }
if (position !== -1 && this.props.multiSelectable) {
newSelected.push(index);
} else {
newSelected = [index];
}
this.props.onSelect(newSelected);
}
};
handleRowChange = (index, key, value) => {
if (this.props.onChange) {
this.props.onChange(index, key, value);
}
};
2016-05-10 22:33:34 +03:00
renderHead () {
if (this.props.heading) {
const {model, selected, source, selectable, multiSelectable} = this.props;
const isSelected = selected.length === source.length;
return (
<TableHead
model={model}
onSelect={this.handleFullSelect}
selectable={selectable}
multiSelectable={multiSelectable}
selected={isSelected}
2016-05-26 21:37:57 +03:00
theme={this.props.theme}
/>
);
}
}
2016-05-10 22:33:34 +03:00
renderBody () {
2016-05-26 21:37:57 +03:00
const { source, model, onChange, selectable, selected, theme } = this.props;
return (
<tbody>
{source.map((data, index) => (
<TableRow
data={data}
index={index}
key={index}
model={model}
onChange={onChange ? this.handleRowChange.bind(this) : undefined}
onSelect={this.handleRowSelect.bind(this, index)}
selectable={selectable}
selected={selected.indexOf(index) !== -1}
theme={theme}
/>
))}
</tbody>
);
}
2016-05-10 22:33:34 +03:00
render () {
2016-05-26 21:37:57 +03:00
const { className, theme } = this.props;
return (
2016-05-26 21:37:57 +03:00
<table data-react-toolbox='table' className={classnames(theme.table, className)}>
{this.renderHead()}
{this.renderBody()}
</table>
);
}
}
2016-05-26 21:37:57 +03:00
export default themr('ToolboxTable')(Table);