Repository navigation
Expand file tree
/
Copy pathInfiniteScroll.js
More file actions
150 lines (126 loc) · 3.71 KB
/
Copy pathInfiniteScroll.js
File metadata and controls
150 lines (126 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// @flow
import React, { Component } from 'react';
import type { ComponentType, Node } from 'react';
// $FlowFixMe
import createRef from 'create-react-ref/lib/createRef';
import type { ReactElementRef } from '../utils/types.js';
// internal components
import { withTheme } from './HOC/withTheme';
// utilities
import { composeTheme, addThemeId, didThemePropsChange } from '../utils/themes';
// constants
import { IDENTIFIERS } from '../themes/API';
type Props = {
className: string,
context: {
theme: Object,
ROOT_THEME_API: Object
},
fetchData: Function,
renderItems: Function,
skin: ComponentType<any>,
theme: Object, // will take precedence over theme in context if passed
themeId: string,
themeOverrides: Object,
threshold: number
};
type State = {
composedTheme: Object,
data: Object | Array<{}>,
error: boolean | string | Node,
hasMoreData: boolean,
isLoading: boolean
};
class InfiniteScrollBase extends Component<Props, State> {
// declare ref types
scrollContainer: ReactElementRef<typeof HTMLElement>;
// define static properties
static displayName = 'InfiniteScroll';
static defaultProps = {
fetchData() {},
theme: null,
themeId: IDENTIFIERS.INFINITE_SCROLL,
themeOverrides: {},
threshold: 250
};
constructor(props: Props) {
super(props);
const { context, themeId, theme, themeOverrides } = props;
// refs
this.scrollContainer = createRef();
this.state = {
composedTheme: composeTheme(
addThemeId(theme || context.theme, themeId),
addThemeId(themeOverrides, themeId),
context.ROOT_THEME_API
),
data: [],
error: false,
isLoading: false,
hasMoreData: true
};
}
componentWillMount() {
this._handleFetchData();
}
componentDidMount() {
const { scrollContainer } = this;
if (!scrollContainer.current) return;
scrollContainer.current.addEventListener('scroll', this._handleScroll);
}
componentWillReceiveProps(nextProps: Props) {
didThemePropsChange(this.props, nextProps, this.setState.bind(this));
}
// calls user's fetchData function from props
_handleFetchData = () => this.props.fetchData(this.setState.bind(this))
// scroll event listener attached to scrollContainer element
_handleScroll = () => {
const { error, isLoading, hasMoreData } = this.state;
// return early for error, loading, or lack of future data
if (error || isLoading || !hasMoreData) { return; }
return this._checkForScrollBottom();
}
// prevents new data fetch until user has scrolled near bottom of existing data
_checkForScrollBottom = () => {
const { scrollContainer, props: { threshold } } = this;
if (!scrollContainer.current) return;
const { offsetHeight, scrollTop, scrollHeight } = scrollContainer.current;
if (offsetHeight + scrollTop >= scrollHeight - threshold) {
return this._handleFetchData();
}
}
_isFunction = (renderProp: ?Function) => (renderProp && typeof renderProp === 'function')
render() {
const {
props: {
className,
renderItems,
skin: InfiniteScrollSkin,
themeId
},
state: {
composedTheme,
data,
error,
hasMoreData,
isLoading
},
scrollContainer
} = this;
if (!this._isFunction(renderItems)) { return null; }
return (
<InfiniteScrollSkin
className={className}
data={data}
error={error}
hasMoreData={hasMoreData}
isLoading={isLoading}
renderItems={renderItems}
scrollContainerRef={scrollContainer}
theme={composedTheme}
themeId={themeId}
/>
);
}
}
export const InfiniteScroll = withTheme(InfiniteScrollBase);