-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfinite-loading-table.js
More file actions
82 lines (71 loc) · 1.96 KB
/
Copy pathinfinite-loading-table.js
File metadata and controls
82 lines (71 loc) · 1.96 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
import React from 'react';
import PropTypes from 'prop-types';
import { Table } from './index';
import throttle from 'lodash.throttle';
export default class InfiniteLoadingTable extends React.Component {
static displayName = 'InfiniteLoadingTable';
static propTypes = {
rowPerPage: PropTypes.number,
data: PropTypes.array,
onPaginate: PropTypes.func,
isLoading: PropTypes.bool,
isExhausted: PropTypes.bool,
children: PropTypes.node,
}
static defaultProps = {
rowPerPage: 20,
data: [],
onPaginate: () => {},
isLoading: false,
isExhausted: false,
}
constructor(props) {
super(props);
this.throttledScrollHandler = throttle(this.scrollHandler, 200);
}
componentDidMount() {
window.addEventListener('scroll', this.throttledScrollHandler);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.throttledScrollHandler);
}
getPlaceholders(amount) {
return Array(amount).fill({ pending: true });
}
appendPlaceholders(data, amount) {
return data.concat(this.getPlaceholders(amount));
}
scrollHandler = () => {
if (!this._container || this.props.isLoading || this.props.isExhausted) {
return;
}
const { bottom } = this._container.getBoundingClientRect();
if (bottom <= window.innerHeight + 100) {
const { data, rowPerPage } = this.props;
const currentPage = Math.ceil(data.length / rowPerPage);
this.props.onPaginate(currentPage);
}
};
render() {
const { data, rowPerPage, isLoading, ...rest } = this.props;
let dataWithPendings;
if (isLoading) {
dataWithPendings = this.appendPlaceholders(data, rowPerPage);
}
return (
<div
ref={ el => {
this._container = el;
} }
className="data-table--infinite"
>
<Table
data={ dataWithPendings || data }
{ ...rest }
>
{ this.props.children }
</Table>
</div>
);
}
}