-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInfiniteListView.js
More file actions
157 lines (140 loc) · 3.9 KB
/
InfiniteListView.js
File metadata and controls
157 lines (140 loc) · 3.9 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
146
147
148
149
150
151
152
153
154
155
156
157
import React, { Component, PropTypes } from "react";
import {
Dimensions,
ListView,
View,
RefreshControl,
ActivityIndicator
} from "react-native";
const LOAD_MORE_TYPE = "LOAD_MORE_TYPE";
export default class InfiniteListView extends Component {
static propTypes = {
...ListView.propTypes,
distanceToLoadMore: PropTypes.number.isRequired,
canLoadMore: PropTypes.oneOfType([PropTypes.func, PropTypes.bool])
.isRequired,
loadMoreRow: PropTypes.func,
isLoadingMore: PropTypes.bool,
spinnerColor: PropTypes.color,
onLoadMore: PropTypes.func.isRequired
};
static defaultProps = {
distanceToLoadMore: 1500,
canLoadMore: false,
isLoadingMore: false,
spinnerColor: "#33A9E0",
scrollEventThrottle: 100
};
constructor(props) {
super(props);
if (props.dataArray && props.renderRow) {
let rowHasChanged = props.rowHasChanged || ((r1, r2) => r1 !== r2);
const ds = new ListView.DataSource({ rowHasChanged: rowHasChanged });
this.state = {
dataSource: ds.cloneWithRows(props.dataArray)
};
} else {
this.state = {};
}
this.onScroll = this.onScroll.bind(this);
this.shouldLoadMore = this.shouldLoadMore.bind(this);
this.onRefresh = this.onRefresh.bind(this);
}
componentWillReceiveProps(nextProps) {
if (this.state.dataSource) {
let list = [...nextProps.dataArray, { type: LOAD_MORE_TYPE }];
this.setState({
dataSource: this.state.dataSource.cloneWithRows(list)
});
}
}
onScroll(event) {
if (this.props.onScroll) {
this.props.onScroll(event);
}
if (this.shouldLoadMore(event)) {
this.props.onLoadMore();
}
}
onRefresh() {
this.props.onRefresh();
}
shouldLoadMore(event) {
var canLoadMore = typeof this.props.canLoadMore === "function"
? this.props.canLoadMore()
: this.props.canLoadMore;
canLoadMore =
!this.props.isLoadingMore &&
canLoadMore &&
this.distanceFromEnd(event) < this.props.distanceToLoadMore;
return canLoadMore;
}
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;
};
renderLoadMoreRow = () => {
return (
<View
style={{ height: 60, alignItems: "center", justifyContent: "center" }}
>
{this.props.isLoadingMore
? <ActivityIndicator
color={this.props.spinnerColor}
animating={true}
size="small"
/>
: <View />}
</View>
);
};
renderRefreshControl = () => {
return (
<RefreshControl
style={{ backgroundColor: "transparent" }}
refreshing={this.props.isRefreshing}
onRefresh={this.onRefresh}
/>
);
};
renderRow = (rowData, sectionID, rowID) => {
if (rowData.type !== null && rowData.type === LOAD_MORE_TYPE) {
return this.props.renderLoadMoreRow
? this.props.renderLoadMoreRow()
: this.renderLoadMoreRow();
}
return this.props.renderRow(rowData, sectionID, rowID);
};
render() {
return (
<ListView
{...this.props}
enableEmptySections
dataSource={this.state.dataSource}
renderRow={this.renderRow}
refreshControl={this.renderRefreshControl()}
onScroll={this.onScroll}
/>
);
}
}