-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
81 lines (69 loc) · 1.87 KB
/
index.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
'use strict';
import React, { Component } from 'react';
import {
ScrollView
} from 'react-native';
export default class InfiniteScroll extends Component {
state = {
distanceFromEnd: 10
};
componentWillMount() {
var self = this;
if (this.props.distanceFromEnd) {
self.setState({
distanceFromEnd: this.props.distanceFromEnd
});
}
}
_handleScroll(event) {
if(this.props.onScroll){
this.props.onScroll(event);
}
if (this._shouldLoadMore(event)) {
if(this.props.onLoadMoreAsync){
this.props.onLoadMoreAsync();
}
}
}
_shouldLoadMore(event) {
return this._distanceFromEnd(event) < this.state.distanceFromEnd;
}
_distanceFromEnd(event): number {
let {
contentSize,
contentInset,
contentOffset,
layoutMeasurement,
} = event.nativeEvent;
let contentLength;
let trailingInset;
let scrollOffset;
let viewportLength;
let horizontal = this.props.horizontal || false;
if (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;
}
render() {
var self = this;
return (
<ScrollView
{...this.props}
automaticallyAdjustContentInsets={false}
onScroll={self._handleScroll.bind(self)}
scrollEventThrottle={200}
>
{this.props.children}
</ScrollView>
);
}
}