1- import React , { Component } from "react" ;
1+ import React , { Component , ReactNode , CSSProperties } from "react" ;
22import PropTypes from "prop-types" ;
33import throttle from "./utils/throttle" ;
44import { 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+ }
525
6- export default class InfiniteScroll extends Component {
7- constructor ( props ) {
8- super ( props ) ;
26+ interface State {
27+ showLoader : boolean ;
28+ pullToRefreshThresholdBreached : boolean ;
29+ }
930
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 ) ;
1235
1336 this . state = {
1437 showLoader : false ,
1538 pullToRefreshThresholdBreached : false
1639 } ;
1740
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-
2741 this . onScrollListener = this . onScrollListener . bind ( this ) ;
2842 this . throttledOnScrollListener = throttle ( this . onScrollListener , 150 ) . bind (
2943 this
@@ -34,6 +48,17 @@ export default class InfiniteScroll extends Component {
3448 this . getScrollableTarget = this . getScrollableTarget . bind ( this ) ;
3549 }
3650
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 ;
3762 componentDidMount ( ) {
3863 this . _scrollableNode = this . getScrollableTarget ( ) ;
3964 this . el = this . props . height
@@ -87,7 +112,11 @@ export default class InfiniteScroll extends Component {
87112
88113 componentWillReceiveProps ( props ) {
89114 // 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 ;
91120
92121 this . actionTriggered = false ;
93122 // update state when new data was sent in
@@ -98,8 +127,9 @@ export default class InfiniteScroll extends Component {
98127 }
99128
100129 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" ) {
103133 return document . getElementById ( this . props . scrollableTarget ) ;
104134 }
105135 if ( this . props . scrollableTarget === null ) {
@@ -156,9 +186,9 @@ export default class InfiniteScroll extends Component {
156186 requestAnimationFrame ( ( ) => {
157187 // this._infScroll
158188 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" ;
162192 }
163193 } ) ;
164194 }
@@ -178,7 +208,8 @@ export default class InfiniteScroll extends Component {
178208 }
179209
180210 return (
181- target . scrollTop + clientHeight >= threshold . value / 100 * target . scrollHeight
211+ target . scrollTop + clientHeight >=
212+ ( threshold . value / 100 ) * target . scrollHeight
182213 ) ;
183214 }
184215
@@ -193,8 +224,8 @@ export default class InfiniteScroll extends Component {
193224 this . props . height || this . _scrollableNode
194225 ? event . target
195226 : document . documentElement . scrollTop
196- ? document . documentElement
197- : document . body ;
227+ ? document . documentElement
228+ : document . body ;
198229
199230 // return immediately if the action has already been triggered,
200231 // prevents multiple triggers.
@@ -232,7 +263,7 @@ export default class InfiniteScroll extends Component {
232263 return (
233264 < div style = { outerDivStyle } >
234265 < div
235- className = { `infinite-scroll-component ${ this . props . className || '' } ` }
266+ className = { `infinite-scroll-component ${ this . props . className || "" } ` }
236267 ref = { infScroll => ( this . _infScroll = infScroll ) }
237268 style = { style }
238269 >
0 commit comments