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