-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathScrollPager.js
351 lines (309 loc) · 10.7 KB
/
ScrollPager.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const React = require('react')
const { Component } = React
const { ViewPropTypes } = ReactNative = require('react-native')
const createReactClass = require('create-react-class')
const PropTypes = require('prop-types')
const {
Dimensions,
View,
Animated,
ScrollView,
Platform,
StyleSheet,
InteractionManager,
} = ReactNative
const TimerMixin = require('react-timer-mixin')
import ViewPagerAndroid from "react-native-pager-view"
const SceneComponent = require('./SceneComponent')
const AnimatedViewPagerAndroid = Platform.OS === 'android' ?
Animated.createAnimatedComponent(ViewPagerAndroid) :
undefined
const ScrollableTabView = createReactClass({
mixins: [TimerMixin],
scrollOnMountCalled: false,
propTypes: {
initialPage: PropTypes.number,
page: PropTypes.number,
onChangeTab: PropTypes.func,
onScroll: PropTypes.func,
style: ViewPropTypes.style,
contentProps: PropTypes.object,
scrollWithoutAnimation: PropTypes.bool,
locked: PropTypes.bool,
prerenderingSiblingsNumber: PropTypes.number,
containerWidth: PropTypes.number,
},
getDefaultProps() {
return {
initialPage: 0,
page: -1,
onChangeTab: () => {},
onScroll: () => {},
contentProps: {},
scrollWithoutAnimation: false,
locked: false,
prerenderingSiblingsNumber: 0,
containerWidth: Dimensions.get('window').width,
}
},
getInitialState() {
let scrollValue
let scrollXIOS
let positionAndroid
let offsetAndroid
if (Platform.OS === 'ios') {
scrollXIOS = new Animated.Value(this.props.initialPage * this.props.containerWidth)
const containerWidthAnimatedValue = new Animated.Value(this.props.containerWidth)
// Need to call __makeNative manually to avoid a native animated bug. See
// https://github.com/facebook/react-native/pull/14435
containerWidthAnimatedValue.__makeNative()
scrollValue = Animated.divide(scrollXIOS, containerWidthAnimatedValue)
const callListeners = this._polyfillAnimatedValue(scrollValue)
scrollXIOS.addListener(({ value }) => callListeners(value / this.props.containerWidth) )
} else {
positionAndroid = new Animated.Value(this.props.initialPage)
offsetAndroid = new Animated.Value(0)
scrollValue = Animated.add(positionAndroid, offsetAndroid)
const callListeners = this._polyfillAnimatedValue(scrollValue)
let positionAndroidValue = this.props.initialPage
let offsetAndroidValue = 0
positionAndroid.addListener(({ value }) => {
positionAndroidValue = value
callListeners(positionAndroidValue + offsetAndroidValue)
})
offsetAndroid.addListener(({ value }) => {
offsetAndroidValue = value
callListeners(positionAndroidValue + offsetAndroidValue)
})
}
return {
currentPage: this.props.initialPage,
scrollValue,
scrollXIOS,
positionAndroid,
offsetAndroid,
sceneKeys: this.newSceneKeys({ currentPage: this.props.initialPage }),
}
},
componentWillReceiveProps(props) {
if (props.children !== this.props.children) {
this.updateSceneKeys({ page: this.state.currentPage, children: props.children })
}
if (props.page >= 0 && props.page !== this.state.currentPage) {
this.goToPage(props.page)
}
},
componentWillUnmount() {
if (Platform.OS === 'ios') {
this.state.scrollXIOS.removeAllListeners()
} else {
this.state.positionAndroid.removeAllListeners()
this.state.offsetAndroid.removeAllListeners()
}
},
goToPage(pageNumber) {
if (Platform.OS === 'ios') {
const offset = pageNumber * this.props.containerWidth
if (this.scrollView) {
this.scrollView.getNode().scrollTo({ x: offset, y: 0, animated: !this.props.scrollWithoutAnimation })
}
} else if (this.scrollView) {
if (this.props.scrollWithoutAnimation) {
this.scrollView.getNode().setPageWithoutAnimation(pageNumber)
} else {
this.scrollView.getNode().setPage(pageNumber)
}
}
const currentPage = this.state.currentPage
this.updateSceneKeys({
page: pageNumber,
callback: this._onChangeTab.bind(this, currentPage, pageNumber),
})
},
updateSceneKeys({ page, children = this.props.children, callback = () => {} }) {
const newKeys = this.newSceneKeys({ previousKeys: this.state.sceneKeys, currentPage: page, children })
this.setState({ currentPage: page, sceneKeys: newKeys }, callback)
},
newSceneKeys({ previousKeys = [], currentPage = 0, children = this.props.children }) {
const newKeys = []
this._children(children).forEach((child, idx) => {
const key = this._makeSceneKey(child, idx)
if (this._keyExists(previousKeys, key) ||
this._shouldRenderSceneKey(idx, currentPage)) {
newKeys.push(key)
}
})
return newKeys
},
// Animated.add and Animated.divide do not currently support listeners so
// we have to polyfill it here since a lot of code depends on being able
// to add a listener to `scrollValue`. See https://github.com/facebook/react-native/pull/12620.
_polyfillAnimatedValue(animatedValue) {
const listeners = new Set()
const addListener = (listener) => {
listeners.add(listener)
}
const removeListener = (listener) => {
listeners.delete(listener)
}
const removeAllListeners = () => {
listeners.clear()
}
animatedValue.addListener = addListener
animatedValue.removeListener = removeListener
animatedValue.removeAllListeners = removeAllListeners
return value => listeners.forEach(listener => listener({ value }))
},
_shouldRenderSceneKey(idx, currentPageKey) {
const numOfSibling = this.props.prerenderingSiblingsNumber
return (idx < (currentPageKey + numOfSibling + 1) &&
idx > (currentPageKey - numOfSibling - 1))
},
_keyExists(sceneKeys, key) {
return sceneKeys.find(sceneKey => key === sceneKey)
},
_makeSceneKey(child, idx) {
return `${child.props.tabLabel}_${ idx}`
},
renderScrollableContent() {
if (Platform.OS === 'ios') {
const scenes = this._composeScenes()
return (<Animated.ScrollView
horizontal
pagingEnabled
automaticallyAdjustContentInsets={false}
contentOffset={{ x: this.props.initialPage * this.props.containerWidth, }}
ref={(scrollView) => { this.scrollView = scrollView; }}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { x: this.state.scrollXIOS, }, }, }, ],
{ useNativeDriver: true, listener: this._onScroll, }
)}
onMomentumScrollBegin={this._onMomentumScrollBeginAndEnd}
onMomentumScrollEnd={this._onMomentumScrollBeginAndEnd}
scrollEventThrottle={16}
scrollsToTop={false}
showsHorizontalScrollIndicator={false}
scrollEnabled={!this.props.locked}
directionalLockEnabled
alwaysBounceVertical={false}
keyboardDismissMode="on-drag"
{...this.props.contentProps}
>
{scenes}
</Animated.ScrollView>);
}
const scenes = this._composeScenes()
return (<AnimatedViewPagerAndroid
key={this._children().length}
style={styles.scrollableContentAndroid}
initialPage={this.props.initialPage}
onPageSelected={this._updateSelectedPage}
keyboardDismissMode="on-drag"
scrollEnabled={!this.props.locked}
onPageScroll={Animated.event(
[{
nativeEvent: {
position: this.state.positionAndroid,
offset: this.state.offsetAndroid,
},
}, ],
{
useNativeDriver: true,
listener: this._onScroll,
},
)}
ref={(scrollView) => { this.scrollView = scrollView; }}
{...this.props.contentProps}
>
{scenes}
</AnimatedViewPagerAndroid>);
},
_composeScenes() {
return this._children().map((child, idx) => {
const key = this._makeSceneKey(child, idx)
return (<SceneComponent
key={child.key}
shouldUpdated={this._shouldRenderSceneKey(idx, this.state.currentPage)}
style={{ width: this.props.containerWidth }}
>
{this._keyExists(this.state.sceneKeys, key) ? child : <View tabLabel={child.props.tabLabel} />}
</SceneComponent>)
})
},
_onMomentumScrollBeginAndEnd(e) {
const offsetX = e.nativeEvent.contentOffset.x
const page = Math.round(offsetX / this.props.containerWidth)
if (this.state.currentPage !== page) {
this._updateSelectedPage(page)
}
},
_updateSelectedPage(nextPage) {
let localNextPage = nextPage
if (typeof localNextPage === 'object') {
localNextPage = nextPage.nativeEvent.position
}
const currentPage = this.state.currentPage
this.updateSceneKeys({
page: localNextPage,
callback: this._onChangeTab.bind(this, currentPage, localNextPage),
})
},
_onChangeTab(prevPage, currentPage) {
this.props.onChangeTab({
i: currentPage,
ref: this._children()[currentPage],
from: prevPage,
})
},
_onScroll(e) {
if (Platform.OS === 'ios') {
const offsetX = e.nativeEvent.contentOffset.x
if (offsetX === 0 && !this.scrollOnMountCalled) {
this.scrollOnMountCalled = true
} else {
this.props.onScroll(offsetX / this.props.containerWidth)
}
} else {
const { position, offset } = e.nativeEvent
this.props.onScroll(position + offset)
}
},
_handleLayout(e) {
const { width } = e.nativeEvent.layout
if (!width || width <= 0 || Math.round(width) === Math.round(this.props.containerWidth)) {
return
}
if (Math.round(width) !== Math.round(this.props.containerWidth)) {
if (Platform.OS === 'ios') {
const containerWidthAnimatedValue = new Animated.Value(width)
// Need to call __makeNative manually to avoid a native animated bug. See
// https://github.com/facebook/react-native/pull/14435
containerWidthAnimatedValue.__makeNative()
scrollValue = Animated.divide(this.state.scrollXIOS, containerWidthAnimatedValue)
this.setState({ containerWidth: width, scrollValue })
} else {
this.setState({ containerWidth: width })
}
this.requestAnimationFrame(() => {
this.goToPage(this.state.currentPage)
})
}
},
_children(children = this.props.children) {
return React.Children.map(children, child => child)
},
render() {
return (<View style={[styles.container, this.props.style]} onLayout={this._handleLayout}>
{this.renderScrollableContent()}
</View>)
},
})
module.exports = ScrollableTabView
const styles = StyleSheet.create({
container: {
flex: 1,
},
scrollableContentAndroid: {
flex: 1,
},
})