@momo-kits/swipe 0.0.49-beta.2 → 0.0.49-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SwipeAction.js +2 -1
- package/Swiper.js +664 -731
- package/package.json +15 -13
package/Swiper.js
CHANGED
|
@@ -15,14 +15,15 @@
|
|
|
15
15
|
import React, { Component } from 'react';
|
|
16
16
|
import PropTypes from 'prop-types';
|
|
17
17
|
import {
|
|
18
|
+
Text,
|
|
18
19
|
View,
|
|
19
20
|
ScrollView,
|
|
20
21
|
Dimensions,
|
|
21
22
|
TouchableOpacity,
|
|
22
23
|
Platform,
|
|
23
|
-
ActivityIndicator
|
|
24
|
+
ActivityIndicator
|
|
24
25
|
} from 'react-native';
|
|
25
|
-
import { ViewPager as ViewPagerAndroid
|
|
26
|
+
import { ViewPager as ViewPagerAndroid } from '@momo-kits/core';
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* Default styles
|
|
@@ -32,7 +33,7 @@ const styles = {
|
|
|
32
33
|
container: {
|
|
33
34
|
backgroundColor: 'transparent',
|
|
34
35
|
position: 'relative',
|
|
35
|
-
flex: 1
|
|
36
|
+
flex: 1
|
|
36
37
|
},
|
|
37
38
|
|
|
38
39
|
wrapperIOS: {
|
|
@@ -41,7 +42,7 @@ const styles = {
|
|
|
41
42
|
|
|
42
43
|
wrapperAndroid: {
|
|
43
44
|
backgroundColor: 'transparent',
|
|
44
|
-
flex: 1
|
|
45
|
+
flex: 1
|
|
45
46
|
},
|
|
46
47
|
|
|
47
48
|
slide: {
|
|
@@ -57,7 +58,7 @@ const styles = {
|
|
|
57
58
|
flex: 1,
|
|
58
59
|
justifyContent: 'center',
|
|
59
60
|
alignItems: 'center',
|
|
60
|
-
backgroundColor: 'transparent'
|
|
61
|
+
backgroundColor: 'transparent'
|
|
61
62
|
},
|
|
62
63
|
|
|
63
64
|
pagination_y: {
|
|
@@ -69,7 +70,7 @@ const styles = {
|
|
|
69
70
|
flex: 1,
|
|
70
71
|
justifyContent: 'center',
|
|
71
72
|
alignItems: 'center',
|
|
72
|
-
backgroundColor: 'transparent'
|
|
73
|
+
backgroundColor: 'transparent'
|
|
73
74
|
},
|
|
74
75
|
|
|
75
76
|
title: {
|
|
@@ -81,7 +82,7 @@ const styles = {
|
|
|
81
82
|
left: 0,
|
|
82
83
|
flexWrap: 'nowrap',
|
|
83
84
|
width: 250,
|
|
84
|
-
backgroundColor: 'transparent'
|
|
85
|
+
backgroundColor: 'transparent'
|
|
85
86
|
},
|
|
86
87
|
|
|
87
88
|
buttonWrapper: {
|
|
@@ -94,448 +95,414 @@ const styles = {
|
|
|
94
95
|
paddingHorizontal: 10,
|
|
95
96
|
paddingVertical: 10,
|
|
96
97
|
justifyContent: 'space-between',
|
|
97
|
-
alignItems: 'center'
|
|
98
|
+
alignItems: 'center'
|
|
98
99
|
},
|
|
99
100
|
|
|
100
101
|
buttonText: {
|
|
101
102
|
fontSize: 50,
|
|
102
|
-
color: '#007aff'
|
|
103
|
-
}
|
|
103
|
+
color: '#007aff'
|
|
104
|
+
}
|
|
104
105
|
};
|
|
105
106
|
|
|
106
107
|
// missing `module.exports = exports['default'];` with babel6
|
|
107
108
|
// export default React.createClass({
|
|
108
109
|
export default class Swiper extends Component {
|
|
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
|
-
onIndexChanged: PropTypes.func,
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Default props
|
|
159
|
-
* @return {object} props
|
|
160
|
-
* @see http://facebook.github.io/react-native/docs/scrollview.html
|
|
161
|
-
*/
|
|
162
|
-
static defaultProps = {
|
|
163
|
-
horizontal: true,
|
|
164
|
-
pagingEnabled: true,
|
|
165
|
-
showsHorizontalScrollIndicator: false,
|
|
166
|
-
showsVerticalScrollIndicator: false,
|
|
167
|
-
bounces: false,
|
|
168
|
-
scrollsToTop: false,
|
|
169
|
-
removeClippedSubviews: true,
|
|
170
|
-
automaticallyAdjustContentInsets: false,
|
|
171
|
-
showsPagination: true,
|
|
172
|
-
showsButtons: false,
|
|
173
|
-
disableNextButton: false,
|
|
174
|
-
loop: true,
|
|
175
|
-
loadMinimal: false,
|
|
176
|
-
loadMinimalSize: 1,
|
|
177
|
-
autoplay: false,
|
|
178
|
-
autoplayTimeout: 2.5,
|
|
179
|
-
autoplayDirection: true,
|
|
180
|
-
index: 0,
|
|
181
|
-
onIndexChanged: () => null,
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Init states
|
|
186
|
-
* @return {object} states
|
|
187
|
-
*/
|
|
188
|
-
state = this.initState(this.props);
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Initial render flag
|
|
192
|
-
* @type {bool}
|
|
193
|
-
*/
|
|
194
|
-
initialRender = true;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* autoplay timer
|
|
198
|
-
* @type {null}
|
|
199
|
-
*/
|
|
200
|
-
autoplayTimer = null;
|
|
201
|
-
|
|
202
|
-
loopJumpTimer = null;
|
|
203
|
-
|
|
204
|
-
UNSAFE_componentWillReceiveProps(nextProps) {
|
|
205
|
-
if (!nextProps.autoplay && this.autoplayTimer)
|
|
206
|
-
clearTimeout(this.autoplayTimer);
|
|
207
|
-
// Fix render last item first
|
|
208
|
-
if (nextProps.index === this.props.index) return;
|
|
209
|
-
this.setState(
|
|
210
|
-
this.initState(nextProps, this.props.index !== nextProps.index),
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
componentDidMount() {
|
|
215
|
-
this.autoplay();
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
componentWillUnmount() {
|
|
219
|
-
this.autoplayTimer && clearTimeout(this.autoplayTimer);
|
|
220
|
-
this.loopJumpTimer && clearTimeout(this.loopJumpTimer);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
UNSAFE_componentWillUpdate(nextProps, nextState) {
|
|
224
|
-
// If the index has changed, we notify the parent via the onIndexChanged callback
|
|
225
|
-
if (this.state.index !== nextState.index)
|
|
226
|
-
this.props.onIndexChanged(nextState.index);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
initState(props, updateIndex = false) {
|
|
230
|
-
// set the current state
|
|
231
|
-
const state = this.state || {
|
|
232
|
-
width: 0,
|
|
233
|
-
height: 0,
|
|
234
|
-
offset: { x: 0, y: 0 },
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
const initState = {
|
|
238
|
-
autoplayEnd: false,
|
|
239
|
-
loopJump: false,
|
|
240
|
-
offset: {},
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
initState.total = props.children ? props.children.length || 1 : 0;
|
|
244
|
-
|
|
245
|
-
if (state.total === initState.total && !updateIndex) {
|
|
246
|
-
// retain the index
|
|
247
|
-
initState.index = state.index;
|
|
248
|
-
} else {
|
|
249
|
-
initState.index =
|
|
250
|
-
initState.total > 1
|
|
251
|
-
? Math.min(props.index, initState.total - 1)
|
|
252
|
-
: 0;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Default: horizontal
|
|
256
|
-
const { width, height } = Dimensions.get('window');
|
|
257
|
-
|
|
258
|
-
initState.dir = props.horizontal === false ? 'y' : 'x';
|
|
259
|
-
|
|
260
|
-
if (props.width) {
|
|
261
|
-
initState.width = props.width;
|
|
262
|
-
} else if (this.state && this.state.width) {
|
|
263
|
-
initState.width = this.state.width;
|
|
264
|
-
} else {
|
|
265
|
-
initState.width = width;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (props.height) {
|
|
269
|
-
initState.height = props.height;
|
|
270
|
-
} else if (this.state && this.state.height) {
|
|
271
|
-
initState.height = this.state.height;
|
|
272
|
-
} else {
|
|
273
|
-
initState.height = height;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
initState.offset[initState.dir] =
|
|
277
|
-
initState.dir === 'y' ? height * props.index : width * props.index;
|
|
278
|
-
|
|
279
|
-
this.internals = {
|
|
280
|
-
...this.internals,
|
|
281
|
-
isScrolling: false,
|
|
282
|
-
};
|
|
283
|
-
return initState;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// include internals with state
|
|
287
|
-
fullState() {
|
|
288
|
-
return { ...this.state, ...this.internals };
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
onLayout = (event) => {
|
|
292
|
-
const { width, height } = event.nativeEvent.layout;
|
|
293
|
-
const offset = (this.internals.offset = {});
|
|
294
|
-
const state = { width, height };
|
|
295
|
-
|
|
296
|
-
if (this.state.total > 1) {
|
|
297
|
-
let setup = this.state.index;
|
|
298
|
-
if (this.props.loop) {
|
|
299
|
-
setup++;
|
|
300
|
-
}
|
|
301
|
-
offset[this.state.dir] =
|
|
302
|
-
this.state.dir === 'y' ? height * setup : width * setup;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// only update the offset in state if needed, updating offset while swiping
|
|
306
|
-
// causes some bad jumping / stuttering
|
|
307
|
-
if (
|
|
308
|
-
!this.state.offset ||
|
|
309
|
-
width !== this.state.width ||
|
|
310
|
-
height !== this.state.height
|
|
311
|
-
) {
|
|
312
|
-
state.offset = offset;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// related to https://github.com/leecade/react-native-swiper/issues/570
|
|
316
|
-
// contentOffset is not working in react 0.48.x so we need to use scrollTo
|
|
317
|
-
// to emulate offset.
|
|
318
|
-
if (Platform.OS === 'ios') {
|
|
319
|
-
if (this.initialRender && this.state.total > 1) {
|
|
320
|
-
this.scrollView.scrollTo({ ...offset, animated: false });
|
|
321
|
-
this.initialRender = false;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
this.setState(state);
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
loopJump = () => {
|
|
329
|
-
if (!this.state.loopJump) return;
|
|
330
|
-
const i = this.state.index + (this.props.loop ? 1 : 0);
|
|
331
|
-
const { scrollView } = this;
|
|
332
|
-
this.loopJumpTimer = setTimeout(
|
|
333
|
-
() =>
|
|
334
|
-
scrollView.setPageWithoutAnimation &&
|
|
335
|
-
scrollView.setPageWithoutAnimation(i),
|
|
336
|
-
50,
|
|
337
|
-
);
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Automatic rolling
|
|
110
|
+
/**
|
|
111
|
+
* Props Validation
|
|
112
|
+
* @type {Object}
|
|
113
|
+
*/
|
|
114
|
+
static propTypes = {
|
|
115
|
+
horizontal: PropTypes.bool,
|
|
116
|
+
children: PropTypes.node.isRequired,
|
|
117
|
+
containerStyle: PropTypes.oneOfType([
|
|
118
|
+
PropTypes.object,
|
|
119
|
+
PropTypes.number,
|
|
120
|
+
]),
|
|
121
|
+
style: PropTypes.oneOfType([
|
|
122
|
+
PropTypes.object,
|
|
123
|
+
PropTypes.number,
|
|
124
|
+
]),
|
|
125
|
+
scrollViewStyle: PropTypes.oneOfType([
|
|
126
|
+
PropTypes.object,
|
|
127
|
+
PropTypes.number,
|
|
128
|
+
]),
|
|
129
|
+
pagingEnabled: PropTypes.bool,
|
|
130
|
+
showsHorizontalScrollIndicator: PropTypes.bool,
|
|
131
|
+
showsVerticalScrollIndicator: PropTypes.bool,
|
|
132
|
+
bounces: PropTypes.bool,
|
|
133
|
+
scrollsToTop: PropTypes.bool,
|
|
134
|
+
removeClippedSubviews: PropTypes.bool,
|
|
135
|
+
automaticallyAdjustContentInsets: PropTypes.bool,
|
|
136
|
+
showsPagination: PropTypes.bool,
|
|
137
|
+
showsButtons: PropTypes.bool,
|
|
138
|
+
disableNextButton: PropTypes.bool,
|
|
139
|
+
loadMinimal: PropTypes.bool,
|
|
140
|
+
loadMinimalSize: PropTypes.number,
|
|
141
|
+
loadMinimalLoader: PropTypes.element,
|
|
142
|
+
loop: PropTypes.bool,
|
|
143
|
+
autoplay: PropTypes.bool,
|
|
144
|
+
autoplayTimeout: PropTypes.number,
|
|
145
|
+
autoplayDirection: PropTypes.bool,
|
|
146
|
+
index: PropTypes.number,
|
|
147
|
+
renderPagination: PropTypes.func,
|
|
148
|
+
dotStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
|
|
149
|
+
activeDotStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
|
|
150
|
+
dotColor: PropTypes.string,
|
|
151
|
+
activeDotColor: PropTypes.string,
|
|
152
|
+
/**
|
|
153
|
+
* Called when the index has changed because the user swiped.
|
|
342
154
|
*/
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
155
|
+
onIndexChanged: PropTypes.func
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Default props
|
|
160
|
+
* @return {object} props
|
|
161
|
+
* @see http://facebook.github.io/react-native/docs/scrollview.html
|
|
162
|
+
*/
|
|
163
|
+
static defaultProps = {
|
|
164
|
+
horizontal: true,
|
|
165
|
+
pagingEnabled: true,
|
|
166
|
+
showsHorizontalScrollIndicator: false,
|
|
167
|
+
showsVerticalScrollIndicator: false,
|
|
168
|
+
bounces: false,
|
|
169
|
+
scrollsToTop: false,
|
|
170
|
+
removeClippedSubviews: true,
|
|
171
|
+
automaticallyAdjustContentInsets: false,
|
|
172
|
+
showsPagination: true,
|
|
173
|
+
showsButtons: false,
|
|
174
|
+
disableNextButton: false,
|
|
175
|
+
loop: true,
|
|
176
|
+
loadMinimal: false,
|
|
177
|
+
loadMinimalSize: 1,
|
|
178
|
+
autoplay: false,
|
|
179
|
+
autoplayTimeout: 2.5,
|
|
180
|
+
autoplayDirection: true,
|
|
181
|
+
index: 0,
|
|
182
|
+
onIndexChanged: () => null
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Init states
|
|
187
|
+
* @return {object} states
|
|
188
|
+
*/
|
|
189
|
+
state = this.initState(this.props)
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Initial render flag
|
|
193
|
+
* @type {bool}
|
|
194
|
+
*/
|
|
195
|
+
initialRender = true
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* autoplay timer
|
|
199
|
+
* @type {null}
|
|
200
|
+
*/
|
|
201
|
+
autoplayTimer = null
|
|
202
|
+
|
|
203
|
+
loopJumpTimer = null
|
|
204
|
+
|
|
205
|
+
UNSAFE_componentWillReceiveProps(nextProps) {
|
|
206
|
+
if (!nextProps.autoplay && this.autoplayTimer) clearTimeout(this.autoplayTimer);
|
|
207
|
+
// Fix render last item first
|
|
208
|
+
if (nextProps.index === this.props.index) return;
|
|
209
|
+
this.setState(this.initState(nextProps, this.props.index !== nextProps.index));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
componentDidMount() {
|
|
213
|
+
this.autoplay();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
componentWillUnmount() {
|
|
217
|
+
this.autoplayTimer && clearTimeout(this.autoplayTimer);
|
|
218
|
+
this.loopJumpTimer && clearTimeout(this.loopJumpTimer);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
UNSAFE_componentWillUpdate(nextProps, nextState) {
|
|
222
|
+
// If the index has changed, we notify the parent via the onIndexChanged callback
|
|
223
|
+
if (this.state.index !== nextState.index) this.props.onIndexChanged(nextState.index);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
initState(props, updateIndex = false) {
|
|
227
|
+
// set the current state
|
|
228
|
+
const state = this.state || { width: 0, height: 0, offset: { x: 0, y: 0 } };
|
|
229
|
+
|
|
230
|
+
const initState = {
|
|
231
|
+
autoplayEnd: false,
|
|
232
|
+
loopJump: false,
|
|
233
|
+
offset: {}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
initState.total = props.children ? props.children.length || 1 : 0;
|
|
237
|
+
|
|
238
|
+
if (state.total === initState.total && !updateIndex) {
|
|
239
|
+
// retain the index
|
|
240
|
+
initState.index = state.index;
|
|
241
|
+
} else {
|
|
242
|
+
initState.index = initState.total > 1 ? Math.min(props.index, initState.total - 1) : 0;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Default: horizontal
|
|
246
|
+
const { width, height } = Dimensions.get('window');
|
|
247
|
+
|
|
248
|
+
initState.dir = props.horizontal === false ? 'y' : 'x';
|
|
249
|
+
|
|
250
|
+
if (props.width) {
|
|
251
|
+
initState.width = props.width;
|
|
252
|
+
} else if (this.state && this.state.width) {
|
|
253
|
+
initState.width = this.state.width;
|
|
254
|
+
} else {
|
|
255
|
+
initState.width = width;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (props.height) {
|
|
259
|
+
initState.height = props.height;
|
|
260
|
+
} else if (this.state && this.state.height) {
|
|
261
|
+
initState.height = this.state.height;
|
|
262
|
+
} else {
|
|
263
|
+
initState.height = height;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
initState.offset[initState.dir] = initState.dir === 'y'
|
|
267
|
+
? height * props.index
|
|
268
|
+
: width * props.index;
|
|
269
|
+
|
|
270
|
+
this.internals = {
|
|
271
|
+
...this.internals,
|
|
272
|
+
isScrolling: false
|
|
273
|
+
};
|
|
274
|
+
return initState;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// include internals with state
|
|
278
|
+
fullState() {
|
|
279
|
+
return { ...this.state, ...this.internals };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
onLayout = (event) => {
|
|
283
|
+
const { width, height } = event.nativeEvent.layout;
|
|
284
|
+
const offset = this.internals.offset = {};
|
|
285
|
+
const state = { width, height };
|
|
286
|
+
|
|
287
|
+
if (this.state.total > 1) {
|
|
288
|
+
let setup = this.state.index;
|
|
289
|
+
if (this.props.loop) {
|
|
290
|
+
setup++;
|
|
291
|
+
}
|
|
292
|
+
offset[this.state.dir] = this.state.dir === 'y'
|
|
293
|
+
? height * setup
|
|
294
|
+
: width * setup;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// only update the offset in state if needed, updating offset while swiping
|
|
298
|
+
// causes some bad jumping / stuttering
|
|
299
|
+
if (!this.state.offset || width !== this.state.width || height !== this.state.height) {
|
|
300
|
+
state.offset = offset;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// related to https://github.com/leecade/react-native-swiper/issues/570
|
|
304
|
+
// contentOffset is not working in react 0.48.x so we need to use scrollTo
|
|
305
|
+
// to emulate offset.
|
|
306
|
+
if (Platform.OS === 'ios') {
|
|
307
|
+
if (this.initialRender && this.state.total > 1) {
|
|
308
|
+
this.scrollView.scrollTo({ ...offset, animated: false });
|
|
309
|
+
this.initialRender = false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
this.setState(state);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
loopJump = () => {
|
|
317
|
+
if (!this.state.loopJump) return;
|
|
318
|
+
const i = this.state.index + (this.props.loop ? 1 : 0);
|
|
319
|
+
const { scrollView } = this;
|
|
320
|
+
this.loopJumpTimer = setTimeout(() => scrollView.setPageWithoutAnimation
|
|
321
|
+
&& scrollView.setPageWithoutAnimation(i), 50);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Automatic rolling
|
|
326
|
+
*/
|
|
327
|
+
autoplay = () => {
|
|
328
|
+
if (!Array.isArray(this?.props?.children)
|
|
329
|
+
|| !this.props.autoplay
|
|
330
|
+
|| this.internals.isScrolling
|
|
331
|
+
|| this.state.autoplayEnd) return;
|
|
332
|
+
|
|
333
|
+
this.autoplayTimer && clearTimeout(this.autoplayTimer);
|
|
334
|
+
this.autoplayTimer = setTimeout(() => {
|
|
335
|
+
if (!this.props.loop && (
|
|
336
|
+
this.props.autoplayDirection
|
|
337
|
+
? this.state.index === this.state.total - 1
|
|
338
|
+
: this.state.index === 0
|
|
339
|
+
)
|
|
340
|
+
) return this.setState({ autoplayEnd: true });
|
|
341
|
+
|
|
342
|
+
this.scrollBy(this.props.autoplayDirection ? 1 : -1);
|
|
343
|
+
}, this.props.autoplayTimeout * 1000);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Scroll begin handle
|
|
348
|
+
* @param {object} e native event
|
|
349
|
+
*/
|
|
350
|
+
onScrollBegin = (e) => {
|
|
351
|
+
// update scroll state
|
|
352
|
+
this.internals.isScrolling = true;
|
|
353
|
+
this.props.onScrollBeginDrag && this.props.onScrollBeginDrag(e, this.fullState(), this);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Scroll end handle
|
|
358
|
+
* @param {object} e native event
|
|
359
|
+
*/
|
|
360
|
+
onScrollEnd = (e) => {
|
|
361
|
+
// update scroll state
|
|
362
|
+
this.internals.isScrolling = false;
|
|
363
|
+
|
|
364
|
+
// making our events coming from android compatible to updateIndex logic
|
|
365
|
+
if (!e.nativeEvent.contentOffset) {
|
|
366
|
+
if (this.state.dir === 'x') {
|
|
367
|
+
e.nativeEvent.contentOffset = { x: e.nativeEvent.position * this.state.width };
|
|
368
|
+
} else {
|
|
369
|
+
e.nativeEvent.contentOffset = { y: e.nativeEvent.position * this.state.height };
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
this.updateIndex(e.nativeEvent.contentOffset, this.state.dir, () => {
|
|
374
|
+
this.autoplay();
|
|
375
|
+
this.loopJump();
|
|
376
|
+
|
|
377
|
+
// if `onMomentumScrollEnd` registered will be called here
|
|
378
|
+
this.props.onMomentumScrollEnd && this.props.onMomentumScrollEnd(e, this.fullState(), this);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/*
|
|
383
|
+
* Drag end handle
|
|
384
|
+
* @param {object} e native event
|
|
385
|
+
*/
|
|
386
|
+
onScrollEndDrag = (e) => {
|
|
387
|
+
const { contentOffset } = e.nativeEvent;
|
|
388
|
+
const { horizontal, children } = this.props;
|
|
389
|
+
const { index } = this.state;
|
|
390
|
+
const { offset } = this.internals;
|
|
391
|
+
const previousOffset = horizontal ? offset.x : offset.y;
|
|
392
|
+
const newOffset = horizontal ? contentOffset.x : contentOffset.y;
|
|
393
|
+
|
|
394
|
+
if (previousOffset === newOffset
|
|
395
|
+
&& (index === 0 || index === children.length - 1)) {
|
|
396
|
+
this.internals.isScrolling = false;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Update index after scroll
|
|
402
|
+
* @param {object} offset content offset
|
|
403
|
+
* @param {string} dir 'x' || 'y'
|
|
404
|
+
*/
|
|
405
|
+
updateIndex = (offset, dir, cb) => {
|
|
406
|
+
const { state } = this;
|
|
407
|
+
let { index } = state;
|
|
408
|
+
if (!this.internals.offset) // Android not setting this onLayout first? https://github.com/leecade/react-native-swiper/issues/582
|
|
409
|
+
{ this.internals.offset = {}; }
|
|
410
|
+
const diff = offset[dir] - this.internals.offset[dir];
|
|
411
|
+
const step = dir === 'x' ? state.width : state.height;
|
|
412
|
+
let loopJump = false;
|
|
413
|
+
|
|
414
|
+
// Do nothing if offset no change.
|
|
415
|
+
if (!diff) return;
|
|
416
|
+
|
|
417
|
+
// Note: if touch very very quickly and continuous,
|
|
418
|
+
// the variation of `index` more than 1.
|
|
419
|
+
// parseInt() ensures it's always an integer
|
|
420
|
+
index = parseInt(index + Math.round(diff / step));
|
|
421
|
+
|
|
422
|
+
if (this.props.loop) {
|
|
423
|
+
if (index <= -1) {
|
|
424
|
+
index = state.total - 1;
|
|
425
|
+
offset[dir] = step * state.total;
|
|
426
|
+
loopJump = true;
|
|
427
|
+
} else if (index >= state.total) {
|
|
428
|
+
index = 0;
|
|
429
|
+
offset[dir] = step;
|
|
430
|
+
loopJump = true;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const newState = {};
|
|
435
|
+
newState.index = index;
|
|
436
|
+
newState.loopJump = loopJump;
|
|
437
|
+
|
|
438
|
+
this.internals.offset = offset;
|
|
439
|
+
|
|
440
|
+
// only update offset in state if loopJump is true
|
|
441
|
+
if (loopJump) {
|
|
442
|
+
// when swiping to the beginning of a looping set for the third time,
|
|
443
|
+
// the new offset will be the same as the last one set in state.
|
|
444
|
+
// Setting the offset to the same thing will not do anything,
|
|
445
|
+
// so we increment it by 1 then immediately set it to what it should be,
|
|
446
|
+
// after render.
|
|
447
|
+
if (offset[dir] === this.internals.offset[dir]) {
|
|
448
|
+
newState.offset = { x: 0, y: 0 };
|
|
449
|
+
newState.offset[dir] = offset[dir] + 1;
|
|
450
|
+
this.setState(newState, () => {
|
|
451
|
+
this.setState({ offset }, cb);
|
|
452
|
+
});
|
|
453
|
+
} else {
|
|
454
|
+
newState.offset = offset;
|
|
455
|
+
this.setState(newState, cb);
|
|
456
|
+
}
|
|
457
|
+
} else {
|
|
458
|
+
this.setState(newState, cb);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Scroll by index
|
|
464
|
+
* @param {number} index offset index
|
|
465
|
+
* @param {bool} animated
|
|
466
|
+
*/
|
|
467
|
+
|
|
468
|
+
scrollBy = (index, animated = true) => {
|
|
469
|
+
if (this.internals.isScrolling || this.state.total < 2) return;
|
|
470
|
+
const { state } = this;
|
|
471
|
+
const diff = (this.props.loop ? 1 : 0) + index + this.state.index;
|
|
472
|
+
let x = 0;
|
|
473
|
+
let y = 0;
|
|
474
|
+
if (state.dir === 'x') x = diff * state.width;
|
|
475
|
+
if (state.dir === 'y') y = diff * state.height;
|
|
476
|
+
|
|
477
|
+
if (Platform.OS !== 'ios') {
|
|
478
|
+
this.scrollView && this.scrollView[animated ? 'setPage' : 'setPageWithoutAnimation'](diff);
|
|
479
|
+
} else {
|
|
480
|
+
this.scrollView && this.scrollView.scrollTo({ x, y, animated });
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// update scroll state
|
|
484
|
+
this.internals.isScrolling = true;
|
|
485
|
+
this.setState({
|
|
486
|
+
autoplayEnd: false
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// trigger onScrollEnd manually in android
|
|
490
|
+
if (!animated || Platform.OS !== 'ios') {
|
|
491
|
+
setImmediate(() => {
|
|
492
|
+
this.onScrollEnd({
|
|
493
|
+
nativeEvent: {
|
|
494
|
+
position: diff
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
scrollViewPropOverrides = () => {
|
|
502
|
+
const { props } = this;
|
|
503
|
+
const overrides = {};
|
|
504
|
+
|
|
505
|
+
/*
|
|
539
506
|
const scrollResponders = [
|
|
540
507
|
'onMomentumScrollBegin',
|
|
541
508
|
'onTouchStartCapture',
|
|
@@ -545,296 +512,262 @@ export default class Swiper extends Component {
|
|
|
545
512
|
]
|
|
546
513
|
*/
|
|
547
514
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
return (
|
|
807
|
-
<View style={pageStyleLoading} key={i}>
|
|
808
|
-
{loadMinimalLoader || <ActivityIndicator />}
|
|
809
|
-
</View>
|
|
810
|
-
);
|
|
811
|
-
}
|
|
812
|
-
return (
|
|
813
|
-
<View style={pageStyle} key={i}>
|
|
814
|
-
{children[page]}
|
|
815
|
-
</View>
|
|
816
|
-
);
|
|
817
|
-
});
|
|
818
|
-
} else {
|
|
819
|
-
pages = (
|
|
820
|
-
<View style={pageStyle} key={0}>
|
|
821
|
-
{children}
|
|
822
|
-
</View>
|
|
823
|
-
);
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
return (
|
|
827
|
-
<View
|
|
828
|
-
style={[styles.container, containerStyle]}
|
|
829
|
-
onLayout={this.onLayout}>
|
|
830
|
-
{this.renderScrollView(pages)}
|
|
831
|
-
{showsPagination &&
|
|
832
|
-
(renderPagination
|
|
833
|
-
? renderPagination(index, total, this)
|
|
834
|
-
: this.renderPagination())}
|
|
835
|
-
{this.renderTitle()}
|
|
836
|
-
{showsButtons && this.renderButtons()}
|
|
837
|
-
</View>
|
|
838
|
-
);
|
|
839
|
-
}
|
|
515
|
+
for (const prop in props) {
|
|
516
|
+
// if(~scrollResponders.indexOf(prop)
|
|
517
|
+
if (typeof props[prop] === 'function'
|
|
518
|
+
&& prop !== 'onMomentumScrollEnd'
|
|
519
|
+
&& prop !== 'renderPagination'
|
|
520
|
+
&& prop !== 'onScrollBeginDrag'
|
|
521
|
+
) {
|
|
522
|
+
const originResponder = props[prop];
|
|
523
|
+
overrides[prop] = (e) => originResponder(e, this.fullState(), this);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
return overrides;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Render pagination
|
|
532
|
+
* @return {object} react-dom
|
|
533
|
+
*/
|
|
534
|
+
renderPagination = () => {
|
|
535
|
+
// By default, dots only show when `total` >= 2
|
|
536
|
+
if (this.state.total <= 1) return null;
|
|
537
|
+
|
|
538
|
+
const dots = [];
|
|
539
|
+
const ActiveDot = this.props.activeDot || (
|
|
540
|
+
<View style={[{
|
|
541
|
+
backgroundColor: this.props.activeDotColor || '#007aff',
|
|
542
|
+
width: 8,
|
|
543
|
+
height: 8,
|
|
544
|
+
borderRadius: 4,
|
|
545
|
+
marginLeft: 3,
|
|
546
|
+
marginRight: 3,
|
|
547
|
+
marginTop: 3,
|
|
548
|
+
marginBottom: 3
|
|
549
|
+
}, this.props.activeDotStyle]}
|
|
550
|
+
/>
|
|
551
|
+
);
|
|
552
|
+
const Dot = this.props.dot || (
|
|
553
|
+
<View style={[{
|
|
554
|
+
backgroundColor: this.props.dotColor || 'rgba(0,0,0,.2)',
|
|
555
|
+
width: 8,
|
|
556
|
+
height: 8,
|
|
557
|
+
borderRadius: 4,
|
|
558
|
+
marginLeft: 3,
|
|
559
|
+
marginRight: 3,
|
|
560
|
+
marginTop: 3,
|
|
561
|
+
marginBottom: 3
|
|
562
|
+
}, this.props.dotStyle]}
|
|
563
|
+
/>
|
|
564
|
+
);
|
|
565
|
+
for (let i = 0; i < this.state.total; i++) {
|
|
566
|
+
dots.push(i === this.state.index
|
|
567
|
+
? React.cloneElement(ActiveDot, { key: i })
|
|
568
|
+
: React.cloneElement(Dot, { key: i })
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
return (
|
|
573
|
+
<View pointerEvents="none" style={[styles[`pagination_${this.state.dir}`], this.props.paginationStyle]}>
|
|
574
|
+
{dots}
|
|
575
|
+
</View>
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
renderTitle = () => {
|
|
580
|
+
let { children = [] } = this.props;
|
|
581
|
+
const child = children[this.state.index];
|
|
582
|
+
const title = child && child.props && child.props.title
|
|
583
|
+
return !!title ? (
|
|
584
|
+
<View style={styles.title}>
|
|
585
|
+
{this.props.children?.[this.state.index].props.title}
|
|
586
|
+
</View>
|
|
587
|
+
) : null
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
renderNextButton = () => {
|
|
591
|
+
let button = null;
|
|
592
|
+
|
|
593
|
+
if (this.props.loop
|
|
594
|
+
|| this.state.index !== this.state.total - 1) {
|
|
595
|
+
button = this.props.nextButton || <Text style={styles.buttonText}>›</Text>;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
return (
|
|
599
|
+
<TouchableOpacity
|
|
600
|
+
onPress={() => button !== null && this.scrollBy(1)}
|
|
601
|
+
disabled={this.props.disableNextButton}
|
|
602
|
+
>
|
|
603
|
+
<View>
|
|
604
|
+
{button}
|
|
605
|
+
</View>
|
|
606
|
+
</TouchableOpacity>
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
renderPrevButton = () => {
|
|
611
|
+
let button = null;
|
|
612
|
+
|
|
613
|
+
if (this.props.loop || this.state.index !== 0) {
|
|
614
|
+
button = this.props.prevButton || <Text style={styles.buttonText}>‹</Text>;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return (
|
|
618
|
+
<TouchableOpacity onPress={() => button !== null && this.scrollBy(-1)}>
|
|
619
|
+
<View>
|
|
620
|
+
{button}
|
|
621
|
+
</View>
|
|
622
|
+
</TouchableOpacity>
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
renderButtons = () => (
|
|
627
|
+
<View
|
|
628
|
+
pointerEvents="box-none"
|
|
629
|
+
style={[styles.buttonWrapper, {
|
|
630
|
+
width: this.state.width,
|
|
631
|
+
height: this.state.height
|
|
632
|
+
}, this.props.buttonWrapperStyle]}
|
|
633
|
+
>
|
|
634
|
+
{this.renderPrevButton()}
|
|
635
|
+
{this.renderNextButton()}
|
|
636
|
+
</View>
|
|
637
|
+
)
|
|
638
|
+
|
|
639
|
+
refScrollView = (view) => {
|
|
640
|
+
this.scrollView = view;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
onPageScrollStateChanged = (state) => {
|
|
644
|
+
switch (state) {
|
|
645
|
+
case 'dragging':
|
|
646
|
+
return this.onScrollBegin();
|
|
647
|
+
|
|
648
|
+
case 'idle':
|
|
649
|
+
case 'settling':
|
|
650
|
+
if (this.props.onTouchEnd) this.props.onTouchEnd();
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
renderScrollView = (pages) => {
|
|
655
|
+
if (Platform.OS === 'ios') {
|
|
656
|
+
return (
|
|
657
|
+
<ScrollView
|
|
658
|
+
ref={this.refScrollView}
|
|
659
|
+
{...this.props}
|
|
660
|
+
{...this.scrollViewPropOverrides()}
|
|
661
|
+
contentContainerStyle={[styles.wrapperIOS, this.props.style]}
|
|
662
|
+
contentOffset={this.state.offset}
|
|
663
|
+
onScrollBeginDrag={this.onScrollBegin}
|
|
664
|
+
onMomentumScrollEnd={this.onScrollEnd}
|
|
665
|
+
onScrollEndDrag={this.onScrollEndDrag}
|
|
666
|
+
style={this.props.scrollViewStyle}
|
|
667
|
+
>
|
|
668
|
+
{pages}
|
|
669
|
+
</ScrollView>
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
return (
|
|
673
|
+
<ViewPagerAndroid
|
|
674
|
+
ref={this.refScrollView}
|
|
675
|
+
{...this.props}
|
|
676
|
+
initialPage={this.props.loop ? this.state.index + 1 : this.state.index}
|
|
677
|
+
onPageScrollStateChanged={this.onPageScrollStateChanged}
|
|
678
|
+
onPageSelected={this.onScrollEnd}
|
|
679
|
+
key={pages.length}
|
|
680
|
+
style={[styles.wrapperAndroid, this.props.style]}
|
|
681
|
+
>
|
|
682
|
+
{pages}
|
|
683
|
+
</ViewPagerAndroid>
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
checkPagesBeforeAfter = (children) => {
|
|
688
|
+
let str = '';
|
|
689
|
+
if (children && Array.isArray(children)) {
|
|
690
|
+
for (const child in children) {
|
|
691
|
+
str += children[child].key || child;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return str;
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Default render
|
|
699
|
+
* @return {object} react-dom
|
|
700
|
+
*/
|
|
701
|
+
render() {
|
|
702
|
+
const {
|
|
703
|
+
index,
|
|
704
|
+
total,
|
|
705
|
+
width,
|
|
706
|
+
height
|
|
707
|
+
} = this.state;
|
|
708
|
+
const {
|
|
709
|
+
children,
|
|
710
|
+
containerStyle,
|
|
711
|
+
loop,
|
|
712
|
+
loadMinimal,
|
|
713
|
+
loadMinimalSize,
|
|
714
|
+
loadMinimalLoader,
|
|
715
|
+
renderPagination,
|
|
716
|
+
showsButtons,
|
|
717
|
+
showsPagination,
|
|
718
|
+
} = this.props;
|
|
719
|
+
// let dir = state.dir
|
|
720
|
+
// let key = 0
|
|
721
|
+
const loopVal = loop ? 1 : 0;
|
|
722
|
+
let pages = [];
|
|
723
|
+
|
|
724
|
+
const pageStyle = [{ width, height }, styles.slide];
|
|
725
|
+
const pageStyleLoading = {
|
|
726
|
+
width,
|
|
727
|
+
height,
|
|
728
|
+
flex: 1,
|
|
729
|
+
justifyContent: 'center',
|
|
730
|
+
alignItems: 'center'
|
|
731
|
+
};
|
|
732
|
+
|
|
733
|
+
// For make infinite at least total > 1
|
|
734
|
+
if (total > 1) {
|
|
735
|
+
// Re-design a loop model for avoid img flickering
|
|
736
|
+
pages = Object.keys(children);
|
|
737
|
+
// console.log(`checkPagesBeforeAfter children: ${JSON.stringify(this.checkPagesBeforeAfter(pages))}`);
|
|
738
|
+
|
|
739
|
+
if (loop) {
|
|
740
|
+
pages.unshift(`${total - 1}`);
|
|
741
|
+
pages.push('0');
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
pages = pages.map((page, i) => {
|
|
745
|
+
if (loadMinimal) {
|
|
746
|
+
if (i >= (index + loopVal - loadMinimalSize)
|
|
747
|
+
&& i <= (index + loopVal + loadMinimalSize)) {
|
|
748
|
+
return <View style={pageStyle} key={i}>{children[page]}</View>;
|
|
749
|
+
}
|
|
750
|
+
return (
|
|
751
|
+
<View style={pageStyleLoading} key={i}>
|
|
752
|
+
{loadMinimalLoader || <ActivityIndicator />}
|
|
753
|
+
</View>
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
return <View style={pageStyle} key={i}>{children[page]}</View>;
|
|
757
|
+
});
|
|
758
|
+
} else {
|
|
759
|
+
pages = <View style={pageStyle} key={0}>{children}</View>;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
return (
|
|
763
|
+
<View style={[styles.container, containerStyle]} onLayout={this.onLayout}>
|
|
764
|
+
{this.renderScrollView(pages)}
|
|
765
|
+
{showsPagination && (renderPagination
|
|
766
|
+
? renderPagination(index, total, this)
|
|
767
|
+
: this.renderPagination())}
|
|
768
|
+
{this.renderTitle()}
|
|
769
|
+
{showsButtons && this.renderButtons()}
|
|
770
|
+
</View>
|
|
771
|
+
);
|
|
772
|
+
}
|
|
840
773
|
}
|