-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
List.js
206 lines (169 loc) · 5.98 KB
/
List.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
/** @flow */
import Grid from '../Grid'
import React, { Component, PropTypes } from 'react'
import cn from 'classnames'
import shallowCompare from 'react-addons-shallow-compare'
/**
* It is inefficient to create and manage a large list of DOM elements within a scrolling container
* if only a few of those elements are visible. The primary purpose of this component is to improve
* performance by only rendering the DOM nodes that a user is able to see based on their current
* scroll position.
*
* This component renders a virtualized list of elements with either fixed or dynamic heights.
*/
export default class List extends Component {
static propTypes = {
'aria-label': PropTypes.string,
/**
* Removes fixed height from the scrollingContainer so that the total height
* of rows can stretch the window. Intended for use with WindowScroller
*/
autoHeight: PropTypes.bool,
/** Optional CSS class name */
className: PropTypes.string,
/**
* Used to estimate the total height of a List before all of its rows have actually been measured.
* The estimated total height is adjusted as rows are rendered.
*/
estimatedRowSize: PropTypes.number.isRequired,
/** Height constraint for list (determines how many actual rows are rendered) */
height: PropTypes.number.isRequired,
/** Optional renderer to be used in place of rows when rowCount is 0 */
noRowsRenderer: PropTypes.func.isRequired,
/**
* Callback invoked with information about the slice of rows that were just rendered.
* ({ startIndex, stopIndex }): void
*/
onRowsRendered: 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.
*/
overscanRowCount: PropTypes.number.isRequired,
/**
* 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,
/**
* 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,
/** Responsible for rendering a row given an index; ({ index: number }): node */
rowRenderer: PropTypes.func.isRequired,
/** Number of rows in list. */
rowCount: PropTypes.number.isRequired,
/** See Grid#scrollToAlignment */
scrollToAlignment: PropTypes.oneOf(['auto', 'end', 'start', 'center']).isRequired,
/** Row index to ensure visible (by forcefully scrolling if necessary) */
scrollToIndex: PropTypes.number,
/** Vertical offset. */
scrollTop: PropTypes.number,
/** Optional inline style */
style: PropTypes.object,
/** Tab index for focus */
tabIndex: PropTypes.number,
/** Width of list */
width: PropTypes.number.isRequired
};
static defaultProps = {
estimatedRowSize: 30,
noRowsRenderer: () => null,
onRowsRendered: () => null,
onScroll: () => null,
overscanRowCount: 10,
scrollToAlignment: 'auto',
style: {}
};
constructor (props, context) {
super(props, context)
this._cellRenderer = this._cellRenderer.bind(this)
this._onScroll = this._onScroll.bind(this)
this._onSectionRendered = this._onSectionRendered.bind(this)
this._setRef = this._setRef.bind(this)
}
forceUpdateGrid () {
this.Grid.forceUpdate()
}
/** See Grid#measureAllCells */
measureAllRows () {
this.Grid.measureAllCells()
}
/** See Grid#recomputeGridSize */
recomputeRowHeights (index = 0) {
this.Grid.recomputeGridSize({
rowIndex: index
})
}
/** See Grid#scrollToCell */
scrollToRow (index = 0) {
this.Grid.scrollToCell({
columnIndex: 0,
rowIndex: index
})
}
render () {
const {
className,
noRowsRenderer,
scrollToIndex,
width
} = this.props
const classNames = cn('ReactVirtualized__List', className)
return (
<Grid
{...this.props}
autoContainerWidth
cellRenderer={this._cellRenderer}
className={classNames}
columnWidth={width}
columnCount={1}
noContentRenderer={noRowsRenderer}
onScroll={this._onScroll}
onSectionRendered={this._onSectionRendered}
ref={this._setRef}
scrollToRow={scrollToIndex}
/>
)
}
shouldComponentUpdate (nextProps, nextState) {
return shallowCompare(this, nextProps, nextState)
}
_cellRenderer ({ rowIndex, style, ...rest }) {
const { rowRenderer } = this.props
// TRICKY The style object is sometimes cached by Grid.
// This prevents new style objects from bypassing shallowCompare().
// However as of React 16, style props are auto-frozen (at least in dev mode)
// Check to make sure we can still modify the style before proceeding.
// https://github.com/facebook/react/commit/977357765b44af8ff0cfea327866861073095c12#commitcomment-20648713
const { writable } = Object.getOwnPropertyDescriptor(style, 'width')
if (writable) {
// By default, List cells should be 100% width.
// This prevents them from flowing under a scrollbar (if present).
style.width = '100%'
}
return rowRenderer({
index: rowIndex,
style,
...rest
})
}
_setRef (ref) {
this.Grid = ref
}
_onScroll ({ clientHeight, scrollHeight, scrollTop }) {
const { onScroll } = this.props
onScroll({ clientHeight, scrollHeight, scrollTop })
}
_onSectionRendered ({ rowOverscanStartIndex, rowOverscanStopIndex, rowStartIndex, rowStopIndex }) {
const { onRowsRendered } = this.props
onRowsRendered({
overscanStartIndex: rowOverscanStartIndex,
overscanStopIndex: rowOverscanStopIndex,
startIndex: rowStartIndex,
stopIndex: rowStopIndex
})
}
}