forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollection.js
238 lines (202 loc) · 6.11 KB
/
Collection.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
/** @flow */
import PropTypes from 'prop-types';
import * as React from 'react';
import CollectionView from './CollectionView';
import calculateSizeAndPositionData from './utils/calculateSizeAndPositionData';
import getUpdatedOffsetForIndex from '../utils/getUpdatedOffsetForIndex';
import type {ScrollPosition, SizeInfo} from './types';
/**
* Renders scattered or non-linear data.
* Unlike Grid, which renders checkerboard data, Collection can render arbitrarily positioned- even overlapping- data.
*/
export default class Collection extends React.PureComponent {
static propTypes = {
'aria-label': PropTypes.string,
/**
* Number of cells in Collection.
*/
cellCount: PropTypes.number.isRequired,
/**
* Responsible for rendering a group of cells given their indices.
* Should implement the following interface: ({
* cellSizeAndPositionGetter:Function,
* indices: Array<number>,
* cellRenderer: Function
* }): Array<PropTypes.node>
*/
cellGroupRenderer: PropTypes.func.isRequired,
/**
* Responsible for rendering a cell given an row and column index.
* Should implement the following interface: ({ index: number, key: string, style: object }): PropTypes.element
*/
cellRenderer: PropTypes.func.isRequired,
/**
* Callback responsible for returning size and offset/position information for a given cell (index).
* ({ index: number }): { height: number, width: number, x: number, y: number }
*/
cellSizeAndPositionGetter: PropTypes.func.isRequired,
/**
* Optionally override the size of the sections a Collection's cells are split into.
*/
sectionSize: PropTypes.number,
};
static defaultProps = {
'aria-label': 'grid',
cellGroupRenderer: defaultCellGroupRenderer,
};
constructor(props, context) {
super(props, context);
this._cellMetadata = [];
this._lastRenderedCellIndices = [];
// Cell cache during scroll (for performance)
this._cellCache = [];
this._isScrollingChange = this._isScrollingChange.bind(this);
this._setCollectionViewRef = this._setCollectionViewRef.bind(this);
}
forceUpdate() {
if (this._collectionView !== undefined) {
this._collectionView.forceUpdate();
}
}
/** See Collection#recomputeCellSizesAndPositions */
recomputeCellSizesAndPositions() {
this._cellCache = [];
this._collectionView.recomputeCellSizesAndPositions();
}
/** React lifecycle methods */
render() {
const {...props} = this.props;
return (
<CollectionView
cellLayoutManager={this}
isScrollingChange={this._isScrollingChange}
ref={this._setCollectionViewRef}
{...props}
/>
);
}
/** CellLayoutManager interface */
calculateSizeAndPositionData() {
const {cellCount, cellSizeAndPositionGetter, sectionSize} = this.props;
const data = calculateSizeAndPositionData({
cellCount,
cellSizeAndPositionGetter,
sectionSize,
});
this._cellMetadata = data.cellMetadata;
this._sectionManager = data.sectionManager;
this._height = data.height;
this._width = data.width;
}
/**
* Returns the most recently rendered set of cell indices.
*/
getLastRenderedIndices() {
return this._lastRenderedCellIndices;
}
/**
* Calculates the minimum amount of change from the current scroll position to ensure the specified cell is (fully) visible.
*/
getScrollPositionForCell({
align,
cellIndex,
height,
scrollLeft,
scrollTop,
width,
}): ScrollPosition {
const {cellCount} = this.props;
if (cellIndex >= 0 && cellIndex < cellCount) {
const cellMetadata = this._cellMetadata[cellIndex];
scrollLeft = getUpdatedOffsetForIndex({
align,
cellOffset: cellMetadata.x,
cellSize: cellMetadata.width,
containerSize: width,
currentOffset: scrollLeft,
targetIndex: cellIndex,
});
scrollTop = getUpdatedOffsetForIndex({
align,
cellOffset: cellMetadata.y,
cellSize: cellMetadata.height,
containerSize: height,
currentOffset: scrollTop,
targetIndex: cellIndex,
});
}
return {
scrollLeft,
scrollTop,
};
}
getTotalSize(): SizeInfo {
return {
height: this._height,
width: this._width,
};
}
cellRenderers({height, isScrolling, width, x, y}) {
const {cellGroupRenderer, cellRenderer} = this.props;
// Store for later calls to getLastRenderedIndices()
this._lastRenderedCellIndices = this._sectionManager.getCellIndices({
height,
width,
x,
y,
});
return cellGroupRenderer({
cellCache: this._cellCache,
cellRenderer,
cellSizeAndPositionGetter: ({index}) =>
this._sectionManager.getCellMetadata({index}),
indices: this._lastRenderedCellIndices,
isScrolling,
});
}
_isScrollingChange(isScrolling) {
if (!isScrolling) {
this._cellCache = [];
}
}
_setCollectionViewRef(ref) {
this._collectionView = ref;
}
}
function defaultCellGroupRenderer({
cellCache,
cellRenderer,
cellSizeAndPositionGetter,
indices,
isScrolling,
}) {
return indices
.map(index => {
const cellMetadata = cellSizeAndPositionGetter({index});
let cellRendererProps = {
index,
isScrolling,
key: index,
style: {
height: cellMetadata.height,
left: cellMetadata.x,
position: 'absolute',
top: cellMetadata.y,
width: cellMetadata.width,
},
};
// Avoid re-creating cells while scrolling.
// This can lead to the same cell being created many times and can cause performance issues for "heavy" cells.
// If a scroll is in progress- cache and reuse cells.
// This cache will be thrown away once scrolling complets.
if (isScrolling) {
if (!(index in cellCache)) {
cellCache[index] = cellRenderer(cellRendererProps);
}
return cellCache[index];
} else {
return cellRenderer(cellRendererProps);
}
})
.filter(renderedCell => !!renderedCell);
}