forked from machadogj/react-native-carousel-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (159 loc) · 5.61 KB
/
index.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
"use strict";
import React, { Component, PropTypes } from "react";
import {
Dimensions,
StyleSheet,
View,
ScrollView,
Text,
TouchableWithoutFeedback
} from "react-native";
import styles from "./styles";
let { width } = Dimensions.get("window");
export default class Carousel extends Component {
displayName = "Carousel";
static propTypes = {
pageStyle: PropTypes.object,
pageWidth: PropTypes.number,
children: PropTypes.oneOfType([ PropTypes.arrayOf(PropTypes.node), PropTypes.node ]).isRequired,
initialPage: PropTypes.number,
noItemsText: PropTypes.string,
onPageChange: PropTypes.func,
sneak: PropTypes.number,
transitionDelay: PropTypes.number
};
static defaultProps = {
initialPage: 0,
pageStyle: null,
pageWidth: width - 80,
sneak: 20,
noItemsText: "Sorry, there are currently \n no items available",
transitionDelay: 0
};
componentWillMount() {
this.calculateGap(this.props);
}
componentDidMount() {
if (this.props.children && this.props.initialPage > 0 && this.props.initialPage < this.props.children.length) {
this.goToPage(this.props.initialPage);
}
}
componentWillReceiveProps(props) {
this.calculateGap(props);
}
calculateGap(props) {
let { sneak, pageWidth } = props;
if (pageWidth > width) {
throw new Error("invalid pageWith");
}
/*
------------
| |
|- ---- -|
| | | | | |
| | | | | |
| | | | | |
|- ---- -|
|^-- sneak |
| ^--- gap
------------
*/
let gap = (width - (2 * sneak) - pageWidth) / 2;
this.setState({gap: gap});
}
goToPage(position) {
let { pageWidth, transitionDelay } = this.props;
let { gap } = this.state;
let pagePosition = position * (pageWidth + gap);
// in android, you can't scroll directly in componentDidMount
// (http://stackoverflow.com/questions/33208477/react-native-android-scrollview-scrollto-not-working)
// however this doesn't work in android for some reason:
// InteractionManager.runAfterInteractions(() => {
// this.scrollView.scrollTo({ y: 0, x: pagePosition}, true);
// console.log('scrollView.scrollTo x:', pagePosition);
// });
// So I was left with an arbitrary timeout.
setTimeout(()=> {
this.scrollView.scrollTo({ y: 0, x: pagePosition}, true);
}, transitionDelay);
this._onPageChange(position);
}
handleScrollEnd = (e) => {
let { pageWidth } = this.props;
let { gap } = this.state;
let pageOffset = pageWidth + gap;
//select page based on the position of the middle of the screen.
let currentPosition = e.nativeEvent.contentOffset.x + (pageOffset / 2);
let currentPage = ~~(currentPosition / pageOffset);
this.scrollView.scrollTo({ y: 0, x: currentPage * pageOffset });
this._onPageChange(currentPage);
};
_onPageChange(position) {
if (this.props.onPageChange) {
let currentElement = this.props.children[position];
this.props.onPageChange(position, currentElement);
}
}
render() {
let { sneak, pageWidth } = this.props;
let { gap } = this.state;
let computedStyles = StyleSheet.create({
scrollView: {
paddingLeft: sneak + gap / 2,
paddingRight: sneak + gap / 2
},
page: {
width: pageWidth,
justifyContent: "center",
marginLeft: gap / 2,
marginRight: gap / 2
}
});
// if no children render a no items dummy page without callbacks
let body = null;
if (!this.props.children) {
body = (
<TouchableWithoutFeedback>
<View style={ [ styles.page, computedStyles.page, this.props.pageStyle ] }>
<Text style={ styles.noItemsText }>
{ this.props.noItemsText }
</Text>
</View>
</TouchableWithoutFeedback>
);
}
else {
const children = Array.isArray(this.props.children) ? this.props.children : [this.props.children]
body = children.map((c, index) => {
return (
<TouchableWithoutFeedback
key={ index }
onPress={ () => this.goToPage(index) }
>
<View
style={ [ styles.page, computedStyles.page, this.props.pageStyle ] }
>
{ c }
</View>
</TouchableWithoutFeedback>
);
});
}
return (
<View style={ styles.container }>
<ScrollView
automaticallyAdjustContentInsets={ false }
bounces
contentContainerStyle={ [ computedStyles.scrollView ] }
decelerationRate={ 0.9 }
horizontal
onScrollEndDrag={ this.handleScrollEnd }
ref={ c => this.scrollView = c }
showsHorizontalScrollIndicator={ false }
>
{ body }
</ScrollView>
</View>
);
}
}