Skip to content

Commit 2738154

Browse files
authored
Feat/add label support to checkbox (#959)
* label support in checkbox * generetedTypes * typescript * typeScript * change View to uilib's View * generatedTypes * support pressing the label * remove View import * fix checkbox bug * checkbox label fixes * add containerStyle prop to checkbox
1 parent 46417c4 commit 2738154

File tree

3 files changed

+94
-33
lines changed

3 files changed

+94
-33
lines changed

demo/src/screens/componentScreens/CheckboxScreen.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React, {Component} from 'react';
2-
import {Checkbox, Assets, Text, View, Colors} from 'react-native-ui-lib'; //eslint-disable-line
3-
2+
import {Checkbox, Assets, Text, View, Colors, Spacings} from 'react-native-ui-lib'; //eslint-disable-line
43

54
class CheckboxScreen extends Component {
65
state = {
76
value1: false,
87
value2: false,
98
value3: true,
109
value4: true,
11-
value5: false
10+
value5: false,
11+
value6: false
1212
};
1313

1414
render() {
@@ -21,6 +21,13 @@ class CheckboxScreen extends Component {
2121
onValueChange={value1 => this.setState({value1})}
2222
style={{marginBottom: 20}}
2323
/>
24+
<Checkbox
25+
value={this.state.value6}
26+
label={'With label'}
27+
color={Colors.green20}
28+
onValueChange={value6 => this.setState({value6})}
29+
containerStyle={{marginBottom: 20, marginLeft: 75}}
30+
/>
2431
<Checkbox
2532
value={this.state.value2}
2633
onValueChange={value2 => this.setState({value2})}
@@ -40,7 +47,9 @@ class CheckboxScreen extends Component {
4047
style={{marginBottom: 20}}
4148
/>
4249
<View row marginB-20>
43-
<Text text70 centerV>Disabled: </Text>
50+
<Text text70 centerV>
51+
Disabled:{' '}
52+
</Text>
4453
<Checkbox
4554
disabled
4655
value={this.state.value5}

generatedTypes/components/checkbox/index.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { StyleProp, TouchableOpacityProps, ViewStyle } from 'react-native';
2+
import { StyleProp, TouchableOpacityProps, ViewStyle, TextStyle } from 'react-native';
33
export interface CheckboxPropTypes extends TouchableOpacityProps {
44
/**
55
* The value of the Checkbox. If true the switch will be turned on. Default value is false.
@@ -33,10 +33,22 @@ export interface CheckboxPropTypes extends TouchableOpacityProps {
3333
* The selected icon color
3434
*/
3535
iconColor?: string;
36+
/**
37+
* The label of the checkbox
38+
*/
39+
label?: string;
40+
/**
41+
* The style of the label
42+
*/
43+
labelStyle?: StyleProp<TextStyle>;
3644
/**
3745
* Additional styling
3846
*/
3947
style?: StyleProp<ViewStyle>;
48+
/**
49+
* Additional styling for checkbox and label container
50+
*/
51+
containerStyle?: StyleProp<ViewStyle>;
4052
}
4153
declare const _default: React.ComponentClass<CheckboxPropTypes & {
4254
useCustomTheme?: boolean | undefined;

src/components/checkbox/index.tsx

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
3-
import {Animated, Easing, StyleSheet, StyleProp, TouchableOpacityProps, ViewStyle} from 'react-native';
3+
import {
4+
Animated,
5+
Easing,
6+
StyleSheet,
7+
StyleProp,
8+
TouchableOpacityProps,
9+
ViewStyle,
10+
TextStyle
11+
} from 'react-native';
412
import {Colors} from '../../style';
513
//@ts-ignore
614
import Assets from '../../assets';
715
import {asBaseComponent} from '../../commons/new';
816
import TouchableOpacity from '../touchableOpacity';
17+
import Text from '../text';
18+
import View from '../view';
19+
import {Spacings} from '../../style';
920

1021
const DEFAULT_SIZE = 24;
1122
const DEFAULT_COLOR = Colors.blue30;
@@ -45,15 +56,28 @@ export interface CheckboxPropTypes extends TouchableOpacityProps {
4556
* The selected icon color
4657
*/
4758
iconColor?: string;
59+
/**
60+
* The label of the checkbox
61+
*/
62+
label?: string;
63+
/**
64+
* The style of the label
65+
*/
66+
labelStyle?: StyleProp<TextStyle>;
4867
/**
4968
* Additional styling
5069
*/
51-
style?: StyleProp<ViewStyle>
70+
style?: StyleProp<ViewStyle>;
71+
/**
72+
* Additional styling for checkbox and label container
73+
*/
74+
containerStyle?: StyleProp<ViewStyle>;
75+
5276
}
5377

5478
interface CheckboxState {
5579
isChecked: Animated.Value;
56-
};
80+
}
5781

5882
/**
5983
* @description: Checkbox component for toggling boolean value related to some context
@@ -68,6 +92,7 @@ class Checkbox extends Component<CheckboxPropTypes, CheckboxState> {
6892
styles: {
6993
container: StyleProp<ViewStyle>;
7094
selectedIcon: StyleProp<ViewStyle>;
95+
checkboxLabel: StyleProp<TextStyle>;
7196
};
7297

7398
animationStyle: {
@@ -155,34 +180,45 @@ class Checkbox extends Component<CheckboxPropTypes, CheckboxState> {
155180
}
156181

157182
render() {
158-
const {selectedIcon, color, iconColor, disabled, testID, style, ...others} = this.props;
183+
const {selectedIcon, color, iconColor, disabled, testID, label, labelStyle, style, containerStyle, ...others} = this.props;
159184
return (
160-
// @ts-ignore
161-
<TouchableOpacity
162-
{...this.getAccessibilityProps()}
163-
activeOpacity={1}
164-
testID={testID}
165-
{...others}
166-
style={[this.getBorderStyle(), style]}
167-
onPress={this.onPress}
168-
>
169-
{
170-
<Animated.View
171-
style={[this.styles.container, {backgroundColor: this.getColor()}, {opacity: this.animationStyle.opacity}]}
172-
>
173-
<Animated.Image
185+
<View row style={containerStyle}>
186+
{/*@ts-ignore*/}
187+
<TouchableOpacity
188+
{...this.getAccessibilityProps()}
189+
activeOpacity={1}
190+
testID={testID}
191+
{...others}
192+
style={[this.getBorderStyle(), style]}
193+
onPress={this.onPress}
194+
>
195+
{
196+
<Animated.View
174197
style={[
175-
this.styles.selectedIcon,
176-
color && {tintColor: iconColor},
177-
{transform: this.animationStyle.transform},
178-
disabled && {tintColor: DEFAULT_ICON_COLOR}
198+
this.styles.container,
199+
{backgroundColor: this.getColor()},
200+
{opacity: this.animationStyle.opacity}
179201
]}
180-
source={selectedIcon || Assets.icons.checkSmall}
181-
testID={`${testID}.selected`}
182-
/>
183-
</Animated.View>
184-
}
185-
</TouchableOpacity>
202+
>
203+
<Animated.Image
204+
style={[
205+
this.styles.selectedIcon,
206+
color && {tintColor: iconColor},
207+
{transform: this.animationStyle.transform},
208+
disabled && {tintColor: DEFAULT_ICON_COLOR}
209+
]}
210+
source={selectedIcon || Assets.icons.checkSmall}
211+
testID={`${testID}.selected`}
212+
/>
213+
</Animated.View>
214+
}
215+
</TouchableOpacity>
216+
{label && (
217+
<Text style={[this.styles.checkboxLabel, labelStyle]} onPress={this.onPress}>
218+
{label}
219+
</Text>
220+
)}
221+
</View>
186222
);
187223
}
188224
}
@@ -203,6 +239,10 @@ function createStyles(props: CheckboxPropTypes) {
203239
tintColor: iconColor,
204240
alignItems: 'center',
205241
justifyContent: 'center'
242+
},
243+
checkboxLabel: {
244+
marginLeft: Spacings.s3,
245+
alignSelf: 'center'
206246
}
207247
});
208248
}

0 commit comments

Comments
 (0)