|
1 | | -import React, { Component } from "react"; |
2 | | -import PropTypes from "prop-types"; |
3 | | -import { Animated, Easing, Text, TouchableOpacity, View } from "react-native"; |
4 | | -import Icon from "react-native-dynamic-vector-icons"; |
5 | | -import styles, { _iconContainer, _textStyle } from "./BouncyCheckbox.style"; |
| 1 | +import React, { Component } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { Animated, Easing, Text, TouchableOpacity, View} from 'react-native' |
| 4 | +import Icon from 'react-native-dynamic-vector-icons' |
| 5 | +import styles, { _iconContainer, _textStyle } from "./BouncyCheckbox.style" |
6 | 6 |
|
7 | 7 | class BouncyCheckbox extends Component { |
8 | 8 | constructor(props) { |
9 | | - super(props); |
| 9 | + super(props) |
10 | 10 | this.state = { |
11 | 11 | checked: false, |
12 | | - springValue: new Animated.Value(1), |
13 | | - }; |
| 12 | + springValue: new Animated.Value(1) |
| 13 | + } |
14 | 14 | } |
15 | 15 |
|
16 | 16 | componentDidMount() { |
17 | | - this.setState({ checked: this.props.isChecked }); |
| 17 | + this.setState({ checked: this.props.isChecked }) |
18 | 18 | } |
19 | 19 |
|
20 | 20 | spring = () => { |
21 | | - const { useNativeDriver } = this.props; |
22 | | - const { checked, springValue } = this.state; |
| 21 | + const { useNativeDriver } = this.props |
| 22 | + const { checked, springValue } = this.state |
23 | 23 | this.setState({ checked: !checked }, () => { |
24 | | - springValue.setValue(0.7); |
| 24 | + springValue.setValue(0.7) |
25 | 25 | Animated.spring(springValue, { |
26 | 26 | toValue: 1, |
27 | 27 | friction: 3, |
28 | | - useNativeDriver, |
29 | | - }).start(); |
| 28 | + useNativeDriver |
| 29 | + }).start() |
30 | 30 | // Outside of the onPress function |
31 | | - const { onPress } = this.props; |
32 | | - if (onPress) onPress(this.state.checked); |
33 | | - }); |
34 | | - }; |
| 31 | + const { onPress } = this.props |
| 32 | + if (onPress) { |
| 33 | + onPress(this.state.checked) |
| 34 | + } |
| 35 | + }) |
| 36 | + } |
35 | 37 |
|
36 | 38 | renderCheckIcon = () => { |
37 | | - const { checked, springValue } = this.state; |
| 39 | + const { checked, springValue } = this.state |
38 | 40 | const { |
| 41 | + size, |
| 42 | + fillColor, |
| 43 | + unfillColor, |
| 44 | + borderWidth, |
| 45 | + borderRadius, |
| 46 | + borderColor, |
39 | 47 | iconName, |
40 | 48 | iconSize, |
41 | 49 | iconType, |
42 | 50 | iconColor, |
43 | | - fillColor, |
44 | 51 | iconStyle, |
45 | | - unfillColor, |
46 | | - iconComponent, |
47 | | - } = this.props; |
| 52 | + iconComponent |
| 53 | + } = this.props |
48 | 54 | return ( |
49 | | - <Animated.View |
50 | | - style={[ { transform: [{ scale: springValue }] }, |
51 | | - _iconContainer(checked, fillColor, unfillColor), iconStyle, |
52 | | - ]} |
53 | | - > |
54 | | - {iconComponent || ( |
55 | | - <Icon |
56 | | - size={iconSize} |
57 | | - name={iconName} |
58 | | - type={iconType} |
59 | | - color={iconColor} |
60 | | - /> |
61 | | - )} |
62 | | - </Animated.View> |
63 | | - ); |
64 | | - }; |
| 55 | + <Animated.View |
| 56 | + style={[ |
| 57 | + { transform: [{ scale: springValue }] }, |
| 58 | + _iconContainer(size, borderWidth, borderRadius, borderColor, checked, fillColor, unfillColor), |
| 59 | + iconStyle |
| 60 | + ]} |
| 61 | + > |
| 62 | + {iconComponent || <Icon size={iconSize} name={iconName} type={iconType} color={iconColor} />} |
| 63 | + </Animated.View> |
| 64 | + ) |
| 65 | + } |
65 | 66 |
|
66 | 67 | render() { |
67 | | - const { |
68 | | - text, |
69 | | - fontSize, |
70 | | - textColor, |
71 | | - textStyle, |
72 | | - fontFamily, |
73 | | - disableTextDecoration, |
74 | | - } = this.props; |
75 | | - |
| 68 | + const { checked } = this.state |
| 69 | + const { text, fontSize, color, textStyle, fontFamily, textDecoration } = this.props |
76 | 70 | return ( |
77 | | - <TouchableOpacity |
78 | | - style={styles.container} |
79 | | - onPress={this.spring.bind(this, Easing.bounce)} |
80 | | - > |
81 | | - {this.renderCheckIcon()} |
82 | | - <View style={styles.textContainer}> |
83 | | - <Text |
84 | | - style={[_textStyle( |
85 | | - this.state.checked, |
86 | | - textColor, |
87 | | - fontFamily, |
88 | | - fontSize, |
89 | | - disableTextDecoration |
90 | | - ), textStyle ]}> |
91 | | - {text} |
92 | | - </Text> |
93 | | - </View> |
94 | | - </TouchableOpacity> |
95 | | - ); |
| 71 | + <TouchableOpacity style={styles.container} onPress={this.spring.bind(this, Easing.bounce)}> |
| 72 | + {this.renderCheckIcon()} |
| 73 | + <View style={styles.textContainer}> |
| 74 | + <Text |
| 75 | + style={[_textStyle( |
| 76 | + this.state.checked, |
| 77 | + color, |
| 78 | + fontFamily, |
| 79 | + fontSize, |
| 80 | + textDecoration, |
| 81 | + ), textStyle ]}> |
| 82 | + {text} |
| 83 | + </Text> |
| 84 | + |
| 85 | + </View> |
| 86 | + </TouchableOpacity> |
| 87 | + ) |
96 | 88 | } |
97 | 89 | } |
98 | 90 |
|
| 91 | + |
99 | 92 | BouncyCheckbox.propTypes = { |
| 93 | + borderWidth: PropTypes.number, |
| 94 | + borderRadius: PropTypes.number, |
| 95 | + borderColor: PropTypes.string, |
| 96 | + color: PropTypes.string, |
| 97 | + size: PropTypes.number, |
| 98 | + unfillColor: PropTypes.string, |
| 99 | + useNativeDriver: PropTypes.bool, |
100 | 100 | text: PropTypes.string, |
| 101 | + fillColor: PropTypes.string, |
| 102 | + fontFamily: PropTypes.string, |
| 103 | + textStyle: PropTypes.object, |
| 104 | + textDecoration: PropTypes.bool, |
101 | 105 | isChecked: PropTypes.bool, |
| 106 | + onPress: PropTypes.func, |
102 | 107 | fontSize: PropTypes.number, |
103 | 108 | iconName: PropTypes.string, |
104 | 109 | iconType: PropTypes.string, |
105 | 110 | iconSize: PropTypes.number, |
106 | 111 | iconColor: PropTypes.string, |
107 | | - textColor: PropTypes.string, |
108 | | - fillColor: PropTypes.string, |
109 | | - fontFamily: PropTypes.string, |
110 | | - unfillColor: PropTypes.string, |
111 | | - useNativeDriver: PropTypes.bool, |
112 | | - disableTextDecoration: PropTypes.bool, |
113 | | -}; |
| 112 | + iconStyle: PropTypes.object, |
| 113 | + iconComponent: PropTypes.element |
| 114 | +} |
114 | 115 |
|
115 | 116 | BouncyCheckbox.defaultProps = { |
| 117 | + size: 25, |
| 118 | + borderWidth: 1, |
| 119 | + borderRadius: 20, |
| 120 | + borderColor: '#ffc484', |
116 | 121 | fontSize: 16, |
117 | 122 | iconSize: 15, |
118 | 123 | isChecked: false, |
119 | | - iconName: "check", |
120 | | - iconType: "Entypo", |
121 | | - textColor: "#757575", |
122 | | - fillColor: "#ffc484", |
123 | | - iconColor: "#fdfdfd", |
| 124 | + iconName: 'check', |
| 125 | + iconType: 'Entypo', |
| 126 | + color: '#757575', |
| 127 | + fillColor: '#ffc484', |
| 128 | + iconColor: '#fdfdfd', |
124 | 129 | useNativeDriver: true, |
125 | | - text: "Call my mom 😇", |
126 | | - unfillColor: "transparent", |
127 | | - disableTextDecoration: false, |
128 | | -}; |
| 130 | + text: 'Call my mom 😇', |
| 131 | + unfillColor: 'transparent', |
| 132 | + textDecoration: false |
| 133 | +} |
129 | 134 |
|
130 | | -export default BouncyCheckbox; |
| 135 | +export default BouncyCheckbox |
0 commit comments