-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Table.js
327 lines (294 loc) · 8.64 KB
/
Table.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import warning from 'warning';
function getStyles(props, context) {
const {
baseTheme,
table,
} = context.muiTheme;
return {
root: {
backgroundColor: table.backgroundColor,
width: '100%',
borderCollapse: 'collapse',
borderSpacing: 0,
tableLayout: 'fixed',
fontFamily: baseTheme.fontFamily,
},
bodyTable: {
height: (props.fixedHeader || props.fixedFooter) ? props.height : 'auto',
overflowX: 'hidden',
overflowY: 'auto',
},
tableWrapper: {
height: (props.fixedHeader || props.fixedFooter) ? 'auto' : props.height,
overflow: 'auto',
},
};
}
class Table extends Component {
static propTypes = {
/**
* Set to true to indicate that all rows should be selected.
*/
allRowsSelected: PropTypes.bool,
/**
* Override the inline-styles of the body's table element.
*/
bodyStyle: PropTypes.object,
/**
* Children passed to table.
*/
children: PropTypes.node,
/**
* The css class name of the root element.
*/
className: PropTypes.string,
/**
* If true, the footer will appear fixed below the table.
* The default value is true.
*/
fixedFooter: PropTypes.bool,
/**
* If true, the header will appear fixed above the table.
* The default value is true.
*/
fixedHeader: PropTypes.bool,
/**
* Override the inline-styles of the footer's table element.
*/
footerStyle: PropTypes.object,
/**
* Override the inline-styles of the header's table element.
*/
headerStyle: PropTypes.object,
/**
* The height of the table.
*/
height: PropTypes.string,
/**
* If true, multiple table rows can be selected.
* CTRL/CMD+Click and SHIFT+Click are valid actions.
* The default value is false.
*/
multiSelectable: PropTypes.bool,
/**
* Called when a row cell is clicked.
* rowNumber is the row number and columnId is
* the column number or the column key.
*/
onCellClick: PropTypes.func,
/**
* Called when a table cell is hovered.
* rowNumber is the row number of the hovered row
* and columnId is the column number or the column key of the cell.
*/
onCellHover: PropTypes.func,
/**
* Called when a table cell is no longer hovered.
* rowNumber is the row number of the row and columnId
* is the column number or the column key of the cell.
*/
onCellHoverExit: PropTypes.func,
/**
* Called when a table row is hovered.
* rowNumber is the row number of the hovered row.
*/
onRowHover: PropTypes.func,
/**
* Called when a table row is no longer hovered.
* rowNumber is the row number of the row that is no longer hovered.
*/
onRowHoverExit: PropTypes.func,
/**
* Called when a row is selected.
* selectedRows is an array of all row selections.
* IF all rows have been selected, the string "all"
* will be returned instead to indicate that all rows have been selected.
*/
onRowSelection: PropTypes.func,
/**
* If true, table rows can be selected.
* If multiple row selection is desired, enable multiSelectable.
* The default value is true.
*/
selectable: PropTypes.bool,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* Override the inline-styles of the table's wrapper element.
*/
wrapperStyle: PropTypes.object,
};
static defaultProps = {
allRowsSelected: false,
fixedFooter: true,
fixedHeader: true,
height: 'inherit',
multiSelectable: false,
selectable: true,
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
allRowsSelected: false,
};
componentWillMount() {
if (this.props.allRowsSelected) {
this.setState({allRowsSelected: true});
}
}
componentWillReceiveProps(nextProps) {
if (this.props.allRowsSelected !== nextProps.allRowsSelected) {
this.setState({allRowsSelected: nextProps.allRowsSelected});
}
}
isScrollbarVisible() {
const tableDivHeight = this.refs.tableDiv.clientHeight;
const tableBodyHeight = this.refs.tableBody.clientHeight;
return tableBodyHeight > tableDivHeight;
}
createTableHeader(base) {
return React.cloneElement(
base,
{
enableSelectAll: base.props.enableSelectAll && this.props.selectable && this.props.multiSelectable,
onSelectAll: this.onSelectAll,
selectAllSelected: this.state.allRowsSelected,
}
);
}
createTableBody(base) {
return React.cloneElement(
base,
{
allRowsSelected: this.state.allRowsSelected,
multiSelectable: this.props.multiSelectable,
onCellClick: this.onCellClick,
onCellHover: this.onCellHover,
onCellHoverExit: this.onCellHoverExit,
onRowHover: this.onRowHover,
onRowHoverExit: this.onRowHoverExit,
onRowSelection: this.onRowSelection,
selectable: this.props.selectable,
}
);
}
createTableFooter(base) {
return base;
}
onCellClick = (rowNumber, columnNumber, event) => {
if (this.props.onCellClick) this.props.onCellClick(rowNumber, columnNumber, event);
};
onCellHover = (rowNumber, columnNumber, event) => {
if (this.props.onCellHover) this.props.onCellHover(rowNumber, columnNumber, event);
};
onCellHoverExit = (rowNumber, columnNumber, event) => {
if (this.props.onCellHoverExit) this.props.onCellHoverExit(rowNumber, columnNumber, event);
};
onRowHover = (rowNumber) => {
if (this.props.onRowHover) this.props.onRowHover(rowNumber);
};
onRowHoverExit = (rowNumber) => {
if (this.props.onRowHoverExit) this.props.onRowHoverExit(rowNumber);
};
onRowSelection = (selectedRows) => {
if (this.state.allRowsSelected) {
this.setState({allRowsSelected: false});
}
if (this.props.onRowSelection) {
this.props.onRowSelection(selectedRows);
}
};
onSelectAll = () => {
if (this.props.onRowSelection) {
if (!this.state.allRowsSelected) {
this.props.onRowSelection('all');
} else {
this.props.onRowSelection('none');
}
}
this.setState({allRowsSelected: !this.state.allRowsSelected});
};
render() {
const {
children,
className,
fixedFooter,
fixedHeader,
style,
wrapperStyle,
headerStyle,
bodyStyle,
footerStyle,
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
let tHead;
let tFoot;
let tBody;
React.Children.forEach(children, (child) => {
if (!React.isValidElement(child)) return;
const {muiName} = child.type;
if (muiName === 'TableBody') {
tBody = this.createTableBody(child);
} else if (muiName === 'TableHeader') {
tHead = this.createTableHeader(child);
} else if (muiName === 'TableFooter') {
tFoot = this.createTableFooter(child);
} else {
warning(false,
`Material-UI: Children of the Table component must be TableBody or TableHeader or TableFooter.
Nothing is rendered.`);
}
});
// If we could not find a table-header and a table-body, do not attempt to display anything.
if (!tBody && !tHead) return null;
const mergedTableStyle = Object.assign(styles.root, style);
let headerTable;
let footerTable;
let inlineHeader;
let inlineFooter;
if (fixedHeader) {
headerTable = (
<div style={prepareStyles(Object.assign({}, headerStyle))}>
<table className={className} style={mergedTableStyle}>
{tHead}
</table>
</div>
);
} else {
inlineHeader = tHead;
}
if (tFoot !== undefined) {
if (fixedFooter) {
footerTable = (
<div style={prepareStyles(Object.assign({}, footerStyle))}>
<table className={className} style={prepareStyles(mergedTableStyle)}>
{tFoot}
</table>
</div>
);
} else {
inlineFooter = tFoot;
}
}
return (
<div style={prepareStyles(Object.assign(styles.tableWrapper, wrapperStyle))}>
{headerTable}
<div style={prepareStyles(Object.assign(styles.bodyTable, bodyStyle))} ref="tableDiv">
<table className={className} style={mergedTableStyle} ref="tableBody">
{inlineHeader}
{inlineFooter}
{tBody}
</table>
</div>
{footerTable}
</div>
);
}
}
export default Table;