1
1
import React , { Component , ReactNode , CSSProperties } from "react" ;
2
- import throttle from "./utils/ throttle" ;
2
+ import { throttle } from ' throttle-debounce' ;
3
3
import { ThresholdUnits , parseThreshold } from "./utils/threshold" ;
4
4
5
5
type Fn = ( ) => any ;
6
6
interface Props {
7
- next : Fn ;
8
- hasMore : boolean ;
9
- children : ReactNode ;
7
+ next ? : Fn ;
8
+ hasMore ? : boolean ;
9
+ children ? : ReactNode ;
10
10
loader : ReactNode ;
11
- scrollThreshold : number | string ;
12
- endMessage : ReactNode ;
13
- style : CSSProperties ;
14
- height : number ;
15
- scrollableTarget : ReactNode ;
16
- hasChildren : boolean ;
17
- pullDownToRefresh : boolean ;
18
- pullDownToRefreshContent : ReactNode ;
19
- releaseToRefreshContent : ReactNode ;
20
- pullDownToRefreshThreshold : number ;
21
- refreshFunction : Fn ;
22
- onScroll : ( e : MouseEvent ) => any ;
11
+ scrollThreshold ? : number | string ;
12
+ endMessage ? : ReactNode ;
13
+ style ? : CSSProperties ;
14
+ height ? : number ;
15
+ scrollableTarget ? : ReactNode ;
16
+ hasChildren ? : boolean ;
17
+ pullDownToRefresh ? : boolean ;
18
+ pullDownToRefreshContent ? : ReactNode ;
19
+ releaseToRefreshContent ? : ReactNode ;
20
+ pullDownToRefreshThreshold ? : number ;
21
+ refreshFunction ? : Fn ;
22
+ onScroll ? : ( e : MouseEvent ) => any ;
23
23
dataLength : number ;
24
- initialScrollY : number ;
25
- key : string ;
26
- className : string ;
24
+ initialScrollY ? : number ;
25
+ key ? : string ;
26
+ className ? : string ;
27
27
}
28
28
29
29
interface State {
@@ -41,7 +41,7 @@ export default class InfiniteScroll extends Component<Props, State> {
41
41
} ;
42
42
43
43
this . onScrollListener = this . onScrollListener . bind ( this ) ;
44
- this . throttledOnScrollListener = throttle ( this . onScrollListener , 150 ) . bind (
44
+ this . throttledOnScrollListener = throttle ( 150 , this . onScrollListener ) . bind (
45
45
this
46
46
) ;
47
47
this . onStart = this . onStart . bind ( this ) ;
@@ -50,7 +50,7 @@ export default class InfiniteScroll extends Component<Props, State> {
50
50
this . getScrollableTarget = this . getScrollableTarget . bind ( this ) ;
51
51
}
52
52
53
- private throttledOnScrollListener : ( ) => void ;
53
+ private throttledOnScrollListener : ( e : MouseEvent ) => void ;
54
54
private _scrollableNode : HTMLElement | undefined | null ;
55
55
private el : HTMLElement | undefined | Window & typeof globalThis ;
56
56
private _infScroll : HTMLDivElement | undefined ;
@@ -74,7 +74,7 @@ export default class InfiniteScroll extends Component<Props, State> {
74
74
: this . _scrollableNode || window ;
75
75
76
76
if ( this . el ) {
77
- this . el . addEventListener ( "scroll" , this . throttledOnScrollListener ) ;
77
+ this . el . addEventListener ( "scroll" , ( e ) => this . throttledOnScrollListener ( e as MouseEvent ) ) ;
78
78
}
79
79
80
80
if (
@@ -116,7 +116,7 @@ export default class InfiniteScroll extends Component<Props, State> {
116
116
117
117
componentWillUnmount ( ) {
118
118
if ( this . el ) {
119
- this . el . removeEventListener ( "scroll" , this . throttledOnScrollListener ) ;
119
+ this . el . removeEventListener ( "scroll" , ( e ) => this . throttledOnScrollListener ( e as MouseEvent ) ) ;
120
120
121
121
if ( this . props . pullDownToRefresh ) {
122
122
this . el . removeEventListener ( "touchstart" , this . onStart ) ;
@@ -191,7 +191,7 @@ export default class InfiniteScroll extends Component<Props, State> {
191
191
// user is scrolling down to up
192
192
if ( this . currentY < this . startY ) return ;
193
193
194
- if ( this . currentY - this . startY >= this . props . pullDownToRefreshThreshold ) {
194
+ if ( this . currentY - this . startY >= Number ( this . props . pullDownToRefreshThreshold ) ) {
195
195
this . setState ( {
196
196
pullToRefreshThresholdBreached : true
197
197
} ) ;
@@ -207,7 +207,7 @@ export default class InfiniteScroll extends Component<Props, State> {
207
207
}
208
208
} ;
209
209
210
- onEnd : EventListener = evt => {
210
+ onEnd : EventListener = ( ) => {
211
211
this . startY = 0 ;
212
212
this . currentY = 0 ;
213
213
@@ -254,7 +254,7 @@ export default class InfiniteScroll extends Component<Props, State> {
254
254
if ( typeof this . props . onScroll === "function" ) {
255
255
// Execute this callback in next tick so that it does not affect the
256
256
// functionality of the library.
257
- setTimeout ( ( ) => this . props . onScroll ( event ) , 0 ) ;
257
+ setTimeout ( ( ) => this . props . onScroll && this . props . onScroll ( event ) , 0 ) ;
258
258
}
259
259
260
260
let target =
@@ -274,7 +274,7 @@ export default class InfiniteScroll extends Component<Props, State> {
274
274
if ( atBottom && this . props . hasMore ) {
275
275
this . actionTriggered = true ;
276
276
this . setState ( { showLoader : true } ) ;
277
- this . props . next ( ) ;
277
+ this . props . next && this . props . next ( ) ;
278
278
}
279
279
280
280
this . lastScrollTop = target . scrollTop ;
0 commit comments