-
Notifications
You must be signed in to change notification settings - Fork 50
/
InfiniteScroll.js
53 lines (42 loc) 路 1.17 KB
/
InfiniteScroll.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
import React from 'react';
import PropTypes from 'prop-types';
export default class InfiniteScroll extends React.Component {
static scrollingClassName = 'mdl-layout__content';
static propTypes = {
distance: PropTypes.number.isRequired,
onLoadMore: PropTypes.func.isRequired,
hasMore: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired
};
constructor(props) {
super(props);
}
componentDidMount() {
this.scrollingElement.addEventListener('scroll', this.onScroll);
}
componentWillUnmount() {
this.scrollingElement.removeEventListener('scroll', this.onScroll);
}
@autobind
onScroll(event) {
if (this.props.isLoading) {
return;
}
if (!this.props.hasMore) {
return;
}
const target = event.target,
targetHeight = target.clientHeight,
scrollTop = target.scrollTop,
scrollHeight = target.scrollHeight;
if (scrollTop + targetHeight - scrollHeight > -1 * this.props.distance) {
this.props.onLoadMore();
}
}
get scrollingElement() {
return document.querySelector(`.${InfiniteScroll.scrollingClassName}`);
}
render() {
return this.props.children;
}
}