-
Notifications
You must be signed in to change notification settings - Fork 763
Expand file tree
/
Copy pathTableHeader.js
More file actions
180 lines (165 loc) · 5.54 KB
/
TableHeader.js
File metadata and controls
180 lines (165 loc) · 5.54 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import AutoAffix from 'react-overlays/lib/AutoAffix';
import Const from './Const';
import classSet from 'classnames';
import SelectRowHeaderColumn from './SelectRowHeaderColumn';
class Checkbox extends Component {
componentDidMount() { this.update(this.props.checked); }
componentWillReceiveProps(props) { this.update(props.checked); }
update(checked) {
ReactDOM.findDOMNode(this).indeterminate = checked === 'indeterminate';
}
render() {
return (
<input className='react-bs-select-all'
type='checkbox'
checked={ this.props.checked }
onChange={ this.props.onChange } />
);
}
}
function getSortOrder(sortList, field, enableSort) {
if (!enableSort) return undefined;
const result = sortList.filter(sortObj => {
return sortObj.sortField === field;
});
if (result.length > 0) {
return result[0].order;
} else {
return undefined;
}
}
class TableHeader extends Component {
render() {
const containerClasses = classSet(
'react-bs-container-header',
'table-header-wrapper',
this.props.headerContainerClass);
const tableClasses = classSet('table', 'table-hover', {
'table-bordered': this.props.bordered,
'table-condensed': this.props.condensed
}, this.props.tableHeaderClass);
const rowCount = Math.max(...React.Children.map(this.props.children, elm =>
elm.props.row ? Number(elm.props.row) : 0
));
const rows = [];
let rowKey = 0;
rows[0] = [];
rows[0].push( [
this.props.expandColumnVisible &&
this.props.expandColumnBeforeSelectColumn &&
<th className='react-bs-table-expand-cell'> </th>
], [
this.renderSelectRowHeader(rowCount + 1, rowKey++)
], [
this.props.expandColumnVisible &&
!this.props.expandColumnBeforeSelectColumn &&
<th className='react-bs-table-expand-cell'> </th>
]);
const { sortIndicator, sortList, onSort, reset } = this.props;
React.Children.forEach(this.props.children, (elm) => {
const { dataField, dataSort } = elm.props;
const sort = getSortOrder(sortList, dataField, dataSort);
const rowIndex = elm.props.row ? Number(elm.props.row) : 0;
const rowSpan = elm.props.rowSpan ? Number(elm.props.rowSpan) : 1;
if (rows[rowIndex] === undefined) {
rows[rowIndex] = [];
}
if ((rowSpan + rowIndex) === (rowCount + 1)) {
rows[rowIndex].push(React.cloneElement(
elm, { reset, key: rowKey++, onSort, sort, sortIndicator, isOnlyHead: false }
));
} else {
rows[rowIndex].push(React.cloneElement(
elm, { key: rowKey++, isOnlyHead: true }
));
}
});
const trs = rows.map((row, indexRow)=>{
return (
<tr key={ indexRow }>
{ row }
</tr>
);
});
const tableHeaders = (
<table className={ tableClasses }>
{ React.cloneElement(this.props.colGroups, { ref: 'headerGrp' }) }
<thead ref='header'>
{ trs }
</thead>
</table>
);
if (this.props.stickyHeaders) {
return (
<AutoAffix affixStyle={ this.props.affixStyle } container={ () => {
return this.props.autoAffixContainer;
} }>
<div ref='container' className={ containerClasses } style={ this.props.style }>
{ tableHeaders }
</div>
</AutoAffix>
);
}
return (
<div ref='container' className={ containerClasses } style={ this.props.style }>
{ tableHeaders }
</div>
);
}
getHeaderColGrouop = () => {
return this.refs.headerGrp.childNodes;
}
renderSelectRowHeader(rowCount, rowKey) {
if (this.props.hideSelectColumn) {
return null;
} else if (this.props.customComponent) {
const CustomComponent = this.props.customComponent;
return (
<SelectRowHeaderColumn key={ rowKey } rowCount={ rowCount }>
<CustomComponent type='checkbox' checked={ this.props.isSelectAll }
indeterminate={ this.props.isSelectAll === 'indeterminate' } disabled={ false }
onChange={ this.props.onSelectAllRow } rowIndex='Header'/>
</SelectRowHeaderColumn>
);
} else if (this.props.rowSelectType === Const.ROW_SELECT_SINGLE) {
return (<SelectRowHeaderColumn key={ rowKey } rowCount={ rowCount }/>);
} else if (this.props.rowSelectType === Const.ROW_SELECT_MULTI) {
return (
<SelectRowHeaderColumn key={ rowKey } rowCount={ rowCount }>
<Checkbox
onChange={ this.props.onSelectAllRow }
checked={ this.props.isSelectAll }/>
</SelectRowHeaderColumn>
);
} else {
return null;
}
}
}
TableHeader.propTypes = {
headerContainerClass: PropTypes.string,
tableHeaderClass: PropTypes.string,
style: PropTypes.object,
rowSelectType: PropTypes.string,
onSort: PropTypes.func,
onSelectAllRow: PropTypes.func,
sortList: PropTypes.array,
hideSelectColumn: PropTypes.bool,
bordered: PropTypes.bool,
condensed: PropTypes.bool,
isFiltered: PropTypes.bool,
isSelectAll: PropTypes.oneOf([ true, 'indeterminate', false ]),
sortIndicator: PropTypes.bool,
customComponent: PropTypes.func,
colGroups: PropTypes.element,
reset: PropTypes.bool,
expandColumnVisible: PropTypes.bool,
expandColumnComponent: PropTypes.func,
expandColumnBeforeSelectColumn: PropTypes.bool,
autoAffixContainer: PropTypes.object,
stickyHeaders: PropTypes.bool,
affixStyle: PropTypes.object
};
export default TableHeader;