-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathInfiniteLoader.tsx
More file actions
208 lines (174 loc) · 5.66 KB
/
InfiniteLoader.tsx
File metadata and controls
208 lines (174 loc) · 5.66 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, { Component, createRef, CSSProperties, ReactNode } from "react";
import { isFunction } from "../___utils/isType";
import DefaultLoadIndicator from "./DefaultLoadIndicator";
export interface InfiniteLoaderInterface {
loadingIndicator?: null | (() => ReactNode) | ReactNode;
loadingIndicatorPosition?: string;
hasMore: boolean;
loadMore: null | (() => void);
}
interface InfiniteLoaderState {
scrollingContainer: HTMLElement | null;
loadIndicatorContainer: HTMLDivElement | null;
loading: boolean;
prevItemsCount: number;
}
interface InfiniteLoaderProps extends InfiniteLoaderInterface {
itemsCount: number;
scrollingContainerId?: string;
}
class InfiniteLoader extends Component<
InfiniteLoaderProps,
InfiniteLoaderState
> {
state: InfiniteLoaderState = {
prevItemsCount: this.props.itemsCount,
loadIndicatorContainer: null,
loading: false,
scrollingContainer: null,
};
loaderContainerRef = createRef<HTMLDivElement>();
// track the last scroll position so when new dom elements are inserted to avoid scroll jump
lastScrollTop = 0;
mounted = false;
// keep track of the dom items in the list
currentItemsCount = 0;
componentDidMount(): void {
this.mounted = true;
const { current: loadIndicatorContainer } = this.loaderContainerRef;
if (loadIndicatorContainer) {
let scrollingContainer = loadIndicatorContainer.parentNode as HTMLElement;
if (this.props.scrollingContainerId) {
// find the closest ancestor with the id
scrollingContainer = scrollingContainer.closest(
`#${this.props.scrollingContainerId}`
) as HTMLElement;
if (!scrollingContainer) {
throw new Error(
`Can't find scrolling container with id of "${this.props.scrollingContainerId}"`
);
}
}
this.setState(
{
loadIndicatorContainer,
scrollingContainer,
},
() => {
this.currentItemsCount = this.getScrollingContainerChildrenCount();
this.setupScrollingContainerEventsListener();
}
);
} else {
console.warn(
"FlatList: it was not possible to get container's ref. " +
"Infinite scrolling pagination will not be possible"
);
}
}
componentDidUpdate(
prevProps: InfiniteLoaderProps,
prevState: InfiniteLoaderState
): void {
// reset scroll position to where last was
if (this.state.scrollingContainer) {
this.state.scrollingContainer.scrollTop = this.lastScrollTop;
}
// reset loading state when the list size changes
if (prevProps.itemsCount !== this.props.itemsCount) {
this.reset();
}
this.checkIfLoadingIsNeeded();
}
componentWillUnmount(): void {
this.setupScrollingContainerEventsListener(true);
this.mounted = false;
}
// update the loading flags and items count whether "hasMore" is false or list changed
reset(): void {
this.setState({ loading: false });
}
getScrollingContainerChildrenCount = (): number => {
const { scrollingContainer } = this.state;
if (scrollingContainer) {
return Math.max(0, scrollingContainer.children.length);
}
return 0;
};
setupScrollingContainerEventsListener = (removeEvent = false) => {
const { scrollingContainer } = this.state;
if (scrollingContainer) {
["scroll", "mousewheel", "touchmove"].forEach((event: string) => {
scrollingContainer.removeEventListener(
event,
this.checkIfLoadingIsNeeded,
true
);
if (!removeEvent) {
scrollingContainer.addEventListener(
event,
this.checkIfLoadingIsNeeded,
true
);
}
});
}
};
// show or hide loading indicators based on scroll position
// calls the "loadMore" function when is needed
checkIfLoadingIsNeeded = (): void => {
if (!this.mounted || !this.props.hasMore || this.state.loading) {
return;
}
const { scrollingContainer, loadIndicatorContainer } = this.state;
if (scrollingContainer && loadIndicatorContainer) {
const { scrollTop, offsetTop, offsetHeight } = scrollingContainer;
this.lastScrollTop = scrollTop;
const loaderPosition = loadIndicatorContainer.offsetTop - scrollTop;
const startingPoint = offsetTop + offsetHeight;
if (loaderPosition <= startingPoint) {
this.setState(
{ prevItemsCount: this.props.itemsCount, loading: true },
() => {
(this.props.loadMore as () => void)();
}
);
}
}
};
render(): ReactNode {
const { loading } = this.state;
const {
hasMore,
loadingIndicator = DefaultLoadIndicator,
loadingIndicatorPosition = "left",
} = this.props;
const spinning = hasMore && loading;
// do not remove the element from the dom so the ref is not broken but set it invisible enough
const styles: CSSProperties = {
display: "flex",
height: spinning ? "auto" : 0,
justifyContent:
loadingIndicatorPosition === "center"
? loadingIndicatorPosition
: loadingIndicatorPosition === "right"
? "flex-end"
: "flex-start",
padding: spinning ? "5px 0" : 0,
visibility: spinning ? "visible" : "hidden",
};
const loadingEl = isFunction(loadingIndicator)
? (loadingIndicator as () => ReactNode)()
: (loadingIndicator as ReactNode);
return (
<div
ref={this.loaderContainerRef}
className="__infinite-loader"
style={styles}
>
{spinning && (loadingIndicator ? loadingEl : <DefaultLoadIndicator />)}
</div>
);
}
}
export default InfiniteLoader;