Skip to content

Commit 172ac90

Browse files
committed
fix: onPress functionality logic
1 parent 214371b commit 172ac90

File tree

7 files changed

+267
-20
lines changed

7 files changed

+267
-20
lines changed

lib/BouncyCheckbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ class BouncyCheckbox extends React.Component<IBouncyCheckboxProps, IState> {
184184
}}
185185
onPressOut={() => {
186186
this.bounceEffect(bounceEffectOut, bounceVelocityOut, bouncinessOut);
187-
this.handleCheck();
188187
}}
188+
onPress={this.handleCheck}
189189
>
190190
{this.renderCheckIcon()}
191191
{this.renderCheckboxText()}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-bouncy-checkbox",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Fully customizable animated bouncy checkbox for React Native",
55
"keywords": [
66
"bouncy",

v3example/App.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import {
3+
Alert,
34
SafeAreaView,
45
ScrollView,
56
StatusBar,
@@ -8,7 +9,8 @@ import {
89
useColorScheme,
910
View,
1011
} from 'react-native';
11-
import BouncyCheckbox from 'react-native-bouncy-checkbox';
12+
// import BouncyCheckbox from 'react-native-bouncy-checkbox';
13+
import BouncyCheckbox from './lib/BouncyCheckbox';
1214

1315
const App = () => {
1416
return (
@@ -18,15 +20,22 @@ const App = () => {
1820
alignItems: 'center',
1921
justifyContent: 'center',
2022
}}>
21-
<BouncyCheckbox size={50} onPress={() => {}} />
23+
<BouncyCheckbox
24+
size={50}
25+
onPress={isChecked => {
26+
console.log('OUTSIDE: ', isChecked);
27+
}}
28+
/>
2229
<BouncyCheckbox
2330
onPress={() => {}}
2431
unfillColor={'#030'} //? Inner color of the checkbox
2532
fillColor="#faf" //? Outer color (radius) of the checkbox
2633
isChecked={true} //? Checks initial state, doesn't update state yet
2734
size={35} //? Size of the checkbox
28-
innerIconStyle={{
29-
borderWidth: 3, //? Make the TODO checkbox thicker than default
35+
iconStyle={{
36+
borderWidth: 3,
37+
alignItems: 'center',
38+
justifyContent: 'center',
3039
}}
3140
/>
3241
</SafeAreaView>

v3example/__tests__/App-test.tsx

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ViewStyle, TextStyle, StyleSheet } from "react-native";
2+
3+
export const _iconContainer = (
4+
size: number,
5+
checked: boolean,
6+
fillColor: string,
7+
unfillColor: string,
8+
): ViewStyle => {
9+
return {
10+
width: size,
11+
height: size,
12+
borderRadius: size / 2,
13+
alignItems: "center",
14+
justifyContent: "center",
15+
backgroundColor: checked ? fillColor : unfillColor,
16+
};
17+
};
18+
19+
export const _textStyle = (checked: boolean): TextStyle => {
20+
return {
21+
fontSize: 16,
22+
color: "#757575",
23+
textDecorationLine: checked ? "line-through" : "none",
24+
};
25+
};
26+
27+
export default StyleSheet.create<any>({
28+
container: {
29+
alignItems: "center",
30+
flexDirection: "row",
31+
},
32+
iconImageStyle: {
33+
width: 10,
34+
height: 10,
35+
},
36+
textContainer: {
37+
marginLeft: 16,
38+
},
39+
iconContainer: (
40+
size: number,
41+
checked: boolean,
42+
fillColor: string,
43+
unfillColor: string,
44+
) => ({
45+
width: size,
46+
height: size,
47+
borderRadius: size / 2,
48+
backgroundColor: checked ? fillColor : unfillColor,
49+
}),
50+
innerIconContainer: (size: number, fillColor: string) => ({
51+
width: size,
52+
height: size,
53+
borderWidth: 1,
54+
borderColor: fillColor,
55+
borderRadius: size / 2,
56+
alignItems: "center",
57+
justifyContent: "center",
58+
}),
59+
});

v3example/lib/BouncyCheckbox.tsx

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import * as React from 'react';
2+
import {
3+
Text,
4+
View,
5+
Image,
6+
Animated,
7+
StyleProp,
8+
ViewStyle,
9+
TextStyle,
10+
Pressable,
11+
ImageSourcePropType,
12+
TouchableWithoutFeedbackProps,
13+
} from 'react-native';
14+
import styles, {_textStyle} from './BouncyCheckbox.style';
15+
16+
type CustomStyleProp = StyleProp<ViewStyle> | Array<StyleProp<ViewStyle>>;
17+
type CustomTextStyleProp = StyleProp<TextStyle> | Array<StyleProp<TextStyle>>;
18+
type BaseTouchableProps = Pick<
19+
TouchableWithoutFeedbackProps,
20+
Exclude<keyof TouchableWithoutFeedbackProps, 'onPress'>
21+
>;
22+
23+
export interface IBouncyCheckboxProps extends BaseTouchableProps {
24+
size?: number;
25+
text?: string;
26+
fillColor?: string;
27+
isChecked?: boolean;
28+
unfillColor?: string;
29+
disableText?: boolean;
30+
bounceEffect?: number;
31+
bounceFriction?: number;
32+
useNativeDriver?: boolean;
33+
disableBuiltInState?: boolean;
34+
ImageComponent?: any;
35+
TouchableComponent?: any;
36+
bounceEffectIn?: number;
37+
bounceEffectOut?: number;
38+
bounceVelocityIn?: number;
39+
bounceVelocityOut?: number;
40+
bouncinessIn?: number;
41+
bouncinessOut?: number;
42+
iconComponent?: React.ReactNode;
43+
textComponent?: React.ReactNode;
44+
iconStyle?: CustomStyleProp;
45+
innerIconStyle?: CustomStyleProp;
46+
style?: CustomStyleProp;
47+
textStyle?: CustomTextStyleProp;
48+
iconImageStyle?: CustomStyleProp;
49+
textContainerStyle?: CustomStyleProp;
50+
checkIconImageSource?: ImageSourcePropType;
51+
onPress?: (checked: boolean) => void;
52+
}
53+
54+
interface IState {
55+
checked: boolean;
56+
springValue: Animated.Value;
57+
bounceValue: Animated.Value;
58+
}
59+
60+
const defaultCheckImage = require('./check.png');
61+
62+
class BouncyCheckbox extends React.Component<IBouncyCheckboxProps, IState> {
63+
constructor(props: IBouncyCheckboxProps) {
64+
super(props);
65+
this.state = {
66+
checked: false,
67+
springValue: new Animated.Value(1),
68+
bounceValue: new Animated.Value(1),
69+
};
70+
}
71+
72+
componentDidMount() {
73+
this.setState({checked: this.props.isChecked || false});
74+
}
75+
76+
bounceEffect = (value: number, velocity: number, bounciness: number) => {
77+
const {useNativeDriver = true} = this.props;
78+
Animated.spring(this.state.bounceValue, {
79+
toValue: value,
80+
velocity,
81+
bounciness,
82+
useNativeDriver,
83+
}).start();
84+
};
85+
86+
renderCheckIcon = () => {
87+
const {checked} = this.state;
88+
const {
89+
size = 25,
90+
iconStyle,
91+
iconComponent,
92+
iconImageStyle,
93+
fillColor = '#ffc484',
94+
ImageComponent = Image,
95+
unfillColor = 'transparent',
96+
disableBuiltInState,
97+
isChecked,
98+
innerIconStyle,
99+
checkIconImageSource = defaultCheckImage,
100+
} = this.props;
101+
102+
const checkStatus = disableBuiltInState ? isChecked! : checked;
103+
return (
104+
<Animated.View
105+
style={[
106+
{transform: [{scale: this.state.bounceValue}]},
107+
styles.iconContainer(size, checkStatus, fillColor, unfillColor),
108+
iconStyle,
109+
]}>
110+
<View
111+
style={[styles.innerIconContainer(size, fillColor), innerIconStyle]}>
112+
{iconComponent ||
113+
(checkStatus && (
114+
<ImageComponent
115+
source={checkIconImageSource}
116+
style={[styles.iconImageStyle, iconImageStyle]}
117+
/>
118+
))}
119+
</View>
120+
</Animated.View>
121+
);
122+
};
123+
124+
renderCheckboxText = () => {
125+
const {
126+
text,
127+
textComponent,
128+
isChecked,
129+
textStyle,
130+
textContainerStyle,
131+
disableBuiltInState,
132+
disableText = false,
133+
} = this.props;
134+
const {checked} = this.state;
135+
const checkDisableTextType = typeof disableText === 'undefined';
136+
return (
137+
(!disableText || checkDisableTextType) &&
138+
(textComponent || (
139+
<View style={[styles.textContainer, textContainerStyle]}>
140+
<Text
141+
style={[
142+
_textStyle(disableBuiltInState ? isChecked! : checked),
143+
textStyle,
144+
]}>
145+
{text}
146+
</Text>
147+
</View>
148+
))
149+
);
150+
};
151+
152+
handleCheck = () => {
153+
const {disableBuiltInState = false} = this.props;
154+
const {checked} = this.state;
155+
if (!disableBuiltInState) {
156+
this.setState({checked: !checked}, () => {
157+
this.props.onPress && this.props.onPress(this.state.checked);
158+
});
159+
} else {
160+
this.props.onPress && this.props.onPress(this.state.checked);
161+
}
162+
};
163+
164+
render() {
165+
const {
166+
style,
167+
bounceEffectIn = 0.9,
168+
bounceEffectOut = 1,
169+
bounceVelocityIn = 0.1,
170+
bounceVelocityOut = 0.4,
171+
bouncinessIn = 20,
172+
bouncinessOut = 20,
173+
TouchableComponent = Pressable,
174+
} = this.props;
175+
return (
176+
<TouchableComponent
177+
{...this.props}
178+
style={[styles.container, style]}
179+
onPressIn={() => {
180+
this.bounceEffect(bounceEffectIn, bounceVelocityIn, bouncinessIn);
181+
}}
182+
onPressOut={() => {
183+
this.bounceEffect(bounceEffectOut, bounceVelocityOut, bouncinessOut);
184+
}}
185+
onPress={this.handleCheck}>
186+
{this.renderCheckIcon()}
187+
{this.renderCheckboxText()}
188+
</TouchableComponent>
189+
);
190+
}
191+
}
192+
193+
export default BouncyCheckbox;

v3example/lib/check.png

675 Bytes
Loading

0 commit comments

Comments
 (0)