Skip to content

Commit 7877d2b

Browse files
author
Yangfan Huang
committed
Support auto pagination
1 parent 7b4bfe0 commit 7877d2b

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

GiftedListView.js

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function MergeRecursive(obj1, obj2) {
3131
var GiftedSpinner = require('react-native-gifted-spinner');
3232

3333
var GiftedListView = React.createClass({
34-
34+
3535
getDefaultProps() {
3636
return {
3737
customStyles: {},
@@ -44,8 +44,9 @@ var GiftedListView = React.createClass({
4444
headerView: null,
4545
sectionHeaderView: null,
4646
withSections: false,
47+
autoPaginate: false,
4748
onFetch(page, callback, options) { callback([]); },
48-
49+
4950
paginationFetchigView: null,
5051
paginationAllLoadedView: null,
5152
paginationWaitingView: null,
@@ -60,7 +61,7 @@ var GiftedListView = React.createClass({
6061
},
6162
};
6263
},
63-
64+
6465
propTypes: {
6566
customStyles: React.PropTypes.object,
6667
initialListSize: React.PropTypes.number,
@@ -72,8 +73,9 @@ var GiftedListView = React.createClass({
7273
headerView: React.PropTypes.func,
7374
sectionHeaderView: React.PropTypes.func,
7475
withSections: React.PropTypes.bool,
76+
autoPaginate: React.PropTypes.bool,
7577
onFetch: React.PropTypes.func,
76-
78+
7779
paginationFetchigView: React.PropTypes.func,
7880
paginationAllLoadedView: React.PropTypes.func,
7981
paginationWaitingView: React.PropTypes.func,
@@ -84,20 +86,20 @@ var GiftedListView = React.createClass({
8486
renderSeparator: React.PropTypes.func,
8587
PullToRefreshViewAndroidProps: React.PropTypes.object,
8688
},
87-
89+
8890
_setY(y) { this._y = y; },
8991
_getY(y) { return this._y; },
9092
_setPage(page) { this._page = page; },
9193
_getPage() { return this._page; },
9294
_setRows(rows) { this._rows = rows; },
9395
_getRows() { return this._rows; },
94-
95-
96+
97+
9698
paginationFetchigView() {
9799
if (this.props.paginationFetchigView) {
98100
return this.props.paginationFetchigView();
99101
}
100-
102+
101103
return (
102104
<View style={[this.defaultStyles.paginationView, this.props.customStyles.paginationView]}>
103105
<GiftedSpinner />
@@ -108,7 +110,7 @@ var GiftedListView = React.createClass({
108110
if (this.props.paginationAllLoadedView) {
109111
return this.props.paginationAllLoadedView();
110112
}
111-
113+
112114
return (
113115
<View style={[this.defaultStyles.paginationView, this.props.customStyles.paginationView]}>
114116
<Text style={[this.defaultStyles.actionsLabel, this.props.customStyles.actionsLabel]}>
@@ -121,9 +123,9 @@ var GiftedListView = React.createClass({
121123
if (this.props.paginationWaitingView) {
122124
return this.props.paginationWaitingView(paginateCallback);
123125
}
124-
126+
125127
return (
126-
<TouchableHighlight
128+
<TouchableHighlight
127129
underlayColor='#c8c7cc'
128130
onPress={paginateCallback}
129131
style={[this.defaultStyles.paginationView, this.props.customStyles.paginationView]}
@@ -157,7 +159,7 @@ var GiftedListView = React.createClass({
157159
if (this.props.refreshableWillRefreshView) {
158160
return this.props.refreshableWillRefreshView();
159161
}
160-
162+
161163
return (
162164
<View>
163165
<View style={[this.defaultStyles.refreshableView, this.props.customStyles.refreshableView]}>
@@ -173,7 +175,7 @@ var GiftedListView = React.createClass({
173175
if (this.props.refreshableWaitingView) {
174176
return this.props.refreshableWaitingView(refreshCallback);
175177
}
176-
178+
177179
return (
178180
<View>
179181
<View style={[this.defaultStyles.refreshableView, this.props.customStyles.refreshableView]}>
@@ -189,14 +191,14 @@ var GiftedListView = React.createClass({
189191
if (this.props.emptyView) {
190192
return this.props.emptyView(refreshCallback);
191193
}
192-
194+
193195
return (
194196
<View style={[this.defaultStyles.defaultView, this.props.customStyles.defaultView]}>
195197
<Text style={[this.defaultStyles.defaultViewTitle, this.props.customStyles.defaultViewTitle]}>
196198
Sorry, there is no content to display
197199
</Text>
198-
199-
<TouchableHighlight
200+
201+
<TouchableHighlight
200202
underlayColor='#c8c7cc'
201203
onPress={refreshCallback}
202204
>
@@ -211,12 +213,20 @@ var GiftedListView = React.createClass({
211213
if (this.props.renderSeparator) {
212214
return this.props.renderSeparator();
213215
}
214-
216+
215217
return (
216218
<View style={[this.defaultStyles.separator, this.props.customStyles.separator]} />
217219
);
218220
},
219-
221+
onEndReached() {
222+
if (this.props.autoPaginate) {
223+
this._onPaginate();
224+
}
225+
if (this.props.onEndReached) {
226+
this.props.onEndReached();
227+
}
228+
},
229+
220230
getInitialState() {
221231

222232
if (this.props.refreshable === true && Platform.OS !== 'android') {
@@ -243,7 +253,7 @@ var GiftedListView = React.createClass({
243253
} else {
244254
ds = new ListView.DataSource({
245255
rowHasChanged: (row1, row2) => row1 !== row2,
246-
});
256+
});
247257
return {
248258
dataSource: ds.cloneWithRows(this._getRows()),
249259
refreshStatus: 'waiting',
@@ -265,7 +275,7 @@ var GiftedListView = React.createClass({
265275
_refresh() {
266276
this._onRefresh({external: true});
267277
},
268-
278+
269279
_onRefresh(options = {}) {
270280
this._scrollResponder.scrollTo(0);
271281
this.setState({
@@ -275,23 +285,26 @@ var GiftedListView = React.createClass({
275285
this._setPage(1);
276286
this.props.onFetch(this._getPage(), this._postRefresh, options);
277287
},
278-
288+
279289
_postRefresh(rows = [], options = {}) {
280290
this._updateRows(rows, options);
281291
if (this.props.refreshable === true) {
282292
// @issue
283293
// if a scrolling is already in progress, this scroll will not be executed
284-
this._scrollResponder.scrollTo(this.props.refreshableViewHeight);
294+
this._scrollResponder.scrollTo(this.props.refreshableViewHeight);
285295
}
286296
},
287-
297+
288298
_onPaginate() {
289-
this.setState({
290-
paginationStatus: 'fetching',
291-
});
292-
this.props.onFetch(this._getPage() + 1, this._postPaginate, {});
299+
if (this.state.paginationStatus === 'firstLoad'
300+
|| this.state.paginationStatus === 'waiting') {
301+
this.setState({
302+
paginationStatus: 'fetching',
303+
});
304+
this.props.onFetch(this._getPage() + 1, this._postPaginate, {});
305+
}
293306
},
294-
307+
295308
_postPaginate(rows = [], options = {}) {
296309
this._setPage(this._getPage() + 1);
297310
var mergedRows = null;
@@ -302,7 +315,7 @@ var GiftedListView = React.createClass({
302315
}
303316
this._updateRows(mergedRows, options);
304317
},
305-
318+
306319
_updateRows(rows = [], options = {}) {
307320
this._setRows(rows);
308321
if (this.props.withSections === true) {
@@ -318,10 +331,10 @@ var GiftedListView = React.createClass({
318331
refreshStatus: 'waiting',
319332
isRefreshing: false,
320333
paginationStatus: (options.allLoaded === true ? 'allLoaded' : 'waiting'),
321-
});
334+
});
322335
}
323336
},
324-
337+
325338
_onResponderRelease() {
326339
if (this.props.refreshable === true) {
327340
if (Platform.OS !== 'android') {
@@ -331,12 +344,12 @@ var GiftedListView = React.createClass({
331344
}
332345
}
333346
},
334-
347+
335348
_onScroll(e) {
336349
this._setY(e.nativeEvent.contentOffset.y);
337350
if (this.props.refreshable === true) {
338351
if (Platform.OS !== 'android') {
339-
if (this._getY() < this.props.refreshableViewHeight - this.props.refreshableDistance
352+
if (this._getY() < this.props.refreshableViewHeight - this.props.refreshableDistance
340353
&& this.state.refreshStatus === 'waiting'
341354
&& this._scrollResponder.scrollResponderHandleScrollShouldSetResponder() === true
342355
) {
@@ -348,7 +361,7 @@ var GiftedListView = React.createClass({
348361
}
349362
}
350363
},
351-
364+
352365
_renderRefreshView() {
353366
switch (this.state.refreshStatus) {
354367
case 'fetching':
@@ -361,7 +374,7 @@ var GiftedListView = React.createClass({
361374
return this.refreshableWaitingView(this._onRefresh);
362375
}
363376
},
364-
377+
365378
_renderPaginationView() {
366379
if ((this.state.paginationStatus === 'fetching' && this.props.pagination === true) || (this.state.paginationStatus === 'firstLoad' && this.props.firstLoader === true)) {
367380
return this.paginationFetchigView();
@@ -383,16 +396,16 @@ var GiftedListView = React.createClass({
383396
return {top: 0, bottom: 0, left: 0, right: 0};
384397
}
385398
},
386-
399+
387400
_calculateContentOffset() {
388401
if (this.props.refreshable === true && Platform.OS !== 'android') {
389402
return {x: 0, y: this.props.refreshableViewHeight};
390403
} else {
391404
return {x: 0, y: 0};
392405
}
393406
},
394-
395-
407+
408+
396409
renderListView(style = {}) {
397410
return (
398411
<ListView
@@ -403,23 +416,25 @@ var GiftedListView = React.createClass({
403416

404417
renderHeader={this.props.refreshable === true && Platform.OS !== 'android' ? this._renderRefreshView : this.headerView}
405418
renderFooter={this._renderPaginationView}
406-
419+
407420
onScroll={this.props.refreshable === true && Platform.OS !== 'android' ? this._onScroll : null}
408421
onResponderRelease={this.props.refreshable === true && Platform.OS !== 'android' ? this._onResponderRelease : null}
409422

410423
scrollEventThrottle={200}
411-
424+
412425
contentInset={this._calculateContentInset()}
413426
contentOffset={this._calculateContentOffset()}
414-
427+
415428
automaticallyAdjustContentInsets={false}
416429
scrollEnabled={true}
417430
canCancelContentTouches={true}
418-
431+
419432
renderSeparator={this.renderSeparator}
420-
433+
434+
onEndReached={this.onEndReached}
435+
421436
{...this.props}
422-
437+
423438
style={[this.props.style, style]}
424439
/>
425440
);
@@ -433,7 +448,7 @@ var GiftedListView = React.createClass({
433448
onRefresh={this._onRefresh}
434449

435450
{...this.props.PullToRefreshViewAndroidProps}
436-
451+
437452
style={[this.props.PullToRefreshViewAndroidProps.style, {flex: 1}]}
438453
>
439454
{this.renderListView({flex: 1})}
@@ -443,7 +458,7 @@ var GiftedListView = React.createClass({
443458
return this.renderListView();
444459
}
445460
},
446-
461+
447462
defaultStyles: {
448463
separator: {
449464
height: 1,

0 commit comments

Comments
 (0)