forked from phil-r/react-native-looped-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (137 loc) · 3.95 KB
/
index.js
File metadata and controls
155 lines (137 loc) · 3.95 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
151
152
153
154
155
'use strict';
var React = require('react-native');
var TimerMixin = require('react-timer-mixin');
var Dimensions = require('Dimensions');
var {width, height} = Dimensions.get('window');
var {
AppRegistry,
StyleSheet,
Image,
Text,
ScrollView,
View
} = React;
var PAGE_CHANGE_DELAY = 4000;
/**
* Animates pages in cycle
* (loop possible if children count > 1)
*/
var Carousel = React.createClass({
propTypes:{
children: React.PropTypes.node.isRequired,
delay: React.PropTypes.number,
style: View.propTypes.style
},
mixins: [TimerMixin],
getDefaultProps: function() {
return {delay: PAGE_CHANGE_DELAY};
},
getInitialState: function() {
if (!!this.props.children) {
var childrenCount = this.props.children.length;
return {
contentOffset: {x: childrenCount > 1 ? width : 0, y: 0},
currentPage: childrenCount > 1 ? 1 : 0,
hasChildren: true,
};
} else {
return {
hasChildren: false,
}
}
},
componentDidMount:function(){
if (this.state.hasChildren) {
this._setUpTimer();
}
},
_onScrollBegin: function(event) {
clearTimeout(this.timer);
},
_onScrollEnd: function(event) {
this._setUpTimer();
var offset = event.nativeEvent.contentOffset;
var childrenCount = this.props.children.length;
if (offset.x == 0) {
offset = {x: childrenCount*width, y: 0};
} else if (offset.x == (childrenCount+1)*width) {
offset = {x: width, y: 0};
}
this._calculateCurrentPage(offset.x);
this.setState({contentOffset: offset});
},
_setUpTimer: function() {
//only for cycling
if (this.props.children.length > 1) {
clearTimeout(this.timer);
this.timer = this.setTimeout(this._animateNextPage, this.props.delay);
}
},
_animateNextPage: function() {
var k = this.state.currentPage;
k++;
this.setState({currentPage: k});
this.refs.scrollView.scrollTo(0, k*width);
this._setUpTimer();
},
_calculateCurrentPage: function(offset) {
var page = Math.floor((offset - width/2) / width) + 1;
this.setState({currentPage: page});
},
//TODO: add optional `dots` for displaying current page (like pageControl)
render: function() {
var pages = [];
if (!this.state.hasChildren) {
return (
<Text style={{backgroundColor: 'white'}}>
You are supposed to add children inside Carousel
</Text>
);
}
var children = this.props.children;
//to make infinite pages structure like this is needed: 3-1-2-3-1
//add last one at the 1st place
if (children.length > 1) {
pages.push(children[children.length-1]);
}
//add all pages
for (var i=0; i<children.length; i++) {
pages.push(children[i]);
}
//add first one at the last place
if (children.length > 1) {
pages.push(children[0]);
}
pages = pages.map((page, i) => {
return <View style={{width: width}} key={"page"+i}>{page}</View>;
});
return (
<ScrollView
ref='scrollView'
onScrollBeginDrag={this._onScrollBegin}
onMomentumScrollEnd={this._onScrollEnd}
alwaysBounceHorizontal={false}
alwaysBounceVertical={false}
showsHorizontalScrollIndicator={false}
horizontal={true}
pagingEnabled={true}
bounces={false}
contentOffset={this.state.contentOffset}
contentContainerStyle={[
styles.horizontalScroll,
this.props.style,
{width: width*(this.props.children.length+(this.props.children.length>1?2:0))}
]}
>
{pages}
</ScrollView>
);
},
});
var styles = StyleSheet.create({
horizontalScroll: {
height: height,
position:'absolute'
}
});
module.exports = Carousel;