-
Notifications
You must be signed in to change notification settings - Fork 934
/
TableBody.js
338 lines (300 loc) · 12.2 KB
/
TableBody.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
328
329
330
331
332
333
334
335
336
337
338
import React from 'react';
import PropTypes from 'prop-types';
import Typography from '@mui/material/Typography';
import MuiTableBody from '@mui/material/TableBody';
import TableBodyCell from './TableBodyCell';
import TableBodyRow from './TableBodyRow';
import TableSelectCell from './TableSelectCell';
import { withStyles } from 'tss-react/mui';
import cloneDeep from 'lodash.clonedeep';
import { getPageValue } from '../utils';
import clsx from 'clsx';
const defaultBodyStyles = theme => ({
root: {},
emptyTitle: {
textAlign: 'center',
},
lastStackedCell: {
[theme.breakpoints.down('md')]: {
'& td:last-child': {
borderBottom: 'none',
},
},
},
lastSimpleCell: {
[theme.breakpoints.down('sm')]: {
'& td:last-child': {
borderBottom: 'none',
},
},
},
});
class TableBody extends React.Component {
static propTypes = {
/** Data used to describe table */
data: PropTypes.array.isRequired,
/** Total number of data rows */
count: PropTypes.number.isRequired,
/** Columns used to describe table */
columns: PropTypes.array.isRequired,
/** Options used to describe table */
options: PropTypes.object.isRequired,
/** Data used to filter table against */
filterList: PropTypes.array,
/** Callback to execute when row is clicked */
onRowClick: PropTypes.func,
/** Table rows expanded */
expandedRows: PropTypes.object,
/** Table rows selected */
selectedRows: PropTypes.object,
/** Callback to trigger table row select */
selectRowUpdate: PropTypes.func,
/** The most recent row to have been selected/unselected */
previousSelectedRow: PropTypes.object,
/** Data used to search table against */
searchText: PropTypes.string,
/** Toggle row expandable */
toggleExpandRow: PropTypes.func,
/** Extend the style applied to components */
classes: PropTypes.object,
};
static defaultProps = {
toggleExpandRow: () => {},
};
buildRows() {
const { data, page, rowsPerPage, count } = this.props;
if (this.props.options.serverSide) return data.length ? data : null;
let rows = [];
const highestPageInRange = getPageValue(count, rowsPerPage, page);
const fromIndex = highestPageInRange === 0 ? 0 : highestPageInRange * rowsPerPage;
const toIndex = Math.min(count, (highestPageInRange + 1) * rowsPerPage);
if (page > highestPageInRange) {
console.warn('Current page is out of range, using the highest page that is in range instead.');
}
for (let rowIndex = fromIndex; rowIndex < count && rowIndex < toIndex; rowIndex++) {
if (data[rowIndex] !== undefined) rows.push(data[rowIndex]);
}
return rows.length ? rows : null;
}
getRowIndex(index) {
const { page, rowsPerPage, options } = this.props;
if (options.serverSide) {
return index;
}
const startIndex = page === 0 ? 0 : page * rowsPerPage;
return startIndex + index;
}
isRowSelected(dataIndex) {
const { selectedRows } = this.props;
return selectedRows.lookup && selectedRows.lookup[dataIndex] ? true : false;
}
isRowExpanded(dataIndex) {
const { expandedRows } = this.props;
return expandedRows.lookup && expandedRows.lookup[dataIndex] ? true : false;
}
isRowSelectable(dataIndex, selectedRows) {
const { options } = this.props;
selectedRows = selectedRows || this.props.selectedRows;
if (options.isRowSelectable) {
return options.isRowSelectable(dataIndex, selectedRows);
} else {
return true;
}
}
isRowExpandable(dataIndex) {
const { options, expandedRows } = this.props;
if (options.isRowExpandable) {
return options.isRowExpandable(dataIndex, expandedRows);
} else {
return true;
}
}
handleRowSelect = (data, event) => {
let shiftKey = event && event.nativeEvent ? event.nativeEvent.shiftKey : false;
let shiftAdjacentRows = [];
let previousSelectedRow = this.props.previousSelectedRow;
// If the user is pressing shift and has previously clicked another row.
if (shiftKey && previousSelectedRow && previousSelectedRow.index < this.props.data.length) {
let curIndex = previousSelectedRow.index;
// Create a copy of the selectedRows object. This will be used and modified
// below when we see if we can add adjacent rows.
let selectedRows = cloneDeep(this.props.selectedRows);
// Add the clicked on row to our copy of selectedRows (if it isn't already present).
let clickedDataIndex = this.props.data[data.index].dataIndex;
if (selectedRows.data.filter(d => d.dataIndex === clickedDataIndex).length === 0) {
selectedRows.data.push({
index: data.index,
dataIndex: clickedDataIndex,
});
selectedRows.lookup[clickedDataIndex] = true;
}
while (curIndex !== data.index) {
let dataIndex = this.props.data[curIndex].dataIndex;
if (this.isRowSelectable(dataIndex, selectedRows)) {
let lookup = {
index: curIndex,
dataIndex: dataIndex,
};
// Add adjacent row to temp selectedRow object if it isn't present.
if (selectedRows.data.filter(d => d.dataIndex === dataIndex).length === 0) {
selectedRows.data.push(lookup);
selectedRows.lookup[dataIndex] = true;
}
shiftAdjacentRows.push(lookup);
}
curIndex = data.index > curIndex ? curIndex + 1 : curIndex - 1;
}
}
this.props.selectRowUpdate('cell', data, shiftAdjacentRows);
};
handleRowClick = (row, data, event) => {
// Don't trigger onRowClick if the event was actually the expandable icon.
if (
event.target.id === 'expandable-button' ||
(event.target.nodeName === 'path' && event.target.parentNode.id === 'expandable-button')
) {
return;
}
// Don't trigger onRowClick if the event was actually a row selection via checkbox
if (event.target.id && event.target.id.startsWith('MUIDataTableSelectCell')) return;
// Check if we should toggle row select when row is clicked anywhere
if (
this.props.options.selectableRowsOnClick &&
this.props.options.selectableRows !== 'none' &&
this.isRowSelectable(data.dataIndex, this.props.selectedRows)
) {
const selectRow = { index: data.rowIndex, dataIndex: data.dataIndex };
this.handleRowSelect(selectRow, event);
}
// Check if we should trigger row expand when row is clicked anywhere
if (
this.props.options.expandableRowsOnClick &&
this.props.options.expandableRows &&
this.isRowExpandable(data.dataIndex, this.props.expandedRows)
) {
const expandRow = { index: data.rowIndex, dataIndex: data.dataIndex };
this.props.toggleExpandRow(expandRow);
}
// Don't trigger onRowClick if the event was actually a row selection via click
if (this.props.options.selectableRowsOnClick) return;
this.props.options.onRowClick && this.props.options.onRowClick(row, data, event);
};
processRow = (row, columnOrder) => {
let ret = [];
for (let ii = 0; ii < row.length; ii++) {
ret.push({
value: row[columnOrder[ii]],
index: columnOrder[ii],
});
}
return ret;
};
render() {
const {
classes,
columns,
toggleExpandRow,
options,
columnOrder = this.props.columns.map((item, idx) => idx),
components = {},
tableId,
} = this.props;
const tableRows = this.buildRows();
const visibleColCnt = columns.filter(c => c.display === 'true').length;
return (
<MuiTableBody>
{tableRows && tableRows.length > 0 ? (
tableRows.map((data, rowIndex) => {
const { data: row, dataIndex } = data;
if (options.customRowRender) {
return options.customRowRender(row, dataIndex, rowIndex);
}
let isRowSelected = options.selectableRows !== 'none' ? this.isRowSelected(dataIndex) : false;
let isRowSelectable = this.isRowSelectable(dataIndex);
let bodyClasses = options.setRowProps ? options.setRowProps(row, dataIndex, rowIndex) || {} : {};
const processedRow = this.processRow(row, columnOrder);
return (
<React.Fragment key={rowIndex}>
<TableBodyRow
{...bodyClasses}
options={options}
rowSelected={isRowSelected}
isRowSelectable={isRowSelectable}
onClick={this.handleRowClick.bind(null, row, { rowIndex, dataIndex })}
className={clsx({
[classes.lastStackedCell]:
options.responsive === 'vertical' ||
options.responsive === 'stacked' ||
options.responsive === 'stackedFullWidth',
[classes.lastSimpleCell]: options.responsive === 'simple',
[bodyClasses.className]: bodyClasses.className,
})}
data-testid={'MUIDataTableBodyRow-' + dataIndex}
id={`MUIDataTableBodyRow-${tableId}-${dataIndex}`}>
<TableSelectCell
onChange={this.handleRowSelect.bind(null, {
index: this.getRowIndex(rowIndex),
dataIndex: dataIndex,
})}
onExpand={toggleExpandRow.bind(null, {
index: this.getRowIndex(rowIndex),
dataIndex: dataIndex,
})}
fixedHeader={options.fixedHeader}
fixedSelectColumn={options.fixedSelectColumn}
checked={isRowSelected}
expandableOn={options.expandableRows}
// When rows are expandable, but this particular row isn't expandable, set this to true.
// This will add a new class to the toggle button, MUIDataTableSelectCell-expandDisabled.
hideExpandButton={!this.isRowExpandable(dataIndex) && options.expandableRows}
selectableOn={options.selectableRows}
selectableRowsHideCheckboxes={options.selectableRowsHideCheckboxes}
isRowExpanded={this.isRowExpanded(dataIndex)}
isRowSelectable={isRowSelectable}
dataIndex={dataIndex}
id={`MUIDataTableSelectCell-${tableId}-${dataIndex}`}
components={components}
/>
{processedRow.map(
column =>
columns[column.index].display === 'true' && (
<TableBodyCell
{...(columns[column.index].setCellProps
? columns[column.index].setCellProps(column.value, dataIndex, column.index) || {}
: {})}
data-testid={`MuiDataTableBodyCell-${column.index}-${rowIndex}`}
dataIndex={dataIndex}
rowIndex={rowIndex}
colIndex={column.index}
columnHeader={columns[column.index].label}
print={columns[column.index].print}
options={options}
tableId={tableId}
key={column.index}>
{column.value}
</TableBodyCell>
),
)}
</TableBodyRow>
{this.isRowExpanded(dataIndex) && options.renderExpandableRow(row, { rowIndex, dataIndex })}
</React.Fragment>
);
})
) : (
<TableBodyRow options={options}>
<TableBodyCell
colSpan={options.selectableRows !== 'none' || options.expandableRows ? visibleColCnt + 1 : visibleColCnt}
options={options}
colIndex={0}
rowIndex={0}>
<Typography variant="body1" className={classes.emptyTitle} component={'div'}>
{options.textLabels.body.noMatch}
</Typography>
</TableBodyCell>
</TableBodyRow>
)}
</MuiTableBody>
);
}
}
export default withStyles(TableBody, defaultBodyStyles, { name: 'MUIDataTableBody' });