-
Notifications
You must be signed in to change notification settings - Fork 30
/
PageControl.js
95 lines (82 loc) · 2.87 KB
/
PageControl.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
var React = require('react');
var ReactNative = require('react-native');
var PropTypes = require('prop-types');
var createReactClass = require('create-react-class');
var { StyleSheet, View, TouchableWithoutFeedback, ViewPropTypes } = ReactNative;
var PageControl = createReactClass({
propTypes: {
numberOfPages: PropTypes.number.isRequired,
currentPage: PropTypes.number,
hidesForSinglePage: PropTypes.bool,
pageIndicatorTintColor: PropTypes.string,
currentPageIndicatorTintColor: PropTypes.string,
indicatorSize: PropTypes.object,
indicatorStyle: ViewPropTypes.style,
currentIndicatorStyle: ViewPropTypes.style,
onPageIndicatorPress: PropTypes.func
},
getDefaultProps: function () {
return {
numberOfPages: 0,
currentPage: 0,
hidesForSinglePage: false,
pageIndicatorTintColor: 'gray',
currentPageIndicatorTintColor: 'white',
indicatorSize: {width: 8, height: 8},
indicatorStyle: {},
currentIndicatorStyle: {},
onPageIndicatorPress: function() {}
};
},
onPageIndicatorPress: function(idx) {
this.props.onPageIndicatorPress(idx);
},
render: function () {
var { style, ...props } = this.props;
var defaultStyle = {
height: this.props.indicatorSize.height
};
var indicatorItemStyle = {
width: this.props.indicatorSize.width,
height: this.props.indicatorSize.height,
borderRadius: this.props.indicatorSize.height / 2,
marginLeft: 5,
marginRight: 5
};
var indicatorStyle = {
...indicatorItemStyle,
...this.props.indicatorStyle,
...{
backgroundColor: this.props.pageIndicatorTintColor
}
};
var currentIndicatorStyle = {
...indicatorItemStyle,
...this.props.currentIndicatorStyle,
...{
backgroundColor: this.props.currentPageIndicatorTintColor
}
};
var pages = [];
for (var i = 0; i < this.props.numberOfPages; i++) {
pages.push(i);
}
return (
this.props.hidesForSinglePage && pages.length <= 1 ? null : <View style={[styles.container, defaultStyle, style]}>
{pages.map((el, i) => <TouchableWithoutFeedback key={i} onPress={this.onPageIndicatorPress.bind(this, i)}>
<View style={i == this.props.currentPage ? currentIndicatorStyle: indicatorStyle} />
</TouchableWithoutFeedback>
)}
</View>
);
}
});
var styles = StyleSheet.create({
container: {
backgroundColor: 'transparent',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'row'
}
});
module.exports = PageControl;