Skip to content

Commit 02916c2

Browse files
authored
Port to ts (#130)
Port to ts
2 parents 62e1f52 + 6be222e commit 02916c2

File tree

6 files changed

+2146
-1031
lines changed

6 files changed

+2146
-1031
lines changed

package.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,29 @@
3535
"react": ">=0.14.0"
3636
},
3737
"devDependencies": {
38-
"babel": "^5.8.23",
39-
"babel-eslint": "^4.1.3",
40-
"babel-loader": "^5.3.1",
38+
"@types/react-dom": "^16.9.1",
39+
"babel": "^6.23.0",
40+
"babel-core": "^6.26.3",
41+
"babel-eslint": "^10.0.3",
42+
"babel-loader": "7",
4143
"eslint-plugin-react": "^3.5.1",
4244
"html-loader": "^0.4.3",
43-
"html-webpack-plugin": "^2.10.0",
45+
"html-webpack-plugin": "^3.2.0",
4446
"prop-types": "~15.5.8",
4547
"react": "~15.5.4",
4648
"react-dom": "~15.5.4",
4749
"react-hot-loader": "^1.3.0",
50+
"ts-loader": "^6.1.2",
4851
"typescript": "^3.6.3",
49-
"webpack": "^1.10.1",
52+
"webpack": "^4.41.0",
53+
"webpack-cli": "^3.3.9",
5054
"webpack-dev-server": "^1.10.1",
5155
"webpack-hot-middleware": "^2.10.0"
5256
},
5357
"dependencies": {
5458
"@types/prop-types": "^15.7.2",
55-
"@types/react": "^16.9.2"
59+
"@types/react": "^16.9.2",
60+
"@types/throttle-debounce": "^2.1.0",
61+
"throttle-debounce": "^2.1.0"
5662
}
5763
}

src/index.tsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import React, { Component, ReactNode, CSSProperties } from "react";
2-
import throttle from "./utils/throttle";
2+
import { throttle } from 'throttle-debounce';
33
import { ThresholdUnits, parseThreshold } from "./utils/threshold";
44

55
type Fn = () => any;
66
interface Props {
7-
next: Fn;
8-
hasMore: boolean;
9-
children: ReactNode;
7+
next?: Fn;
8+
hasMore?: boolean;
9+
children?: ReactNode;
1010
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;
2323
dataLength: number;
24-
initialScrollY: number;
25-
key: string;
26-
className: string;
24+
initialScrollY?: number;
25+
key?: string;
26+
className?: string;
2727
}
2828

