-
Notifications
You must be signed in to change notification settings - Fork 11
/
DynamicVirtualScrollExample.js
132 lines (120 loc) · 4.29 KB
/
DynamicVirtualScrollExample.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
import React from 'react';
import { virtualScrollDriver } from './DynamicVirtualScroll.js';
export class DynamicVirtualScrollExample extends React.PureComponent
{
useFixedHeader = true
constructor()
{
super();
const items = [];
for (let i = 0; i < 1000; i++)
{
items[i] = 30 + Math.round(Math.random()*50);
}
this.state = { items };
}
getRenderedItemHeight_MemoryExample = (index) =>
{
// Just for example: imitating renderer not knowing about off-screen items
if (index >= this.state.firstMiddleItem && index < this.state.firstMiddleItem+this.state.middleItemCount ||
index >= this.state.items.length - this.state.lastItemCount)
{
return this.state.items[index];
}
return 0;
}
getRenderedItemHeight_DOMExample = (index) =>
{
// DOM example. As smooth as the previous one (memory example), even without caching
if (this.itemElements[index])
{
return this.itemElements[index].getBoundingClientRect().height;
}
return 0;
}
getRenderedItemHeight = this.getRenderedItemHeight_DOMExample
renderItems(start, count)
{
return this.state.items.slice(start, start+count).map((item, index) => (<div
key={'i'+(index+start)}
ref={e => this.itemElements[index+start] = e}
style={{height: item+'px', color: 'white', textAlign: 'center', lineHeight: item+'px', background: 'rgb('+Math.round(item*255/80)+',0,0)'}}>
№ {index+start}: {item}px
</div>));
}
render()
{
this.itemElements = [];
return (<div style={{position: 'relative', width: '400px'}}>
<div style={{overflowY: 'scroll', height: '400px', width: '400px', overflowAnchor: 'none', outline: 'none'}}
tabIndex="1"
ref={e => this.viewport = e}
onScroll={this.driver}>
<div style={{height: this.state.targetHeight+'px'}}>
{this.useFixedHeader
? <div style={{height: '30px'}}></div>
: null}
{this.state.topPlaceholderHeight
? <div style={{height: this.state.topPlaceholderHeight+'px'}}></div>
: null}
{this.state.middleItemCount
? this.renderItems(this.state.firstMiddleItem, this.state.middleItemCount)
: null}
{this.state.middlePlaceholderHeight
? <div style={{height: this.state.middlePlaceholderHeight+'px'}}></div>
: null}
{this.state.lastItemCount
? this.renderItems(this.state.items.length-this.state.lastItemCount, this.state.lastItemCount)
: null}
</div>
</div>
{this.useFixedHeader ? <div style={{
position: 'absolute',
top: 0,
left: 0,
right: this.state.scrollbarWidth+'px',
height: '30px',
background: '#0080c0',
color: 'white',
textAlign: 'center',
lineHeight: '30px'}}>
fixed header
</div> : null}
</div>);
}
driver = () =>
{
const newState = virtualScrollDriver(
{
totalItems: this.state.items.length,
minRowHeight: 30,
viewportHeight: this.viewport.clientHeight - (this.useFixedHeader ? 30 : 0),
scrollTop: this.viewport.scrollTop,
},
this.state,
this.getRenderedItemHeight
);
newState.scrollbarWidth = this.viewport ? this.viewport.offsetWidth-this.viewport.clientWidth : 12;
this.setStateIfDiffers(newState);
}
componentDidUpdate = () =>
{
this.driver();
}
componentDidMount()
{
this.componentDidUpdate();
}
setStateIfDiffers(state, cb)
{
for (const k in state)
{
if (this.state[k] != state[k])
{
this.setState(state, cb);
return true;
}
}
return false;
}
}