-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
InfiniteLoader.example.js
173 lines (143 loc) · 4.56 KB
/
InfiniteLoader.example.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
/** @flow */
import * as React from 'react';
import PropTypes from 'prop-types';
import {
ContentBox,
ContentBoxHeader,
ContentBoxParagraph,
} from '../demo/ContentBox';
import Immutable from 'immutable';
import AutoSizer from '../AutoSizer';
import InfiniteLoader from './InfiniteLoader';
import List from '../List';
import styles from './InfiniteLoader.example.css';
const STATUS_LOADING = 1;
const STATUS_LOADED = 2;
export default class InfiniteLoaderExample extends React.PureComponent {
static contextTypes = {
list: PropTypes.instanceOf(Immutable.List).isRequired,
};
constructor(props) {
super(props);
this.state = {
loadedRowCount: 0,
loadedRowsMap: {},
loadingRowCount: 0,
};
this._timeoutIdMap = {};
this._clearData = this._clearData.bind(this);
this._isRowLoaded = this._isRowLoaded.bind(this);
this._loadMoreRows = this._loadMoreRows.bind(this);
this._rowRenderer = this._rowRenderer.bind(this);
}
componentWillUnmount() {
Object.keys(this._timeoutIdMap).forEach(timeoutId => {
clearTimeout(timeoutId);
});
}
render() {
const {list} = this.context;
const {loadedRowCount, loadingRowCount} = this.state;
return (
<ContentBox>
<ContentBoxHeader
text="InfiniteLoader"
sourceLink="https://github.com/bvaughn/react-virtualized/blob/master/source/InfiniteLoader/InfiniteLoader.example.js"
docsLink="https://github.com/bvaughn/react-virtualized/blob/master/docs/InfiniteLoader.md"
/>
<ContentBoxParagraph>
This component manages just-in-time data fetching to ensure that the
all visible rows have been loaded. It also uses a threshold to
determine how early to pre-fetch rows (before a user scrolls to them).
</ContentBoxParagraph>
<ContentBoxParagraph>
<div className={styles.cacheButtonAndCountRow}>
<button className={styles.button} onClick={this._clearData}>
Flush Cached Data
</button>
<div className={styles.cacheCountRow}>
{loadingRowCount} loading, {loadedRowCount} loaded
</div>
</div>
</ContentBoxParagraph>
<InfiniteLoader
isRowLoaded={this._isRowLoaded}
loadMoreRows={this._loadMoreRows}
rowCount={list.size}>
{({onRowsRendered, registerChild}) => (
<AutoSizer disableHeight>
{({width}) => (
<List
ref={registerChild}
className={styles.List}
height={200}
onRowsRendered={onRowsRendered}
rowCount={list.size}
rowHeight={30}
rowRenderer={this._rowRenderer}
width={width}
/>
)}
</AutoSizer>
)}
</InfiniteLoader>
</ContentBox>
);
}
_clearData() {
this.setState({
loadedRowCount: 0,
loadedRowsMap: {},
loadingRowCount: 0,
});
}
_isRowLoaded({index}) {
const {loadedRowsMap} = this.state;
return !!loadedRowsMap[index]; // STATUS_LOADING or STATUS_LOADED
}
_loadMoreRows({startIndex, stopIndex}) {
const {loadedRowsMap, loadingRowCount} = this.state;
const increment = stopIndex - startIndex + 1;
for (var i = startIndex; i <= stopIndex; i++) {
loadedRowsMap[i] = STATUS_LOADING;
}
this.setState({
loadingRowCount: loadingRowCount + increment,
});
const timeoutId = setTimeout(() => {
const {loadedRowCount, loadingRowCount} = this.state;
delete this._timeoutIdMap[timeoutId];
for (var i = startIndex; i <= stopIndex; i++) {
loadedRowsMap[i] = STATUS_LOADED;
}
this.setState({
loadingRowCount: loadingRowCount - increment,
loadedRowCount: loadedRowCount + increment,
});
promiseResolver();
}, 1000 + Math.round(Math.random() * 2000));
this._timeoutIdMap[timeoutId] = true;
let promiseResolver;
return new Promise(resolve => {
promiseResolver = resolve;
});
}
_rowRenderer({index, key, style}) {
const {list} = this.context;
const {loadedRowsMap} = this.state;
const row = list.get(index);
let content;
if (loadedRowsMap[index] === STATUS_LOADED) {
content = row.name;
} else {
content = (
<div className={styles.placeholder} style={{width: row.size}} />
);
}
return (
<div className={styles.row} key={key} style={style}>
{content}
</div>
);
}
}