Skip to content

Commit dbc5646

Browse files
committed
feat: 🎸 add polling to scroll sensor and infinite scroll
1 parent 368ef7f commit dbc5646

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

‎src/InfiniteScroll/__story__/story.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,46 @@ class Demo extends React.Component {
5151
}
5252
}
5353

54+
class Demo2 extends React.Component {
55+
state = {
56+
items: [
57+
<Block key={0} />,
58+
<Block key={1} />,
59+
<Block key={2} />,
60+
<Block key={3} />,
61+
<Block key={4} />,
62+
],
63+
cursor: 1,
64+
};
65+
66+
constructor (props) {
67+
super(props);
68+
}
69+
70+
load = (cnt = 5) => {
71+
console.log('loading for cursor: ' + this.state.cursor);
72+
const items = [...this.state.items];
73+
for (let i = 0; i < cnt; i++) {
74+
items.push(<Block key={items.length} />);
75+
}
76+
this.setState({
77+
items,
78+
cursor: this.state.cursor + 1,
79+
});
80+
};
81+
82+
render () {
83+
return (
84+
<div style={{border: '1px solid red', height: 400, width: 300, overflowY: 'scroll'}}>
85+
<InfiniteScroll hasMore={this.state.cursor < 5} loadMore={this.load} cursor={this.state.cursor} poll={500}>
86+
{this.state.items}
87+
</InfiniteScroll>
88+
</div>
89+
);
90+
}
91+
}
92+
5493
storiesOf('UI/InfiniteScroll', module)
5594
.add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/InfiniteScroll.md')}))
5695
.add('Example', () => <Demo />)
96+
.add('Example in <div>', () => <Demo2 />)

‎src/InfiniteScroll/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface InfiniteScrollProps {
99
sentinel?: React.ReactElement<any>;
1010
hasMore?: boolean;
1111
margin?: number;
12+
poll?: number;
1213
loadMore: () => void;
1314
}
1415

@@ -35,11 +36,11 @@ export class InfiniteScroll extends React.Component<InfiniteScrollProps, Infinit
3536

3637
render () {
3738
const {props} = this;
38-
const {children, hasMore, sentinel, margin} = props;
39+
const {children, hasMore, sentinel, margin, poll} = props;
3940
return h(React.Fragment, null,
4041
children,
4142
hasMore &&
42-
h(ViewportScrollSensor, {margin: [0, 0, margin, 0], onChange: this.onViewportChange}, sentinel),
43+
h(ViewportScrollSensor, {margin: [0, 0, margin, 0], poll, onChange: this.onViewportChange}, sentinel),
4344
);
4445
}
4546
}

‎src/ViewportObserverSensor/__story__/StoryViewportSensorConf.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const StoryViewportSensorConf = ({sensor: Sensor, onChange, threshold, margin=[0
1313
threshold={threshold}
1414
margin={margin}
1515
onChange={onChange}
16+
poll={0}
1617
>{(state) =>
1718
<div style={{
1819
width: 300,

‎src/ViewportScrollSensor/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface IViewportScrollSensorProps extends IUniversalInterfaceProps<IVi
4949
margin?: TMargin;
5050
threshold?: number;
5151
throttle?: number;
52+
poll?: number;
5253
onChange?: (state: IViewportScrollSensorState) => void;
5354
}
5455

@@ -65,6 +66,7 @@ export class ViewportScrollSensor extends React.Component<IViewportScrollSensorP
6566

6667
mounted: boolean = false;
6768
el: HTMLElement;
69+
pollTimer;
6870

6971
state: IViewportScrollSensorState = {
7072
visible: false
@@ -81,15 +83,26 @@ export class ViewportScrollSensor extends React.Component<IViewportScrollSensorP
8183
on(document, 'scroll', this.onScroll);
8284
on(window, 'resize', this.onScroll);
8385
this.onScroll();
86+
if (this.props.poll) {
87+
setTimeout(this.poll, this.props.poll);
88+
}
8489
}
8590

8691
componentWillUnmount () {
8792
this.mounted = false;
8893

94+
clearTimeout(this.pollTimer);
8995
off(document, 'scroll', this.onScroll);
9096
off(window, 'resize', this.onScroll);
9197
}
9298

99+
poll = () => {
100+
if (this.mounted) {
101+
this.onScroll();
102+
setTimeout(this.poll, this.props.poll);
103+
}
104+
};
105+
93106
onCalculation (visible, rectRoot: TRect, rectEl: TRect, rectIntersection: TRect) {
94107
if (visible !== this.state.visible) {
95108
const state = {

0 commit comments

Comments
 (0)