forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlexTable.js
More file actions
421 lines (354 loc) · 11.3 KB
/
FlexTable.js
File metadata and controls
421 lines (354 loc) · 11.3 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/** @flow */
import cn from 'classnames'
import FlexColumn from './FlexColumn'
import React, { Component, PropTypes } from 'react'
import { findDOMNode } from 'react-dom'
import shallowCompare from 'react-addons-shallow-compare'
import Grid from '../Grid'
import SortDirection from './SortDirection'
/**
* Table component with fixed headers and virtualized rows for improved performance with large data sets.
* This component expects explicit width, height, and padding parameters.
*/
export default class FlexTable extends Component {
static propTypes = {
'aria-label': PropTypes.string,
/** One or more FlexColumns describing the data displayed in this row */
children: (props, propName, componentName) => {
const children = React.Children.toArray(props.children)
for (let i = 0; i < children.length; i++) {
if (children[i].type !== FlexColumn) {
return new Error(`FlexTable only accepts children of type FlexColumn`)
}
}
},
/** Optional CSS class name */
className: PropTypes.string,
/** Disable rendering the header at all */
disableHeader: PropTypes.bool,
/** Optional CSS class to apply to all column headers */
headerClassName: PropTypes.string,
/** Fixed height of header row */
headerHeight: PropTypes.number.isRequired,
/** Fixed/available height for out DOM element */
height: PropTypes.number.isRequired,
/** Optional renderer to be used in place of table body rows when rowsCount is 0 */
noRowsRenderer: PropTypes.func,
/**
* Optional callback when a column's header is clicked.
* (dataKey: string): void
*/
onHeaderClick: PropTypes.func,
/**
* Callback invoked when a user clicks on a table row.
* (rowIndex: number): void
*/
onRowClick: PropTypes.func,
/**
* Callback invoked with information about the slice of rows that were just rendered.
* ({ startIndex, stopIndex }): void
*/
onRowsRendered: PropTypes.func,
/**
* Callback invoked whenever the scroll offset changes within the inner scrollable region.
* This callback can be used to sync scrolling between lists, tables, or grids.
* ({ clientHeight, scrollHeight, scrollTop }): void
*/
onScroll: PropTypes.func.isRequired,
/**
* Number of rows to render above/below the visible bounds of the list.
* These rows can help for smoother scrolling on touch devices.
*/
overscanRowsCount: PropTypes.number.isRequired,
/**
* Optional CSS class to apply to all table rows (including the header row).
* This property can be a CSS class name (string) or a function that returns a class name.
* If a function is provided its signature should be: (rowIndex: number): string
*/
rowClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/**
* Callback responsible for returning a data row given an index.
* (index: number): any
*/
rowGetter: PropTypes.func.isRequired,
/**
* Either a fixed row height (number) or a function that returns the height of a row given its index.
* (index: number): number
*/
rowHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]).isRequired,
/** Number of rows in table. */
rowsCount: PropTypes.number.isRequired,
/** Row index to ensure visible (by forcefully scrolling if necessary) */
scrollToIndex: PropTypes.number,
/** Vertical offset. */
scrollTop: PropTypes.number,
/**
* Sort function to be called if a sortable header is clicked.
* (dataKey: string, sortDirection: SortDirection): void
*/
sort: PropTypes.func,
/** FlexTable data is currently sorted by this :dataKey (if it is sorted at all) */
sortBy: PropTypes.string,
/** FlexTable data is currently sorted in this direction (if it is sorted at all) */
sortDirection: PropTypes.oneOf([SortDirection.ASC, SortDirection.DESC]),
/** Width of list */
width: PropTypes.number.isRequired
}
static defaultProps = {
disableHeader: false,
headerHeight: 0,
noRowsRenderer: () => null,
onRowsRendered: () => null,
onScroll: () => null,
overscanRowsCount: 10
}
constructor (props) {
super(props)
this.state = {
scrollbarWidth: 0
}
this._createRow = this._createRow.bind(this)
}
/**
* See Grid#recomputeGridSize
*/
recomputeRowHeights () {
this.refs.Grid.recomputeGridSize()
}
componentDidMount () {
this._setScrollbarWidth()
}
componentDidUpdate () {
this._setScrollbarWidth()
}
render () {
const {
className,
disableHeader,
headerHeight,
height,
noRowsRenderer,
onRowsRendered,
onScroll,
overscanRowsCount,
rowClassName,
rowHeight,
rowsCount,
scrollToIndex,
scrollTop,
width
} = this.props
const { scrollbarWidth } = this.state
const availableRowsHeight = height - headerHeight
// This row-renderer wrapper function is necessary in order to trigger re-render when the
// sort-by or sort-direction have changed (else Grid will not see any props changes)
const rowRenderer = index => {
return this._createRow(index)
}
const rowClass = rowClassName instanceof Function ? rowClassName(-1) : rowClassName
return (
<div
className={cn('FlexTable', className)}
>
{!disableHeader && (
<div
className={cn('FlexTable__headerRow', rowClass)}
style={{
height: headerHeight,
paddingRight: scrollbarWidth,
width: width
}}
>
{this._getRenderedHeaderRow()}
</div>
)}
<Grid
aria-label={this.props['aria-label']}
ref='Grid'
className={'FlexTable__Grid'}
columnWidth={width}
columnsCount={1}
height={availableRowsHeight}
noContentRenderer={noRowsRenderer}
onScroll={({ clientHeight, scrollHeight, scrollTop }) => onScroll({ clientHeight, scrollHeight, scrollTop })}
onSectionRendered={({ rowOverscanStartIndex, rowOverscanStopIndex, rowStartIndex, rowStopIndex }) => onRowsRendered({
overscanStartIndex: rowOverscanStartIndex,
overscanStopIndex: rowOverscanStopIndex,
startIndex: rowStartIndex,
stopIndex: rowStopIndex
})}
overscanRowsCount={overscanRowsCount}
renderCell={({ columnIndex, rowIndex }) => rowRenderer(rowIndex)}
rowHeight={rowHeight}
rowsCount={rowsCount}
scrollToRow={scrollToIndex}
scrollTop={scrollTop}
width={width}
/>
</div>
)
}
shouldComponentUpdate (nextProps, nextState) {
return shallowCompare(this, nextProps, nextState)
}
_createColumn (column, columnIndex, rowData, rowIndex) {
const {
cellClassName,
cellDataGetter,
columnData,
dataKey,
cellRenderer
} = column.props
const cellData = cellDataGetter(dataKey, rowData, columnData)
const renderedCell = cellRenderer(cellData, dataKey, rowData, rowIndex, columnData)
const style = this._getFlexStyleForColumn(column)
const title = typeof renderedCell === 'string'
? renderedCell
: null
return (
<div
key={`Row${rowIndex}-Col${columnIndex}`}
className={cn('FlexTable__rowColumn', cellClassName)}
style={style}
>
<div
className='FlexTable__truncatedColumnText'
title={title}
>
{renderedCell}
</div>
</div>
)
}
_createHeader (column, columnIndex) {
const { headerClassName, onHeaderClick, sort, sortBy, sortDirection } = this.props
const { dataKey, disableSort, headerRenderer, label, columnData } = column.props
const sortEnabled = !disableSort && sort
const classNames = cn(
'FlexTable__headerColumn',
headerClassName,
column.props.headerClassName,
{
'FlexTable__sortableHeaderColumn': sortEnabled
}
)
const style = this._getFlexStyleForColumn(column)
const renderedHeader = headerRenderer({
columnData,
dataKey,
disableSort,
label,
sortBy,
sortDirection
})
const a11yProps = {}
if (sortEnabled || onHeaderClick) {
// If this is a sortable header, clicking it should update the table data's sorting.
const newSortDirection = sortBy !== dataKey || sortDirection === SortDirection.DESC
? SortDirection.ASC
: SortDirection.DESC
const onClick = () => {
sortEnabled && sort(dataKey, newSortDirection)
onHeaderClick && onHeaderClick(dataKey, columnData)
}
const onKeyDown = (event) => {
if (event.key === 'Enter' || event.key === ' ') {
onClick()
}
}
a11yProps['aria-label'] = column.props['aria-label'] || label || dataKey
a11yProps.role = 'rowheader'
a11yProps.tabIndex = 0
a11yProps.onClick = onClick
a11yProps.onKeyDown = onKeyDown
}
return (
<div
{...a11yProps}
key={`Header-Col${columnIndex}`}
className={classNames}
style={style}
>
{renderedHeader}
</div>
)
}
_createRow (rowIndex) {
const {
children,
onRowClick,
rowClassName,
rowGetter
} = this.props
const { scrollbarWidth } = this.state
const rowClass = rowClassName instanceof Function ? rowClassName(rowIndex) : rowClassName
const rowData = rowGetter(rowIndex)
const renderedRow = React.Children.map(
children,
(column, columnIndex) => this._createColumn(
column,
columnIndex,
rowData,
rowIndex
)
)
const a11yProps = {}
if (onRowClick) {
a11yProps['aria-label'] = 'row'
a11yProps.role = 'row'
a11yProps.tabIndex = 0
a11yProps.onClick = () => onRowClick(rowIndex)
}
return (
<div
{...a11yProps}
key={rowIndex}
className={cn('FlexTable__row', rowClass)}
style={{
height: this._getRowHeight(rowIndex),
paddingRight: scrollbarWidth
}}
>
{renderedRow}
</div>
)
}
/**
* Determines the flex-shrink, flex-grow, and width values for a cell (header or column).
*/
_getFlexStyleForColumn (column) {
const flexValue = `${column.props.flexGrow} ${column.props.flexShrink} ${column.props.width}px`
const style = {
flex: flexValue,
msFlex: flexValue,
WebkitFlex: flexValue
}
if (column.props.maxWidth) {
style.maxWidth = column.props.maxWidth
}
if (column.props.minWidth) {
style.minWidth = column.props.minWidth
}
return style
}
_getRenderedHeaderRow () {
const { children, disableHeader } = this.props
const items = disableHeader ? [] : children
return React.Children.map(items, (column, index) =>
this._createHeader(column, index)
)
}
_getRowHeight (rowIndex) {
const { rowHeight } = this.props
return rowHeight instanceof Function
? rowHeight(rowIndex)
: rowHeight
}
_setScrollbarWidth () {
const Grid = findDOMNode(this.refs.Grid)
const clientWidth = Grid.clientWidth || 0
const offsetWidth = Grid.offsetWidth || 0
const scrollbarWidth = offsetWidth - clientWidth
this.setState({ scrollbarWidth })
}
}