PageControl.js
2.68 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
'use strict';
import React from 'react-native';
import assign from 'object-assign';
let {
StyleSheet,
Component,
View,
TouchableWithoutFeedback
} = React;
export default class PageControl extends Component {
static propTypes = {
numberOfPages: React.PropTypes.number.isRequired,
currentPage: React.PropTypes.number,
hidesForSinglePage: React.PropTypes.bool,
pageIndicatorTintColor: React.PropTypes.string,
currentPageIndicatorTintColor: React.PropTypes.string,
indicatorSize: React.PropTypes.object,
indicatorStyle: View.propTypes.style,
currentIndicatorStyle: View.propTypes.style,
onPageIndicatorPress: React.PropTypes.func
};
static defaultProps = {
numberOfPages: 0,
currentPage: 0,
hidesForSinglePage: false,
pageIndicatorTintColor: 'gray',
currentPageIndicatorTintColor: 'white',
indicatorSize: {width: 8, height: 8},
indicatorStyle: {},
currentIndicatorStyle: {},
onPageIndicatorPress: function() {}
};
onPageIndicatorPress(idx) {
this.props.onPageIndicatorPress(idx);
}
render() {
let { style, ...props } = this.props;
let defaultStyle = {
height: this.props.indicatorSize.height
};
let indicatorItemStyle = {
width: this.props.indicatorSize.width,
height: this.props.indicatorSize.height,
borderRadius: this.props.indicatorSize.height / 2,
marginLeft: 5,
marginRight: 5
};
let indicatorStyle = assign({}, indicatorItemStyle, this.props.indicatorStyle, {
backgroundColor: this.props.pageIndicatorTintColor
});
let currentIndicatorStyle = assign({}, indicatorItemStyle, this.props.currentIndicatorStyle, {
backgroundColor: this.props.currentPageIndicatorTintColor
});
let pages = [];
for (let 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>
);
}
}
let styles = StyleSheet.create({
container: {
backgroundColor: 'transparent',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'row'
}
});