1
1
import _ from 'lodash' ;
2
2
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' ;
4
12
import { Colors } from '../../style' ;
5
13
//@ts -ignore
6
14
import Assets from '../../assets' ;
7
15
import { asBaseComponent } from '../../commons/new' ;
8
16
import TouchableOpacity from '../touchableOpacity' ;
17
+ import Text from '../text' ;
18
+ import View from '../view' ;
19
+ import { Spacings } from '../../style' ;
9
20
10
21
const DEFAULT_SIZE = 24 ;
11
22
const DEFAULT_COLOR = Colors . blue30 ;
@@ -45,15 +56,28 @@ export interface CheckboxPropTypes extends TouchableOpacityProps {
45
56
* The selected icon color
46
57
*/
47
58
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 > ;
48
67
/**
49
68
* Additional styling
50
69
*/
51
- style ?: StyleProp < ViewStyle >
70
+ style ?: StyleProp < ViewStyle > ;
71
+ /**
72
+ * Additional styling for checkbox and label container
73
+ */
74
+ containerStyle ?: StyleProp < ViewStyle > ;
75
+
52
76
}
53
77
54
78
interface CheckboxState {
55
79
isChecked : Animated . Value ;
56
- } ;
80
+ }
57
81
58
82
/**
59
83
* @description : Checkbox component for toggling boolean value related to some context
@@ -68,6 +92,7 @@ class Checkbox extends Component<CheckboxPropTypes, CheckboxState> {
68
92
styles : {
69
93
container : StyleProp < ViewStyle > ;
70
94
selectedIcon : StyleProp < ViewStyle > ;
95
+ checkboxLabel : StyleProp < TextStyle > ;
71
96
} ;
72
97
73
98
animationStyle : {
@@ -155,34 +180,45 @@ class Checkbox extends Component<CheckboxPropTypes, CheckboxState> {
155
180
}
156
181
157
182
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 ;
159
184
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
174
197
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 }
179
201
] }
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 >
186
222
) ;
187
223
}
188
224
}
@@ -203,6 +239,10 @@ function createStyles(props: CheckboxPropTypes) {
203
239
tintColor : iconColor ,
204
240
alignItems : 'center' ,
205
241
justifyContent : 'center'
242
+ } ,
243
+ checkboxLabel : {
244
+ marginLeft : Spacings . s3 ,
245
+ alignSelf : 'center'
206
246
}
207
247
} ) ;
208
248
}
0 commit comments