Skip to content

Commit 9c577d9

Browse files
committed
Checkbox with states and line-through feature is added
1 parent 7933014 commit 9c577d9

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

example/App.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ const App = () => {
77
<View style={{ flex: 1 }}>
88
<StatusBar barStyle="dark-content" />
99
<SafeAreaView style={{ flex: 1 }}>
10-
<BouncyCheckbox text="Call my mom" />
11-
<BouncyCheckbox text="Call my mom" />
12-
<BouncyCheckbox text="Call my mom" />
13-
<BouncyCheckbox text="Call my mom" />
10+
<View style={{ margin: 16 }}>
11+
<BouncyCheckbox />
12+
<BouncyCheckbox text="Get groceries" />
13+
<BouncyCheckbox text="Buy tickets for concert" />
14+
<BouncyCheckbox text="Work on inbox zero" />
15+
<BouncyCheckbox text="Do a load of laundry" isChecked />
16+
<BouncyCheckbox text="Try new gym routine" isChecked />
17+
</View>
1418
</SafeAreaView>
1519
</View>
1620
);

example/lib/src/BouncyCheckbox.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from "react";
22
import PropTypes from "prop-types";
33
import { Animated, Easing, Text, TouchableOpacity, View } from "react-native";
44
import Icon from "react-native-dynamic-vector-icons";
5+
import { iconContainer } from "./BouncyCheckbox.style";
56

67
class BouncyCheckbox extends Component {
78
constructor(props) {
@@ -12,6 +13,10 @@ class BouncyCheckbox extends Component {
1213
};
1314
}
1415

16+
componentDidMount() {
17+
this.setState({ checked: this.props.isChecked });
18+
}
19+
1520
setChecked = () => {
1621
const { checked } = this.state;
1722
this.setState({ checked: !checked });
@@ -20,38 +25,53 @@ class BouncyCheckbox extends Component {
2025
spring = () => {
2126
this.setChecked();
2227
const { springValue } = this.state;
28+
const { onPress } = this.props;
2329
springValue.setValue(0.7);
2430
Animated.spring(springValue, {
2531
toValue: 1,
2632
friction: 3
2733
}).start();
34+
// Outside of the onPress function
35+
if (onPress) onPress();
2836
};
2937

3038
renderCheckIcon = () => {
3139
const { checked, springValue } = this.state;
40+
const { checkboxSize, borderColor, fillColor, unfillColor } = this.props;
3241
return (
3342
<Animated.View
3443
style={[
35-
{
36-
width: 35,
37-
height: 35,
38-
borderWidth: 1,
39-
borderRadius: 35,
40-
borderColor: "orange",
41-
alignItems: "center",
42-
justifyContent: "center",
43-
backgroundColor: checked ? "orange" : "transparent"
44-
},
44+
iconContainer(
45+
checkboxSize,
46+
checked,
47+
borderColor,
48+
fillColor,
49+
unfillColor
50+
),
4551
{ transform: [{ scale: springValue }] }
4652
]}
4753
>
48-
<Icon name="check" type="Entypo" size={20} color="white" />
54+
<Icon
55+
{...this.props}
56+
name="check"
57+
type="Entypo"
58+
size={15}
59+
color="white"
60+
/>
4961
</Animated.View>
5062
);
5163
};
5264

5365
render() {
54-
const { text, onPress } = this.props;
66+
const {
67+
text,
68+
size,
69+
isChecked,
70+
borderColor,
71+
fillColor,
72+
unfillColor,
73+
onPress
74+
} = this.props;
5575

5676
return (
5777
<TouchableOpacity
@@ -66,12 +86,13 @@ class BouncyCheckbox extends Component {
6686
<View style={{ marginLeft: 16 }}>
6787
<Text
6888
style={{
89+
fontSize: 16,
6990
color: "#757575",
70-
fontSize: 18,
71-
fontFamily: "JosefinSans-Regular"
91+
fontFamily: "JosefinSans-Regular",
92+
textDecorationLine: this.state.checked ? "line-through" : "none"
7293
}}
7394
>
74-
Call my mom
95+
{text}
7596
</Text>
7697
</View>
7798
</TouchableOpacity>
@@ -80,11 +101,21 @@ class BouncyCheckbox extends Component {
80101
}
81102

82103
BouncyCheckbox.propTypes = {
83-
example: PropTypes.number
104+
text: PropTypes.string,
105+
isChecked: PropTypes.bool,
106+
checkboxSize: PropTypes.number,
107+
borderColor: PropTypes.string,
108+
fillColor: PropTypes.string,
109+
unfillColor: PropTypes.string
84110
};
85111

86112
BouncyCheckbox.defaultProps = {
87-
example: 5
113+
checkboxSize: 25,
114+
isChecked: false,
115+
text: "Call my mom",
116+
fillColor: "#f09f48",
117+
borderColor: "#f09f48",
118+
unfillColor: "transparent"
88119
};
89120

90121
export default BouncyCheckbox;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1+
export const iconContainer = (
2+
size,
3+
checked,
4+
borderColor,
5+
fillColor,
6+
unfillColor
7+
) => {
8+
return {
9+
width: size,
10+
height: size,
11+
borderColor,
12+
borderWidth: 1,
13+
borderRadius: size,
14+
alignItems: "center",
15+
justifyContent: "center",
16+
backgroundColor: checked ? fillColor : unfillColor
17+
};
18+
};
19+
120
export default {};

0 commit comments

Comments
 (0)