-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathVirtualScrollList.js
239 lines (222 loc) · 7.77 KB
/
VirtualScrollList.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
/**
* A simple React list with virtual scrolling based on dynamic-virtual-scroll driver
* USUALLY sufficient for everything including grids (using absolute sizing of cells).
* Just because browsers can't do virtualized grid or table layouts efficiently.
*/
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { virtualScrollDriver } from 'dynamic-virtual-scroll';
export class VirtualScrollList extends React.Component
{
static propTypes = {
className: PropTypes.string,
style: PropTypes.object,
totalItems: PropTypes.number.isRequired,
minRowHeight: PropTypes.number.isRequired,
viewportHeight: PropTypes.number,
header: PropTypes.any,
headerHeight: PropTypes.number,
renderItem: PropTypes.func.isRequired,
}
state = {
targetHeight: 0,
topPlaceholderHeight: 0,
firstMiddleItem: 0,
middleItemCount: 0,
middlePlaceholderHeight: 0,
lastItemCount: 0,
scrollTo: 0,
scrollTimes: 0,
}
setItemRef = []
itemRefs = []
itemRefCount = []
makeRef(i)
{
this.setItemRef[i] = (e) =>
{
// If the new row instance is mounted before unmouting the old one,
// we get called 2 times in wrong order: first with the new instance,
// then with null telling us that the old one is unmounted.
// We track reference count to workaround it.
this.itemRefCount[i] = (this.itemRefCount[i]||0) + (e ? 1 : -1);
if (e || !this.itemRefCount[i])
{
this.itemRefs[i] = e;
}
};
}
renderItems(start, count, is_end)
{
let r = [];
for (let i = 0; i < count; i++)
{
let item = this.props.renderItem(i+start);
if (item)
{
if (!this.setItemRef[i+start])
{
this.makeRef(i+start);
}
r.push(<item.type {...item.props} key={i+start} ref={this.setItemRef[i+start]} />);
}
}
return r;
}
render()
{
if (this.state.totalItems && this.props.totalItems != this.state.totalItems &&
this.state.scrollTimes <= 0 && this.viewport && this.viewport.offsetParent)
{
// Automatically preserve scroll position when item count changes...
// But only when the list is on-screen! We'll end up with an infinite update loop if it's off-screen.
this.state.scrollTo = this.getItemScrollPos();
this.state.scrollTimes = 2;
}
const props = { ...this.props };
for (const k in VirtualScrollList.propTypes)
{
delete props[k];
}
return (<div
{...props}
className={this.props.className}
style={{
position: 'relative',
...(this.props.style||{}),
overflowAnchor: 'none',
}}
ref={this.setViewport}
onScroll={this.onScroll}>
{this.props.header}
{this.state.targetHeight > 0
? <div key="target" style={{position: 'absolute', top: 0, left: '-5px', width: '1px', height: this.state.targetHeight+'px'}}></div>
: null}
{this.state.topPlaceholderHeight
? <div style={{height: this.state.topPlaceholderHeight+'px'}} key="top"></div>
: null}
{this.renderItems(this.state.firstMiddleItem, this.state.middleItemCount)}
{this.state.middlePlaceholderHeight
? <div style={{height: this.state.middlePlaceholderHeight+'px'}} key="mid"></div>
: null}
{this.renderItems(this.props.totalItems-this.state.lastItemCount, this.state.lastItemCount, true)}
</div>);
}
setViewport = (e) =>
{
this.viewport = e;
}
getRenderedItemHeight = (index) =>
{
if (this.itemRefs[index])
{
const e = ReactDOM.findDOMNode(this.itemRefs[index]);
if (e)
{
// MSIE sometimes manages to report non-integer element heights for elements of an integer height...
// Non-integer element sizes are allowed in getBoundingClientRect, one notable example of them
// are collapsed table borders. But we still ignore less than 1/100 of a pixel difference.
return Math.round(e.getBoundingClientRect().height*100)/100;
}
}
return 0;
}
onScroll = () =>
{
this.driver();
if (this.props.onScroll)
{
this.props.onScroll(this.viewport);
}
}
componentDidUpdate = () =>
{
let changed = this.driver();
if (!changed && this.state.scrollTimes > 0 && this.props.totalItems > 0 &&
this.viewport && this.viewport.offsetParent)
{
// FIXME: It would be better to find a way to put this logic back into virtual-scroll-driver
let pos = this.state.scrollTo;
if (pos > this.state.scrollHeightInItems)
{
pos = this.state.scrollHeightInItems;
}
if (this.state.targetHeight)
{
this.viewport.scrollTop = Math.round((this.state.targetHeight - this.state.viewportHeight)*pos/this.state.scrollHeightInItems);
this.setState({ scrollTimes: this.state.scrollTimes - 1 });
}
else
{
const el = ReactDOM.findDOMNode(this.itemRefs[Math.floor(pos)]);
if (el)
{
this.viewport.scrollTop = el.offsetTop - (this.props.headerHeight||0) + el.offsetHeight*(pos-Math.floor(pos));
}
this.setState({ scrollTimes: 0 });
}
}
}
componentDidMount()
{
this.driver();
}
scrollToItem = (pos) =>
{
// Scroll position must be recalculated twice, because first render
// may change the average row height. In fact, it must be repeated
// until average row height stops changing, but twice is usually sufficient
this.setState({ scrollTo: pos, scrollTimes: 2 });
}
getItemScrollPos = () =>
{
if (this.state.targetHeight)
{
// Virtual scroll is active
let pos = this.viewport.scrollTop / (this.state.targetHeight - this.state.viewportHeight);
return pos * this.state.scrollHeightInItems;
}
else
{
// Virtual scroll is inactive
let avgr = this.viewport.scrollHeight / this.state.totalItems;
return this.viewport.scrollTop / avgr;
}
}
driver = () =>
{
if (!this.viewport || !this.viewport.offsetParent)
{
// Fool tolerance - do nothing if we are hidden
return false;
}
const newState = virtualScrollDriver(
{
totalItems: this.props.totalItems,
minRowHeight: this.props.minRowHeight,
viewportHeight: this.props.viewportHeight || (this.viewport.clientHeight-(this.props.headerHeight||0)),
scrollTop: this.viewport.scrollTop,
},
this.state,
this.getRenderedItemHeight
);
if (newState.viewportHeight || this.state.viewportHeight)
{
return this.setStateIfDiffers(newState);
}
return false;
}
setStateIfDiffers(state, cb)
{
for (const k in state)
{
if (this.state[k] != state[k])
{
this.setState(state, cb);
return true;
}
}
return false;
}
}