1
- import React , { Component , ReactNode , CSSProperties } from " react" ;
2
- import { throttle } from " throttle-debounce" ;
3
- import { ThresholdUnits , parseThreshold } from " ./utils/threshold" ;
1
+ import React , { Component , ReactNode , CSSProperties } from ' react' ;
2
+ import { throttle } from ' throttle-debounce' ;
3
+ import { ThresholdUnits , parseThreshold } from ' ./utils/threshold' ;
4
4
5
5
type Fn = ( ) => any ;
6
6
interface Props {
@@ -37,17 +37,15 @@ export default class InfiniteScroll extends Component<Props, State> {
37
37
38
38
this . state = {
39
39
showLoader : false ,
40
- pullToRefreshThresholdBreached : false
40
+ pullToRefreshThresholdBreached : false ,
41
41
} ;
42
42
43
- this . onScrollListener = this . onScrollListener . bind ( this ) ;
44
43
this . throttledOnScrollListener = throttle ( 150 , this . onScrollListener ) . bind (
45
44
this
46
45
) ;
47
46
this . onStart = this . onStart . bind ( this ) ;
48
47
this . onMove = this . onMove . bind ( this ) ;
49
48
this . onEnd = this . onEnd . bind ( this ) ;
50
- this . getScrollableTarget = this . getScrollableTarget . bind ( this ) ;
51
49
}
52
50
53
51
private throttledOnScrollListener : ( e : MouseEvent ) => void ;
@@ -59,13 +57,13 @@ export default class InfiniteScroll extends Component<Props, State> {
59
57
private _pullDown : HTMLDivElement | undefined ;
60
58
61
59
// variables to keep track of pull down behaviour
62
- private startY : number = 0 ;
63
- private currentY : number = 0 ;
64
- private dragging : boolean = false ;
60
+ private startY = 0 ;
61
+ private currentY = 0 ;
62
+ private dragging = false ;
65
63
66
64
// will be populated in componentDidMount
67
65
// based on the height of the pull down element
68
- private maxPullDownDistance : number = 0 ;
66
+ private maxPullDownDistance = 0 ;
69
67
70
68
componentDidMount ( ) {
71
69
this . _scrollableNode = this . getScrollableTarget ( ) ;
@@ -74,13 +72,13 @@ export default class InfiniteScroll extends Component<Props, State> {
74
72
: this . _scrollableNode || window ;
75
73
76
74
if ( this . el ) {
77
- this . el . addEventListener ( " scroll" , e =>
75
+ this . el . addEventListener ( ' scroll' , e =>
78
76
this . throttledOnScrollListener ( e as MouseEvent )
79
77
) ;
80
78
}
81
79
82
80
if (
83
- typeof this . props . initialScrollY === " number" &&
81
+ typeof this . props . initialScrollY === ' number' &&
84
82
this . el &&
85
83
this . el instanceof HTMLElement &&
86
84
this . el . scrollHeight > this . props . initialScrollY
@@ -89,13 +87,13 @@ export default class InfiniteScroll extends Component<Props, State> {
89
87
}
90
88
91
89
if ( this . props . pullDownToRefresh && this . el ) {
92
- this . el . addEventListener ( " touchstart" , this . onStart ) ;
93
- this . el . addEventListener ( " touchmove" , this . onMove ) ;
94
- this . el . addEventListener ( " touchend" , this . onEnd ) ;
90
+ this . el . addEventListener ( ' touchstart' , this . onStart ) ;
91
+ this . el . addEventListener ( ' touchmove' , this . onMove ) ;
92
+ this . el . addEventListener ( ' touchend' , this . onEnd ) ;
95
93
96
- this . el . addEventListener ( " mousedown" , this . onStart ) ;
97
- this . el . addEventListener ( " mousemove" , this . onMove ) ;
98
- this . el . addEventListener ( " mouseup" , this . onEnd ) ;
94
+ this . el . addEventListener ( ' mousedown' , this . onStart ) ;
95
+ this . el . addEventListener ( ' mousemove' , this . onMove ) ;
96
+ this . el . addEventListener ( ' mouseup' , this . onEnd ) ;
99
97
100
98
// get BCR of pullDown element to position it above
101
99
this . maxPullDownDistance =
@@ -106,7 +104,7 @@ export default class InfiniteScroll extends Component<Props, State> {
106
104
0 ;
107
105
this . forceUpdate ( ) ;
108
106
109
- if ( typeof this . props . refreshFunction !== " function" ) {
107
+ if ( typeof this . props . refreshFunction !== ' function' ) {
110
108
throw new Error (
111
109
`Mandatory prop "refreshFunction" missing.
112
110
Pull Down To Refresh functionality will not work
@@ -118,23 +116,23 @@ export default class InfiniteScroll extends Component<Props, State> {
118
116
119
117
componentWillUnmount ( ) {
120
118
if ( this . el ) {
121
- this . el . removeEventListener ( " scroll" , e =>
119
+ this . el . removeEventListener ( ' scroll' , e =>
122
120
this . throttledOnScrollListener ( e as MouseEvent )
123
121
) ;
124
122
125
123
if ( this . props . pullDownToRefresh ) {
126
- this . el . removeEventListener ( " touchstart" , this . onStart ) ;
127
- this . el . removeEventListener ( " touchmove" , this . onMove ) ;
128
- this . el . removeEventListener ( " touchend" , this . onEnd ) ;
124
+ this . el . removeEventListener ( ' touchstart' , this . onStart ) ;
125
+ this . el . removeEventListener ( ' touchmove' , this . onMove ) ;
126
+ this . el . removeEventListener ( ' touchend' , this . onEnd ) ;
129
127
130
- this . el . removeEventListener ( " mousedown" , this . onStart ) ;
131
- this . el . removeEventListener ( " mousemove" , this . onMove ) ;
132
- this . el . removeEventListener ( " mouseup" , this . onEnd ) ;
128
+ this . el . removeEventListener ( ' mousedown' , this . onStart ) ;
129
+ this . el . removeEventListener ( ' mousemove' , this . onMove ) ;
130
+ this . el . removeEventListener ( ' mouseup' , this . onEnd ) ;
133
131
}
134
132
}
135
133
}
136
134
137
- componentWillReceiveProps ( props : Props ) {
135
+ UNSAFE_componentWillReceiveProps ( props : Props ) {
138
136
// do nothing when dataLength and key are unchanged
139
137
if (
140
138
this . props . key === props . key &&
@@ -146,14 +144,14 @@ export default class InfiniteScroll extends Component<Props, State> {
146
144
// update state when new data was sent in
147
145
this . setState ( {
148
146
showLoader : false ,
149
- pullToRefreshThresholdBreached : false
147
+ pullToRefreshThresholdBreached : false ,
150
148
} ) ;
151
149
}
152
150
153
- getScrollableTarget ( ) {
151
+ getScrollableTarget = ( ) => {
154
152
if ( this . props . scrollableTarget instanceof HTMLElement )
155
153
return this . props . scrollableTarget ;
156
- if ( typeof this . props . scrollableTarget === " string" ) {
154
+ if ( typeof this . props . scrollableTarget === ' string' ) {
157
155
return document . getElementById ( this . props . scrollableTarget ) ;
158
156
}
159
157
if ( this . props . scrollableTarget === null ) {
@@ -163,7 +161,7 @@ export default class InfiniteScroll extends Component<Props, State> {
163
161
` ) ;
164
162
}
165
163
return null ;
166
- }
164
+ } ;
167
165
168
166
onStart : EventListener = ( evt : Event ) => {
169
167
if ( this . lastScrollTop ) return ;
@@ -178,7 +176,7 @@ export default class InfiniteScroll extends Component<Props, State> {
178
176
this . currentY = this . startY ;
179
177
180
178
if ( this . _infScroll ) {
181
- this . _infScroll . style . willChange = " transform" ;
179
+ this . _infScroll . style . willChange = ' transform' ;
182
180
this . _infScroll . style . transition = `transform 0.2s cubic-bezier(0,0,0.31,1)` ;
183
181
}
184
182
} ;
@@ -200,15 +198,15 @@ export default class InfiniteScroll extends Component<Props, State> {
200
198
Number ( this . props . pullDownToRefreshThreshold )
201
199
) {
202
200
this . setState ( {
203
- pullToRefreshThresholdBreached : true
201
+ pullToRefreshThresholdBreached : true ,
204
202
} ) ;
205
203
}
206
204
207
205
// so you can drag upto 1.5 times of the maxPullDownDistance
208
206
if ( this . currentY - this . startY > this . maxPullDownDistance * 1.5 ) return ;
209
207
210
208
if ( this . _infScroll ) {
211
- this . _infScroll . style . overflow = " visible" ;
209
+ this . _infScroll . style . overflow = ' visible' ;
212
210
this . _infScroll . style . transform = `translate3d(0px, ${ this . currentY -
213
211
this . startY } px, 0px)`;
214
212
}
@@ -227,9 +225,9 @@ export default class InfiniteScroll extends Component<Props, State> {
227
225
requestAnimationFrame ( ( ) => {
228
226
// this._infScroll
229
227
if ( this . _infScroll ) {
230
- this . _infScroll . style . overflow = " auto" ;
231
- this . _infScroll . style . transform = " none" ;
232
- this . _infScroll . style . willChange = " none" ;
228
+ this . _infScroll . style . overflow = ' auto' ;
229
+ this . _infScroll . style . transform = ' none' ;
230
+ this . _infScroll . style . willChange = ' none' ;
233
231
}
234
232
} ) ;
235
233
} ;
@@ -257,14 +255,14 @@ export default class InfiniteScroll extends Component<Props, State> {
257
255
) ;
258
256
}
259
257
260
- onScrollListener ( event : MouseEvent ) {
261
- if ( typeof this . props . onScroll === " function" ) {
258
+ onScrollListener = ( event : MouseEvent ) => {
259
+ if ( typeof this . props . onScroll === ' function' ) {
262
260
// Execute this callback in next tick so that it does not affect the
263
261
// functionality of the library.
264
262
setTimeout ( ( ) => this . props . onScroll && this . props . onScroll ( event ) , 0 ) ;
265
263
}
266
264
267
- let target =
265
+ const target =
268
266
this . props . height || this . _scrollableNode
269
267
? ( event . target as HTMLElement )
270
268
: document . documentElement . scrollTop
@@ -275,7 +273,7 @@ export default class InfiniteScroll extends Component<Props, State> {
275
273
// prevents multiple triggers.
276
274
if ( this . actionTriggered ) return ;
277
275
278
- let atBottom = this . isElementAtBottom ( target , this . props . scrollThreshold ) ;
276
+ const atBottom = this . isElementAtBottom ( target , this . props . scrollThreshold ) ;
279
277
280
278
// call the `next` function in the props to trigger the next data fetch
281
279
if ( atBottom && this . props . hasMore ) {
@@ -285,14 +283,14 @@ export default class InfiniteScroll extends Component<Props, State> {
285
283
}
286
284
287
285
this . lastScrollTop = target . scrollTop ;
288
- }
286
+ } ;
289
287
290
288
render ( ) {
291
289
const style = {
292
- height : this . props . height || " auto" ,
293
- overflow : " auto" ,
294
- WebkitOverflowScrolling : " touch" ,
295
- ...this . props . style
290
+ height : this . props . height || ' auto' ,
291
+ overflow : ' auto' ,
292
+ WebkitOverflowScrolling : ' touch' ,
293
+ ...this . props . style ,
296
294
} as CSSProperties ;
297
295
const hasChildren =
298
296
this . props . hasChildren ||
@@ -306,26 +304,26 @@ export default class InfiniteScroll extends Component<Props, State> {
306
304
// on drag down as overflow becomes visible
307
305
const outerDivStyle =
308
306
this . props . pullDownToRefresh && this . props . height
309
- ? { overflow : " auto" }
307
+ ? { overflow : ' auto' }
310
308
: { } ;
311
309
return (
312
310
< div style = { outerDivStyle } >
313
311
< div
314
- className = { `infinite-scroll-component ${ this . props . className || "" } ` }
312
+ className = { `infinite-scroll-component ${ this . props . className || '' } ` }
315
313
ref = { ( infScroll : HTMLDivElement ) => ( this . _infScroll = infScroll ) }
316
314
style = { style }
317
315
>
318
316
{ this . props . pullDownToRefresh && (
319
317
< div
320
- style = { { position : " relative" } }
318
+ style = { { position : ' relative' } }
321
319
ref = { ( pullDown : HTMLDivElement ) => ( this . _pullDown = pullDown ) }
322
320
>
323
321
< div
324
322
style = { {
325
- position : " absolute" ,
323
+ position : ' absolute' ,
326
324
left : 0 ,
327
325
right : 0 ,
328
- top : - 1 * this . maxPullDownDistance
326
+ top : - 1 * this . maxPullDownDistance ,
329
327
} }
330
328
>
331
329
{ this . state . pullToRefreshThresholdBreached
0 commit comments