This repository was archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathInfiniteScrollView.js
More file actions
145 lines (121 loc) · 4.04 KB
/
InfiniteScrollView.js
File metadata and controls
145 lines (121 loc) · 4.04 KB
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
'use strict';
import PropTypes from 'prop-types';
import React from 'react';
import { ScrollView, View } from 'react-native';
import ScrollableMixin from 'react-native-scrollable-mixin';
import cloneReferencedElement from 'react-clone-referenced-element';
import DefaultLoadingIndicator from './DefaultLoadingIndicator';
export default class InfiniteScrollView extends React.Component {
static propTypes = {
...ScrollView.propTypes,
distanceToLoadMore: PropTypes.number.isRequired,
canLoadMore: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]).isRequired,
onLoadMoreAsync: PropTypes.func.isRequired,
onLoadError: PropTypes.func,
renderLoadingIndicator: PropTypes.func.isRequired,
renderLoadingErrorIndicator: PropTypes.func.isRequired,
};
static defaultProps = {
distanceToLoadMore: 1500,
canLoadMore: false,
scrollEventThrottle: 100,
renderLoadingIndicator: () => <DefaultLoadingIndicator />,
renderLoadingErrorIndicator: () => <View />,
renderScrollComponent: props => <ScrollView {...props} />,
};
constructor(props, context) {
super(props, context);
this.state = {
isDisplayingError: false,
};
this._handleScroll = this._handleScroll.bind(this);
this._loadMoreAsync = this._loadMoreAsync.bind(this);
}
getScrollResponder() {
return this._scrollComponent.getScrollResponder();
}
setNativeProps(nativeProps) {
this._scrollComponent.setNativeProps(nativeProps);
}
render() {
let statusIndicator;
if (this.state.isDisplayingError) {
statusIndicator = React.cloneElement(
this.props.renderLoadingErrorIndicator({ onRetryLoadMore: this._loadMoreAsync }),
{ key: 'loading-error-indicator' }
);
} else if (this.state.isLoading) {
statusIndicator = React.cloneElement(this.props.renderLoadingIndicator(), {
key: 'loading-indicator',
});
}
let { renderScrollComponent, ...props } = this.props;
Object.assign(props, {
onScroll: this._handleScroll,
children: [this.props.children, statusIndicator],
});
return cloneReferencedElement(renderScrollComponent(props), {
ref: component => {
this._scrollComponent = component;
},
});
}
_handleScroll(event) {
if (this.props.onScroll) {
this.props.onScroll(event);
}
if (this._shouldLoadMore(event)) {
this._loadMoreAsync().catch(error => {
console.error('Unexpected error while loading more content:', error);
});
}
}
_shouldLoadMore(event) {
let canLoadMore =
typeof this.props.canLoadMore === 'function'
? this.props.canLoadMore()
: this.props.canLoadMore;
return (
!this.state.isLoading &&
canLoadMore &&
!this.state.isDisplayingError &&
this._distanceFromEnd(event) < this.props.distanceToLoadMore
);
}
async _loadMoreAsync() {
if (this.state.isLoading && __DEV__) {
throw new Error('_loadMoreAsync called while isLoading is true');
}
try {
this.setState({ isDisplayingError: false, isLoading: true });
await this.props.onLoadMoreAsync();
} catch (e) {
if (this.props.onLoadError) {
this.props.onLoadError(e);
}
this.setState({ isDisplayingError: true });
} finally {
this.setState({ isLoading: false });
}
}
_distanceFromEnd(event) {
let { contentSize, contentInset, contentOffset, layoutMeasurement } = event.nativeEvent;
let contentLength;
let trailingInset;
let scrollOffset;
let viewportLength;
if (this.props.horizontal) {
contentLength = contentSize.width;
trailingInset = contentInset.right;
scrollOffset = contentOffset.x;
viewportLength = layoutMeasurement.width;
} else {
contentLength = contentSize.height;
trailingInset = contentInset.bottom;
scrollOffset = contentOffset.y;
viewportLength = layoutMeasurement.height;
}
return contentLength + trailingInset - scrollOffset - viewportLength;
}
}
Object.assign(InfiniteScrollView.prototype, ScrollableMixin);