Skip to content

Commit 2deea2e

Browse files
committed
Add ESlint and prettier
1 parent aa40f2a commit 2deea2e

File tree

9 files changed

+480
-73
lines changed

9 files changed

+480
-73
lines changed

.eslintrc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const path = require('path');
2+
module.exports = {
3+
parser: '@typescript-eslint/parser',
4+
parserOptions: {
5+
project: path.resolve(__dirname, './tsconfig.json'),
6+
tsconfigRootDir: __dirname,
7+
},
8+
plugins: ['@typescript-eslint'],
9+
extends: [
10+
'eslint:recommended',
11+
'plugin:@typescript-eslint/eslint-recommended',
12+
'plugin:react/recommended',
13+
'plugin:@typescript-eslint/recommended',
14+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
15+
'prettier',
16+
'prettier/@typescript-eslint',
17+
'prettier/react',
18+
],
19+
};

.prettierrc.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trailingComma = "es5"
2+
tabWidth = 2
3+
semi = true
4+
singleQuote = true

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
"scripts": {
1010
"build": "microbundle",
1111
"storybook": "start-storybook -p 6006",
12-
"build-storybook": "build-storybook"
12+
"build-storybook": "build-storybook",
13+
"lint": "eslint 'src/**/*.{ts,tsx,js,jsx}'",
14+
"lint:fix": "yarn lint --fix",
15+
"prettier:check": "prettier --check 'src/**/*'",
16+
"prettify": "prettier --write 'src/**/*'"
1317
},
1418
"repository": {
1519
"type": "git",
@@ -43,9 +47,15 @@
4347
"@types/react-dom": "^16.9.1",
4448
"@types/storybook__react": "^4.0.2",
4549
"@types/throttle-debounce": "^2.1.0",
50+
"@typescript-eslint/eslint-plugin": "^2.3.2",
51+
"@typescript-eslint/parser": "^2.3.2",
4652
"awesome-typescript-loader": "^5.2.1",
4753
"babel-loader": "^8.0.6",
54+
"eslint": "^6.5.1",
55+
"eslint-config-prettier": "^6.3.0",
56+
"eslint-plugin-react": "^7.15.0",
4857
"microbundle": "^0.11.0",
58+
"prettier": "1.18.2",
4959
"react": "^16.10.1",
5060
"react-docgen-typescript-loader": "^3.2.1",
5161
"react-dom": "^16.10.1",

src/index.tsx

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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';
44

55
type Fn = () => any;
66
interface Props {
@@ -37,7 +37,7 @@ export default class InfiniteScroll extends Component<Props, State> {
3737

3838
this.state = {
3939
showLoader: false,
40-
pullToRefreshThresholdBreached: false
40+
pullToRefreshThresholdBreached: false,
4141
};
4242

4343
this.onScrollListener = this.onScrollListener.bind(this);
@@ -59,13 +59,13 @@ export default class InfiniteScroll extends Component<Props, State> {
5959
private _pullDown: HTMLDivElement | undefined;
6060

6161
// variables to keep track of pull down behaviour
62-
private startY: number = 0;
63-
private currentY: number = 0;
64-
private dragging: boolean = false;
62+
private startY = 0;
63+
private currentY = 0;
64+
private dragging = false;
6565

6666
// will be populated in componentDidMount
6767
// based on the height of the pull down element
68-
private maxPullDownDistance: number = 0;
68+
private maxPullDownDistance = 0;
6969

7070
componentDidMount() {
7171
this._scrollableNode = this.getScrollableTarget();
@@ -74,13 +74,13 @@ export default class InfiniteScroll extends Component<Props, State> {
7474
: this._scrollableNode || window;
7575

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

8282
if (
83-
typeof this.props.initialScrollY === "number" &&
83+
typeof this.props.initialScrollY === 'number' &&
8484
this.el &&
8585
this.el instanceof HTMLElement &&
8686
this.el.scrollHeight > this.props.initialScrollY
@@ -89,13 +89,13 @@ export default class InfiniteScroll extends Component<Props, State> {
8989
}
9090

9191
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);
92+
this.el.addEventListener('touchstart', this.onStart);
93+
this.el.addEventListener('touchmove', this.onMove);
94+
this.el.addEventListener('touchend', this.onEnd);
9595

96-
this.el.addEventListener("mousedown", this.onStart);
97-
this.el.addEventListener("mousemove", this.onMove);
98-
this.el.addEventListener("mouseup", this.onEnd);
96+
this.el.addEventListener('mousedown', this.onStart);
97+
this.el.addEventListener('mousemove', this.onMove);
98+
this.el.addEventListener('mouseup', this.onEnd);
9999

100100
// get BCR of pullDown element to position it above
101101
this.maxPullDownDistance =
@@ -106,7 +106,7 @@ export default class InfiniteScroll extends Component<Props, State> {
106106
0;
107107
this.forceUpdate();
108108

109-
if (typeof this.props.refreshFunction !== "function") {
109+
if (typeof this.props.refreshFunction !== 'function') {
110110
throw new Error(
111111
`Mandatory prop "refreshFunction" missing.
112112
Pull Down To Refresh functionality will not work
@@ -118,18 +118,18 @@ export default class InfiniteScroll extends Component<Props, State> {
118118

119119
componentWillUnmount() {
120120
if (this.el) {
121-
this.el.removeEventListener("scroll", e =>
121+
this.el.removeEventListener('scroll', e =>
122122
this.throttledOnScrollListener(e as MouseEvent)
123123
);
124124

125125
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);
126+
this.el.removeEventListener('touchstart', this.onStart);
127+
this.el.removeEventListener('touchmove', this.onMove);
128+
this.el.removeEventListener('touchend', this.onEnd);
129129

130-
this.el.removeEventListener("mousedown", this.onStart);
131-
this.el.removeEventListener("mousemove", this.onMove);
132-
this.el.removeEventListener("mouseup", this.onEnd);
130+
this.el.removeEventListener('mousedown', this.onStart);
131+
this.el.removeEventListener('mousemove', this.onMove);
132+
this.el.removeEventListener('mouseup', this.onEnd);
133133
}
134134
}
135135
}
@@ -146,14 +146,14 @@ export default class InfiniteScroll extends Component<Props, State> {
146146
// update state when new data was sent in
147147
this.setState({
148148
showLoader: false,
149-
pullToRefreshThresholdBreached: false
149+
pullToRefreshThresholdBreached: false,
150150
});
151151
}
152152

153153
getScrollableTarget() {
154154
if (this.props.scrollableTarget instanceof HTMLElement)
155155
return this.props.scrollableTarget;
156-
if (typeof this.props.scrollableTarget === "string") {
156+
if (typeof this.props.scrollableTarget === 'string') {
157157
return document.getElementById(this.props.scrollableTarget);
158158
}
159159
if (this.props.scrollableTarget === null) {
@@ -178,7 +178,7 @@ export default class InfiniteScroll extends Component<Props, State> {
178178
this.currentY = this.startY;
179179

180180
if (this._infScroll) {
181-
this._infScroll.style.willChange = "transform";
181+
this._infScroll.style.willChange = 'transform';
182182
this._infScroll.style.transition = `transform 0.2s cubic-bezier(0,0,0.31,1)`;
183183
}
184184
};
@@ -200,15 +200,15 @@ export default class InfiniteScroll extends Component<Props, State> {
200200
Number(this.props.pullDownToRefreshThreshold)
201201
) {
202202
this.setState({
203-
pullToRefreshThresholdBreached: true
203+
pullToRefreshThresholdBreached: true,
204204
});
205205
}
206206

207207
// so you can drag upto 1.5 times of the maxPullDownDistance
208208
if (this.currentY - this.startY > this.maxPullDownDistance * 1.5) return;
209209

210210
if (this._infScroll) {
211-
this._infScroll.style.overflow = "visible";
211+
this._infScroll.style.overflow = 'visible';
212212
this._infScroll.style.transform = `translate3d(0px, ${this.currentY -
213213
this.startY}px, 0px)`;
214214
}
@@ -227,9 +227,9 @@ export default class InfiniteScroll extends Component<Props, State> {
227227
requestAnimationFrame(() => {
228228
// this._infScroll
229229
if (this._infScroll) {
230-
this._infScroll.style.overflow = "auto";
231-
this._infScroll.style.transform = "none";
232-
this._infScroll.style.willChange = "none";
230+
this._infScroll.style.overflow = 'auto';
231+
this._infScroll.style.transform = 'none';
232+
this._infScroll.style.willChange = 'none';
233233
}
234234
});
235235
};
@@ -258,13 +258,13 @@ export default class InfiniteScroll extends Component<Props, State> {
258258
}
259259

260260
onScrollListener(event: MouseEvent) {
261-
if (typeof this.props.onScroll === "function") {
261+
if (typeof this.props.onScroll === 'function') {
262262
// Execute this callback in next tick so that it does not affect the
263263
// functionality of the library.
264264
setTimeout(() => this.props.onScroll && this.props.onScroll(event), 0);
265265
}
266266

267-
let target =
267+
const target =
268268
this.props.height || this._scrollableNode
269269
? (event.target as HTMLElement)
270270
: document.documentElement.scrollTop
@@ -275,7 +275,7 @@ export default class InfiniteScroll extends Component<Props, State> {
275275
// prevents multiple triggers.
276276
if (this.actionTriggered) return;
277277

278-
let atBottom = this.isElementAtBottom(target, this.props.scrollThreshold);
278+
const atBottom = this.isElementAtBottom(target, this.props.scrollThreshold);
279279

280280
// call the `next` function in the props to trigger the next data fetch
281281
if (atBottom && this.props.hasMore) {
@@ -289,10 +289,10 @@ export default class InfiniteScroll extends Component<Props, State> {
289289

290290
render() {
291291
const style = {
292-
height: this.props.height || "auto",
293-
overflow: "auto",
294-
WebkitOverflowScrolling: "touch",
295-
...this.props.style
292+
height: this.props.height || 'auto',
293+
overflow: 'auto',
294+
WebkitOverflowScrolling: 'touch',
295+
...this.props.style,
296296
} as CSSProperties;
297297
const hasChildren =
298298
this.props.hasChildren ||
@@ -306,26 +306,26 @@ export default class InfiniteScroll extends Component<Props, State> {
306306
// on drag down as overflow becomes visible
307307
const outerDivStyle =
308308
this.props.pullDownToRefresh && this.props.height
309-
? { overflow: "auto" }
309+
? { overflow: 'auto' }
310310
: {};
311311
return (
312312
<div style={outerDivStyle}>
313313
<div
314-
className={`infinite-scroll-component ${this.props.className || ""}`}
314+
className={`infinite-scroll-component ${this.props.className || ''}`}
315315
ref={(infScroll: HTMLDivElement) => (this._infScroll = infScroll)}
316316
style={style}
317317
>
318318
{this.props.pullDownToRefresh && (
319319
<div
320-
style={{ position: "relative" }}
320+
style={{ position: 'relative' }}
321321
ref={(pullDown: HTMLDivElement) => (this._pullDown = pullDown)}
322322
>
323323
<div
324324
style={{
325-
position: "absolute",
325+
position: 'absolute',
326326
left: 0,
327327
right: 0,
328-
top: -1 * this.maxPullDownDistance
328+
top: -1 * this.maxPullDownDistance,
329329
}}
330330
>
331331
{this.state.pullToRefreshThresholdBreached

src/stories/WindowInfiniteScrollComponent.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from "react";
2-
import InfiniteScroll from "../index";
1+
import React from 'react';
2+
import InfiniteScroll from '../index';
33
type State = {
44
data: number[];
55
};
@@ -8,7 +8,7 @@ export default class WindowInfiniteScrollComponent extends React.Component<
88
State
99
> {
1010
state = {
11-
data: new Array(100).fill(1)
11+
data: new Array(100).fill(1),
1212
};
1313

1414
next = () => {
@@ -27,7 +27,7 @@ export default class WindowInfiniteScrollComponent extends React.Component<
2727
dataLength={this.state.data.length}
2828
>
2929
{this.state.data.map((_, i) => (
30-
<div style={{ height: 30, margin: 4, border: "1px solid hotpink" }}>
30+
<div style={{ height: 30, margin: 4, border: '1px solid hotpink' }}>
3131
#{i + 1} row
3232
</div>
3333
))}

src/stories/stories.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as React from "react";
2-
import { storiesOf } from "@storybook/react";
1+
import * as React from 'react';
2+
import { storiesOf } from '@storybook/react';
33

4-
import WindowInf from "./WindowInfiniteScrollComponent";
5-
const stories = storiesOf("Components", module);
4+
import WindowInf from './WindowInfiniteScrollComponent';
5+
const stories = storiesOf('Components', module);
66

7-
stories.add("InfiniteScroll", () => <WindowInf />, {
8-
info: { inline: true }
7+
stories.add('InfiniteScroll', () => <WindowInf />, {
8+
info: { inline: true },
99
});

src/utils/threshold.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const defaultThreshold = {
99
};
1010

1111
export function parseThreshold(scrollThreshold: string | number) {
12-
if (typeof scrollThreshold === "number") {
12+
if (typeof scrollThreshold === 'number') {
1313
return {
1414
unit: ThresholdUnits.Percent,
1515
value: scrollThreshold * 100,
1616
};
1717
}
1818

19-
if (typeof scrollThreshold === "string") {
19+
if (typeof scrollThreshold === 'string') {
2020
if (scrollThreshold.match(/^(\d*(\.\d+)?)px$/)) {
2121
return {
2222
unit: ThresholdUnits.Pixel,
@@ -31,7 +31,9 @@ export function parseThreshold(scrollThreshold: string | number) {
3131
};
3232
}
3333

34-
console.warn('scrollThreshold format is invalid. Valid formats: "120px", "50%"...');
34+
console.warn(
35+
'scrollThreshold format is invalid. Valid formats: "120px", "50%"...'
36+
);
3537

3638
return defaultThreshold;
3739
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@
5656
/* Experimental Options */
5757
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
5858
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
59-
}
59+
},
60+
"include": ["src/**/*"]
6061
}

0 commit comments

Comments
 (0)