|
1 |
| -import React from 'react'; |
| 1 | +import _ from 'lodash'; |
2 | 2 | import PropTypes from 'prop-types';
|
| 3 | +import React from 'react'; |
3 | 4 | import {StyleSheet} from 'react-native';
|
4 |
| -import _ from 'lodash'; |
5 |
| -import * as Constants from '../../helpers/Constants'; |
6 | 5 | import {ThemeManager} from '../../style';
|
7 | 6 | import {BaseComponent} from '../../commons';
|
8 | 7 | import TouchableOpacity from '../touchableOpacity';
|
9 | 8 | import View from '../view';
|
10 | 9 |
|
11 |
| -function getColorStyle(color, index, currentPage) { |
| 10 | + |
| 11 | +function getColorStyle(color, inactiveColor, index, currentPage) { |
12 | 12 | const compColor = color || ThemeManager.primaryColor;
|
13 |
| - return {borderColor: compColor, backgroundColor: (index === currentPage) ? compColor : 'transparent'}; |
| 13 | + return {borderColor: (index === currentPage) ? compColor : inactiveColor || compColor, |
| 14 | + backgroundColor: (index === currentPage) ? compColor : inactiveColor || 'transparent'}; |
| 15 | +} |
| 16 | + |
| 17 | +function getSizeStyle(size, enlargeActive, index, currentPage) { |
| 18 | + const temp = enlargeActive ? ((index === currentPage) ? size + 2 : size) : size; |
| 19 | + return {width: temp, height: temp, borderRadius: temp / 2}; |
14 | 20 | }
|
15 | 21 |
|
16 | 22 | /**
|
@@ -38,56 +44,66 @@ export default class PageControl extends BaseComponent {
|
38 | 44 | */
|
39 | 45 | onPagePress: PropTypes.func,
|
40 | 46 | /**
|
41 |
| - * Color of the selected page dot and the border of the not selected pages |
| 47 | + * Color of the selected page dot and, if inactiveColor not passed, the border of the not selected pages |
42 | 48 | */
|
43 | 49 | color: PropTypes.string,
|
| 50 | + /** |
| 51 | + * Color of the unselected page dots and the border of the not selected pages |
| 52 | + */ |
| 53 | + inactiveColor: PropTypes.string, |
44 | 54 | /**
|
45 | 55 | * The size of the page indicator
|
46 | 56 | */
|
47 | 57 | size: PropTypes.number,
|
| 58 | + /** |
| 59 | + * Whether to enlarge the active page indicator |
| 60 | + */ |
| 61 | + enlargeActive: PropTypes.bool, |
| 62 | + /** |
| 63 | + * The space between the siblings page indicators |
| 64 | + */ |
| 65 | + spacing: PropTypes.number, |
48 | 66 | };
|
49 | 67 |
|
50 |
| - generateStyles() { |
51 |
| - this.styles = createStyles(this.props.size); |
52 |
| - } |
| 68 | + static defaultProps = { |
| 69 | + size: 10, |
| 70 | + spacing: 4, |
| 71 | + enlargeActive: false, |
| 72 | + }; |
53 | 73 |
|
54 | 74 | render() {
|
55 |
| - const {numOfPages, currentPage, color, containerStyle, onPagePress} = this.props; |
| 75 | + const {numOfPages, currentPage, color, inactiveColor, containerStyle, onPagePress, size, spacing, enlargeActive} |
| 76 | + = this.props; |
| 77 | + |
56 | 78 | return (
|
57 |
| - <View style={[this.styles.container, containerStyle]}> |
| 79 | + <View style={[styles.container, containerStyle]}> |
58 | 80 | {
|
59 | 81 | _.map([...new Array(numOfPages)], (item, index) =>
|
60 | 82 | <TouchableOpacity
|
61 | 83 | disabled={_.isUndefined(onPagePress)}
|
62 | 84 | onPress={() => onPagePress && onPagePress(index)}
|
63 | 85 | key={index}
|
64 |
| - style={[this.styles.pageIndicator, getColorStyle(color, index, currentPage)]} |
| 86 | + style={[ |
| 87 | + styles.pageIndicator, |
| 88 | + {marginRight: spacing / 2, marginLeft: spacing / 2}, |
| 89 | + getColorStyle(color, inactiveColor, index, currentPage), |
| 90 | + getSizeStyle(size, enlargeActive, index, currentPage), |
| 91 | + ]} |
65 | 92 | />)
|
66 | 93 | }
|
67 | 94 | </View>
|
68 | 95 | );
|
69 | 96 | }
|
70 | 97 | }
|
71 | 98 |
|
72 |
| -function createStyles(size = 10) { |
73 |
| - return StyleSheet.create({ |
74 |
| - container: { |
75 |
| - flexDirection: 'row', |
76 |
| - alignItems: 'center', |
77 |
| - justifyContent: 'center', |
78 |
| - }, |
79 |
| - pageView: { |
80 |
| - width: Constants.screenWidth, |
81 |
| - height: Constants.screenHeight, |
82 |
| - }, |
83 |
| - pageIndicator: { |
84 |
| - backgroundColor: 'transparent', |
85 |
| - borderWidth: 1, |
86 |
| - marginRight: 2, |
87 |
| - marginLeft: 2, |
88 |
| - width: size, |
89 |
| - height: size, |
90 |
| - borderRadius: size / 2, |
91 |
| - }, |
92 |
| - }); |
93 |
| -} |
| 99 | +const styles = StyleSheet.create({ |
| 100 | + container: { |
| 101 | + flexDirection: 'row', |
| 102 | + alignItems: 'center', |
| 103 | + justifyContent: 'center', |
| 104 | + }, |
| 105 | + pageIndicator: { |
| 106 | + backgroundColor: 'transparent', |
| 107 | + borderWidth: 1, |
| 108 | + }, |
| 109 | +}); |
0 commit comments