Skip to content

Commit cf135e9

Browse files
authored
Merge pull request #145 from rajatsan/add-stories
Adds stories
2 parents ab13fbb + 229f1f4 commit cf135e9

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import { render } from 'react-dom';
3+
import InfiniteScroll from '../index';
4+
5+
const style = {
6+
height: 30,
7+
border: '1px solid green',
8+
margin: 6,
9+
padding: 8,
10+
};
11+
12+
export default class App extends React.Component {
13+
state = {
14+
items: Array.from({ length: 20 }),
15+
hasMore: true,
16+
};
17+
18+
fetchMoreData = () => {
19+
if (this.state.items.length >= 500) {
20+
this.setState({ hasMore: false });
21+
return;
22+
}
23+
// a fake async api call like which sends
24+
// 20 more records in .5 secs
25+
setTimeout(() => {
26+
this.setState({
27+
items: this.state.items.concat(Array.from({ length: 20 })),
28+
});
29+
}, 500);
30+
};
31+
32+
render() {
33+
return (
34+
<div>
35+
<h1>demo: Infinite Scroll with fixed height</h1>
36+
<hr />
37+
<InfiniteScroll
38+
dataLength={this.state.items.length}
39+
next={this.fetchMoreData}
40+
hasMore={this.state.hasMore}
41+
loader={<h4>Loading...</h4>}
42+
height={400}
43+
endMessage={
44+
<p style={{ textAlign: 'center' }}>
45+
<b>Yay! You have seen it all</b>
46+
</p>
47+
}
48+
>
49+
{this.state.items.map((_, index) => (
50+
<div style={style} key={index}>
51+
div - #{index}
52+
</div>
53+
))}
54+
</InfiniteScroll>
55+
</div>
56+
);
57+
}
58+
}
59+
60+
render(<App />, document.getElementById('root'));
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import { render } from 'react-dom';
3+
import InfiniteScroll from '../index';
4+
5+
const style = {
6+
height: 30,
7+
border: '1px solid green',
8+
margin: 6,
9+
padding: 8,
10+
};
11+
12+
export default class App extends React.Component {
13+
state = {
14+
items: Array.from({ length: 20 }),
15+
};
16+
17+
fetchMoreData = () => {
18+
// a fake async api call like which sends
19+
// 20 more records in 1.5 secs
20+
setTimeout(() => {
21+
this.setState({
22+
items: this.state.items.concat(Array.from({ length: 20 })),
23+
});
24+
}, 1500);
25+
};
26+
27+
render() {
28+
return (
29+
<div>
30+
<h1>demo: Pull down to refresh</h1>
31+
<hr />
32+
<InfiniteScroll
33+
dataLength={this.state.items.length}
34+
next={this.fetchMoreData}
35+
hasMore={true}
36+
loader={<h4>Loading...</h4>}
37+
pullDownToRefresh
38+
pullDownToRefreshContent={
39+
<h3 style={{ textAlign: 'center' }}>
40+
&#8595; Pull down to refresh
41+
</h3>
42+
}
43+
releaseToRefreshContent={
44+
<h3 style={{ textAlign: 'center' }}>&#8593; Release to refresh</h3>
45+
}
46+
refreshFunction={this.fetchMoreData}
47+
>
48+
{this.state.items.map((_, index) => (
49+
<div style={style} key={index}>
50+
div - #{index}
51+
</div>
52+
))}
53+
</InfiniteScroll>
54+
</div>
55+
);
56+
}
57+
}
58+
59+
render(<App />, document.getElementById('root'));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
import { render } from 'react-dom';
3+
import InfiniteScroll from '../index';
4+
5+
const style = {
6+
height: 30,
7+
border: '1px solid green',
8+
margin: 6,
9+
padding: 8,
10+
};
11+
12+
export default class App extends React.Component {
13+
state = {
14+
items: Array.from({ length: 20 }),
15+
};
16+
17+
fetchMoreData = () => {
18+
// a fake async api call like which sends
19+
// 20 more records in 1.5 secs
20+
setTimeout(() => {
21+
this.setState({
22+
items: this.state.items.concat(Array.from({ length: 20 })),
23+
});
24+
}, 1500);
25+
};
26+
27+
render() {
28+
return (
29+
<div>
30+
<h1>demo: Infinite Scroll with scrollable target</h1>
31+
<hr />
32+
<div id="scrollableDiv" style={{ height: 300, overflow: 'auto' }}>
33+
<InfiniteScroll
34+
dataLength={this.state.items.length}
35+
next={this.fetchMoreData}
36+
hasMore={true}
37+
loader={<h4>Loading...</h4>}
38+
scrollableTarget="scrollableDiv"
39+
>
40+
{this.state.items.map((_, index) => (
41+
<div style={style} key={index}>
42+
div - #{index}
43+
</div>
44+
))}
45+
</InfiniteScroll>
46+
</div>
47+
</div>
48+
);
49+
}
50+
}
51+
52+
render(<App />, document.getElementById('root'));

src/stories/stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,27 @@ import * as React from 'react';
22
import { storiesOf } from '@storybook/react';
33

44
import WindowInf from './WindowInfiniteScrollComponent';
5+
import PullDownToRefreshInfScroll from './PullDownToRefreshInfScroll';
6+
import InfiniteScrollWithHeight from './InfiniteScrollWithHeight';
7+
import ScrollableTargetInfiniteScroll from './ScrollableTargetInfScroll';
58
const stories = storiesOf('Components', module);
69

710
stories.add('InfiniteScroll', () => <WindowInf />, {
811
info: { inline: true },
912
});
13+
14+
stories.add('PullDownToRefresh', () => <PullDownToRefreshInfScroll />, {
15+
info: { inline: true },
16+
});
17+
18+
stories.add('InfiniteScrollWithHeight', () => <InfiniteScrollWithHeight />, {
19+
info: { inline: true },
20+
});
21+
22+
stories.add(
23+
'ScrollableTargetInfiniteScroll',
24+
() => <ScrollableTargetInfiniteScroll />,
25+
{
26+
info: { inline: true },
27+
}
28+
);

0 commit comments

Comments
 (0)