Skip to content

Commit aa40f2a

Browse files
authored
Merge pull request #134 from ankeetmaini/story
Adds storybook integration
2 parents 9232c6a + d5a148d commit aa40f2a

File tree

9 files changed

+6869
-176
lines changed

9 files changed

+6869
-176
lines changed

.storybook/addons.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '@storybook/addon-actions/register';
2+
import '@storybook/addon-links/register';

.storybook/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { configure } from "@storybook/react";
2+
3+
configure(require.context("../src", true, /\.?stories\.tsx$/), module);

.storybook/webpack.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = ({ config }) => {
2+
config.module.rules.push({
3+
test: /\.(ts|tsx)$/,
4+
use: [
5+
{
6+
loader: require.resolve("awesome-typescript-loader")
7+
},
8+
// Optional
9+
{
10+
loader: require.resolve("react-docgen-typescript-loader")
11+
}
12+
]
13+
});
14+
config.resolve.extensions.push(".ts", ".tsx");
15+
return config;
16+
};

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"module": "dist/index.mjs",
88
"unpkg": "dist/index.umd.js",
99
"scripts": {
10-
"build": "microbundle"
10+
"build": "microbundle",
11+
"storybook": "start-storybook -p 6006",
12+
"build-storybook": "build-storybook"
1113
},
1214
"repository": {
1315
"type": "git",
@@ -31,11 +33,22 @@
3133
"react": ">=16.0.0"
3234
},
3335
"devDependencies": {
36+
"@babel/core": "^7.6.2",
37+
"@storybook/addon-actions": "^5.2.1",
38+
"@storybook/addon-info": "^5.2.1",
39+
"@storybook/addon-links": "^5.2.1",
40+
"@storybook/addons": "^5.2.1",
41+
"@storybook/react": "^5.2.1",
3442
"@types/react": "^16.9.2",
3543
"@types/react-dom": "^16.9.1",
44+
"@types/storybook__react": "^4.0.2",
3645
"@types/throttle-debounce": "^2.1.0",
46+
"awesome-typescript-loader": "^5.2.1",
47+
"babel-loader": "^8.0.6",
3748
"microbundle": "^0.11.0",
38-
"react": "~15.5.4",
49+
"react": "^16.10.1",
50+
"react-docgen-typescript-loader": "^3.2.1",
51+
"react-dom": "^16.10.1",
3952
"typescript": "^3.6.3"
4053
},
4154
"dependencies": {

src/index.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { Component, ReactNode, CSSProperties } from "react";
2-
import { throttle } from 'throttle-debounce';
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;
1111
scrollThreshold?: number | string;
1212
endMessage?: ReactNode;
@@ -74,7 +74,9 @@ export default class InfiniteScroll extends Component<Props, State> {
7474
: this._scrollableNode || window;
7575

7676
if (this.el) {
77-
this.el.addEventListener("scroll", (e) => this.throttledOnScrollListener(e as MouseEvent));
77+
this.el.addEventListener("scroll", e =>
78+
this.throttledOnScrollListener(e as MouseEvent)
79+
);
7880
}
7981

8082
if (
@@ -116,7 +118,9 @@ export default class InfiniteScroll extends Component<Props, State> {
116118

117119
componentWillUnmount() {
118120
if (this.el) {
119-
this.el.removeEventListener("scroll", (e) => this.throttledOnScrollListener(e as MouseEvent));
121+
this.el.removeEventListener("scroll", e =>
122+
this.throttledOnScrollListener(e as MouseEvent)
123+
);
120124

121125
if (this.props.pullDownToRefresh) {
122126
this.el.removeEventListener("touchstart", this.onStart);
@@ -191,7 +195,10 @@ export default class InfiniteScroll extends Component<Props, State> {
191195
// user is scrolling down to up
192196
if (this.currentY < this.startY) return;
193197

194-
if (this.currentY - this.startY >= Number(this.props.pullDownToRefreshThreshold)) {
198+
if (
199+
this.currentY - this.startY >=
200+
Number(this.props.pullDownToRefreshThreshold)
201+
) {
195202
this.setState({
196203
pullToRefreshThresholdBreached: true
197204
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from "react";
2+
import InfiniteScroll from "../index";
3+
type State = {
4+
data: number[];
5+
};
6+
export default class WindowInfiniteScrollComponent extends React.Component<
7+
{},
8+
State
9+
> {
10+
state = {
11+
data: new Array(100).fill(1)
12+
};
13+
14+
next = () => {
15+
setTimeout(() => {
16+
const newData = [...this.state.data, new Array(100).fill(1)];
17+
this.setState({ data: newData });
18+
}, 2000);
19+
};
20+
render() {
21+
return (
22+
<>
23+
<InfiniteScroll
24+
hasMore={true}
25+
next={this.next}
26+
loader={<h1>Loading...</h1>}
27+
dataLength={this.state.data.length}
28+
>
29+
{this.state.data.map((_, i) => (
30+
<div style={{ height: 30, margin: 4, border: "1px solid hotpink" }}>
31+
#{i + 1} row
32+
</div>
33+
))}
34+
</InfiniteScroll>
35+
</>
36+
);
37+
}
38+
}

src/stories/stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as React from "react";
2+
import { storiesOf } from "@storybook/react";
3+
4+
import WindowInf from "./WindowInfiniteScrollComponent";
5+
const stories = storiesOf("Components", module);
6+
7+
stories.add("InfiniteScroll", () => <WindowInf />, {
8+
info: { inline: true }
9+
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
3838

3939
/* Module Resolution Options */
40-
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
40+
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
4141
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
4242
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
4343
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */

0 commit comments

Comments
 (0)