forked from yuristrelets/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.js
More file actions
103 lines (93 loc) · 2.7 KB
/
Table.js
File metadata and controls
103 lines (93 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React from 'react';
import TableHead from './TableHead';
import TableRow from './TableRow';
import style from './style';
class Table extends React.Component {
static propTypes = {
className: React.PropTypes.string,
heading: React.PropTypes.bool,
model: React.PropTypes.object,
multiSelectable: React.PropTypes.bool,
onChange: React.PropTypes.func,
onSelect: React.PropTypes.func,
selectable: React.PropTypes.bool,
selected: React.PropTypes.array,
source: React.PropTypes.array
};
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];
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);
}
};
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}
/>
);
}
}
renderBody () {
const rows = this.props.source.map((data, index) => {
return (
<TableRow
data={data}
index={index}
key={index}
model={this.props.model}
onChange={this.props.onChange ? this.handleRowChange.bind(this) : undefined}
onSelect={this.handleRowSelect.bind(this, index)}
selectable={this.props.selectable}
selected={this.props.selected.indexOf(index) !== -1}
/>
);
});
return <tbody>{rows}</tbody>;
}
render () {
let className = style.root;
if (this.props.className) className += ` ${this.props.className}`;
return (
<table data-react-toolbox='table' className={className}>
{this.renderHead()}
{this.renderBody()}
</table>
);
}
}
export default Table;