Skip to content

Commit 5e1d3bd

Browse files
authored
Add Prop, State interfaces (#127)
1 parent 1f2e1dd commit 5e1d3bd

File tree

5 files changed

+79
-26
lines changed

5 files changed

+79
-26
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dev": "NODE_ENV=development node server.js",
88
"build-demos": "NODE_ENV=development node_modules/.bin/webpack",
99
"test": "echo \"Error: no test specified\" && exit 1",
10-
"prepublish": "NODE_ENV=production node_modules/.bin/webpack --config webpack.config.js"
10+
"sprepublish": "NODE_ENV=production node_modules/.bin/webpack --config webpack.config.js"
1111
},
1212
"directories": {
1313
"demos": "demos"
@@ -49,5 +49,9 @@
4949
"webpack": "^1.10.1",
5050
"webpack-dev-server": "^1.10.1",
5151
"webpack-hot-middleware": "^2.10.0"
52+
},
53+
"dependencies": {
54+
"@types/prop-types": "^15.7.2",
55+
"@types/react": "^16.9.2"
5256
}
5357
}

app/index.js renamed to src/index.tsx

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
import React, { Component } from "react";
1+
import React, { Component, ReactNode, CSSProperties } from "react";
22
import PropTypes from "prop-types";
33
import throttle from "./utils/throttle";
44
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+
}
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
>
File renamed without changes.
File renamed without changes.

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
# yarn lockfile v1
33

44

5+
"@types/prop-types@*", "@types/prop-types@^15.7.2":
6+
version "15.7.2"
7+
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.2.tgz#0e58ae66773d7fd7c372a493aff740878ec9ceaa"
8+
integrity sha512-f8JzJNWVhKtc9dg/dyDNfliTKNOJSLa7Oht/ElZdF/UbMUmAH3rLmAk3ODNjw0mZajDEgatA03tRjB4+Dp/tzA==
9+
10+
"@types/react@^16.9.2":
11+
version "16.9.2"
12+
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.2.tgz#6d1765431a1ad1877979013906731aae373de268"
13+
integrity sha512-jYP2LWwlh+FTqGd9v7ynUKZzjj98T8x7Yclz479QdRhHfuW9yQ+0jjnD31eXSXutmBpppj5PYNLYLRfnZJvcfg==
14+
dependencies:
15+
"@types/prop-types" "*"
16+
csstype "^2.2.0"
17+
518
abbrev@1:
619
version "1.1.1"
720
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
@@ -778,6 +791,11 @@ [email protected]:
778791
resolved "https://registry.yarnpkg.com/css-what/-/css-what-2.1.3.tgz#a6d7604573365fe74686c3f311c56513d88285f2"
779792
integrity sha512-a+EPoD+uZiNfh+5fxw2nO9QwFa6nJe2Or35fGY6Ipw1R3R4AGz1d1TEZrCegvw2YTmZ0jXirGYlzxxpYSHwpEg==
780793

794+
csstype@^2.2.0:
795+
version "2.6.6"
796+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.6.tgz#c34f8226a94bbb10c32cc0d714afdf942291fc41"
797+
integrity sha512-RpFbQGUE74iyPgvr46U9t1xoQBM8T4BL8SxrN66Le2xYAPSaDJJKeztV3awugusb3g3G9iL8StmkBBXhcbbXhg==
798+
781799
date-now@^0.1.4:
782800
version "0.1.4"
783801
resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b"

0 commit comments

Comments
 (0)