2929
interface State {
@@ -41,7 +41,7 @@ export default class InfiniteScroll extends Component<Props, State> {
4141
};
4242

4343
this.onScrollListener = this.onScrollListener.bind(this);
44-
this.throttledOnScrollListener = throttle(this.onScrollListener, 150).bind(
44+
this.throttledOnScrollListener = throttle(150, this.onScrollListener).bind(
4545
this
4646
);
4747
this.onStart = this.onStart.bind(this);
@@ -50,7 +50,7 @@ export default class InfiniteScroll extends Component<Props, State> {
5050
this.getScrollableTarget = this.getScrollableTarget.bind(this);
5151
}
5252

53-
private throttledOnScrollListener: () => void;
53+
private throttledOnScrollListener: (e: MouseEvent) => void;
5454
private _scrollableNode: HTMLElement | undefined | null;
5555
private el: HTMLElement | undefined | Window & typeof globalThis;
5656
private _infScroll: HTMLDivElement | undefined;
@@ -74,7 +74,7 @@ export default class InfiniteScroll extends Component<Props, State> {
7474
: this._scrollableNode || window;
7575

7676
if (this.el) {
77-
this.el.addEventListener("scroll", this.throttledOnScrollListener);
77+
this.el.addEventListener("scroll", (e) => this.throttledOnScrollListener(e as MouseEvent));
7878
}
7979

8080
if (
@@ -116,7 +116,7 @@ export default class InfiniteScroll extends Component<Props, State> {
116116

117117
componentWillUnmount() {
118118
if (this.el) {
119-
this.el.removeEventListener("scroll", this.throttledOnScrollListener);
119+
this.el.removeEventListener("scroll", (e) => this.throttledOnScrollListener(e as MouseEvent));
120120

121121
if (this.props.pullDownToRefresh) {
122122
this.el.removeEventListener("touchstart", this.onStart);
@@ -191,7 +191,7 @@ export default class InfiniteScroll extends Component<Props, State> {
191191
// user is scrolling down to up
192192
if (this.currentY < this.startY) return;
193193

194-
if (this.currentY - this.startY >= this.props.pullDownToRefreshThreshold) {
194+
if (this.currentY - this.startY >= Number(this.props.pullDownToRefreshThreshold)) {
195195
this.setState({
196196
pullToRefreshThresholdBreached: true
197197
});
@@ -207,7 +207,7 @@ export default class InfiniteScroll extends Component<Props, State> {
207207
}
208208
};
209209

210-
onEnd: EventListener = evt => {
210+
onEnd: EventListener = () => {
211211
this.startY = 0;
212212
this.currentY = 0;
213213

@@ -254,7 +254,7 @@ export default class InfiniteScroll extends Component<Props, State> {
254254
if (typeof this.props.onScroll === "function") {
255255
// Execute this callback in next tick so that it does not affect the
256256
// functionality of the library.
257-
setTimeout(() => this.props.onScroll(event), 0);
257+
setTimeout(() => this.props.onScroll && this.props.onScroll(event), 0);
258258
}
259259

260260
let target =
@@ -274,7 +274,7 @@ export default class InfiniteScroll extends Component<Props, State> {
274274
if (atBottom && this.props.hasMore) {
275275
this.actionTriggered = true;
276276
this.setState({ showLoader: true });
277-
this.props.next();
277+
this.props.next && this.props.next();
278278
}
279279

280280
this.lastScrollTop = target.scrollTop;

src/utils/threshold.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const defaultThreshold = {
88
value: 0.8,
99
};
1010

11-
export function parseThreshold(scrollThreshold) {
11+
export function parseThreshold(scrollThreshold: string | number) {
1212
if (typeof scrollThreshold === "number") {
1313
return {
1414
unit: ThresholdUnits.Percent,

src/utils/throttle.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

webpack.config.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@ var common = {
99
entry: {},
1010
output: {},
1111
module: {
12-
loaders: [
13-
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
14-
{ test: /\.css$/, exclude: /node_modules/, loader: 'style!css' }
12+
rules: [
13+
{ test: /\.js$/, exclude: /node_modules/, use: 'babel-loader' },
14+
{ test: /\.css$/, exclude: /node_modules/, use: 'style!css' },
15+
{ test: /\.tsx?$/, exclude: /node_modules/, use: 'ts-loader' },
1516
]
1617
},
1718
plugins: [
18-
new webpack.NoErrorsPlugin()
19-
]
19+
new webpack.NoEmitOnErrorsPlugin()
20+
],
21+
resolve: {
22+
modules: [
23+
path.join(__dirname, "src"),
24+
"node_modules"
25+
],
26+
extensions: ['.js', '.ts', '.tsx'],
27+
},
2028
};
2129

2230
var finalConf;
2331
if (isDevelopment) {
2432
finalConf = Object.assign({}, common);
25-
finalConf.entry.app = ['webpack-hot-middleware/client', './demos/index.js']; // HMR
33+
finalConf.mode = 'development';
34+
finalConf.entry.app = ['./demos/index.tsx']; // HMR
2635
finalConf.output = {
2736
path: BUILD_PATH,
2837
filename: 'demo.js'
@@ -31,13 +40,12 @@ if (isDevelopment) {
3140
finalConf.plugins = [...finalConf.plugins,
3241
new HtmlWebpackPlugin({
3342
template: './index.html'
34-
}),
35-
new webpack.optimize.OccurenceOrderPlugin(),
36-
new webpack.HotModuleReplacementPlugin()];
43+
})];
3744
} else {
3845
// production config
3946
finalConf = Object.assign({}, common);
40-
finalConf.entry.app = './app/index.js';
47+
finalConf.entry.app = './src/index.tsx';
48+
finalConf.mode = 'production';
4149
finalConf.output = {
4250
path: BUILD_PATH,
4351
filename: 'index.js',
@@ -54,8 +62,6 @@ if (isDevelopment) {
5462
}
5563
};
5664
finalConf.plugins = [...finalConf.plugins,
57-
new webpack.optimize.OccurrenceOrderPlugin(),
58-
new webpack.optimize.DedupePlugin(),
5965
new webpack.DefinePlugin({
6066
'process.env.NODE_ENV': '"production"'
6167
})

0 commit comments

Comments
 (0)