1
- import React , { Component } from "react" ;
1
+ import React , { Component , ReactNode , CSSProperties } from "react" ;
2
2
import PropTypes from "prop-types" ;
3
3
import throttle from "./utils/throttle" ;
4
4
import { ThresholdUnits , parseThreshold } from "./utils/threshold" ;
5
+ type Fn = ( ) => any ;
6
+ interface Props {
7
+ next : Fn ;
8
+ hasMore : ReactNode ;
9
+ children : ReactNode ;
10
+ loader : ReactNode ;
11
+ scrollThreshold : number | string ;
12
+ endMessage : ReactNode ;
13
+ style : CSSProperties ;
14
+ height : number ;
15
+ scrollableTarget : ReactNode ;
16
+ hasChildren : ReactNode ;
17
+ pullDownToRefresh : ReactNode ;
18
+ pullDownToRefreshContent : ReactNode ;
19
+ releaseToRefreshContent : ReactNode ;
20
+ pullDownToRefreshThreshold : number ;
21
+ refreshFunction : Fn ;
22
+ onScroll : Fn ;
23
+ dataLength : number ;
24
+ }
5
25
6
- export default class InfiniteScroll extends Component {
7
- constructor ( props ) {
8
- super ( props ) ;
26
+ interface State {
27
+ showLoader : boolean ;
28
+ pullToRefreshThresholdBreached : boolean ;
29
+ }
9
30
10
- this . lastScrollTop = 0 ;
11
- this . actionTriggered = false ;
31
+ export default class InfiniteScroll extends Component < Props , State > {
32
+ private throttledOnScrollListener : ( ) => void ;
33
+ constructor ( props : Props ) {
34
+ super ( props ) ;
12
35
13
36
this . state = {
14
37
showLoader : false ,
15
38
pullToRefreshThresholdBreached : false
16
39
} ;
17
40
18
- // variables to keep track of pull down behaviour
19
- this . startY = 0 ;
20
- this . currentY = 0 ;
21
- this . dragging = false ;
22
-
23
- // will be populated in componentDidMount
24
- // based on the height of the pull down element
25
- this . maxPullDownDistance = 0 ;
26
-
27
41
this . onScrollListener = this . onScrollListener . bind ( this ) ;
28
42
this . throttledOnScrollListener = throttle ( this . onScrollListener , 150 ) . bind (
29
43
this
@@ -34,6 +48,17 @@ export default class InfiniteScroll extends Component {
34
48
this . getScrollableTarget = this . getScrollableTarget . bind ( this ) ;
35
49
}
36
50
51
+ private lastScrollTop = 0 ;
52
+ private actionTriggered = false ;
53
+
54
+ // variables to keep track of pull down behaviour
55
+ private startY = 0 ;
56
+ private currentY = 0 ;
57
+ private dragging = false ;
58
+
59
+ // will be populated in componentDidMount
60
+ // based on the height of the pull down element
61
+ private maxPullDownDistance = 0 ;
37
62
componentDidMount ( ) {
38
63
this . _scrollableNode = this . getScrollableTarget ( ) ;
39
64
this . el = this . props . height
@@ -87,7 +112,11 @@ export default class InfiniteScroll extends Component {
87
112
88
113
componentWillReceiveProps ( props ) {
89
114
// do nothing when dataLength and key are unchanged
90
- if ( this . props . key === props . key && this . props . dataLength === props . dataLength ) return ;
115
+ if (
116
+ this . props . key === props . key &&
117
+ this . props . dataLength === props . dataLength
118
+ )
119
+ return ;
91
120
92
121
this . actionTriggered = false ;
93
122
// update state when new data was sent in
@@ -98,8 +127,9 @@ export default class InfiniteScroll extends Component {
98
127
}
99
128
100
129
getScrollableTarget ( ) {
101
- if ( this . props . scrollableTarget instanceof HTMLElement ) return this . props . scrollableTarget ;
102
- if ( typeof this . props . scrollableTarget === 'string' ) {
130
+ if ( this . props . scrollableTarget instanceof HTMLElement )
131
+ return this . props . scrollableTarget ;
132
+ if ( typeof this . props . scrollableTarget === "string" ) {
103
133
return document . getElementById ( this . props . scrollableTarget ) ;
104
134
}
105
135
if ( this . props . scrollableTarget === null ) {
@@ -156,9 +186,9 @@ export default class InfiniteScroll extends Component {
156
186
requestAnimationFrame ( ( ) => {
157
187
// this._infScroll
158
188
if ( this . _infScroll ) {
159
- this . _infScroll . style . overflow = "auto" ;
160
- this . _infScroll . style . transform = "none" ;
161
- this . _infScroll . style . willChange = "none" ;
189
+ this . _infScroll . style . overflow = "auto" ;
190
+ this . _infScroll . style . transform = "none" ;
191
+ this . _infScroll . style . willChange = "none" ;
162
192
}
163
193
} ) ;
164
194
}
@@ -178,7 +208,8 @@ export default class InfiniteScroll extends Component {
178
208
}
179
209
180
210
return (
181
- target . scrollTop + clientHeight >= threshold . value / 100 * target . scrollHeight
211
+ target . scrollTop + clientHeight >=
212
+ ( threshold . value / 100 ) * target . scrollHeight
182
213
) ;
183
214
}
184
215
@@ -193,8 +224,8 @@ export default class InfiniteScroll extends Component {
193
224
this . props . height || this . _scrollableNode
194
225
? event . target
195
226
: document . documentElement . scrollTop
196
- ? document . documentElement
197
- : document . body ;
227
+ ? document . documentElement
228
+ : document . body ;
198
229
199
230
// return immediately if the action has already been triggered,
200
231
// prevents multiple triggers.
@@ -232,7 +263,7 @@ export default class InfiniteScroll extends Component {
232
263
return (
233
264
< div style = { outerDivStyle } >
234
265
< div
235
- className = { `infinite-scroll-component ${ this . props . className || '' } ` }
266
+ className = { `infinite-scroll-component ${ this . props . className || "" } ` }
236
267
ref = { infScroll => ( this . _infScroll = infScroll ) }
237
268
style = { style }
238
269
>
0 commit comments