1
1
import _ from 'lodash' ;
2
- import PropTypes from 'prop-types' ;
3
- import React from 'react' ;
4
- import { Animated , Easing , StyleSheet } from 'react-native' ;
2
+ import React , { Component } from 'react' ;
3
+ import { Animated , Easing , StyleSheet , StyleProp , TouchableOpacityProps , ViewStyle } from 'react-native' ;
5
4
import { Colors } from '../../style' ;
5
+ //@ts -ignore
6
6
import Assets from '../../assets' ;
7
- import { BaseComponent } from '../../commons' ;
7
+ import { asBaseComponent } from '../../commons/new ' ;
8
8
import TouchableOpacity from '../touchableOpacity' ;
9
9
10
10
const DEFAULT_SIZE = 24 ;
11
11
const DEFAULT_COLOR = Colors . blue30 ;
12
12
const DEFAULT_ICON_COLOR = Colors . white ;
13
13
const DEFAULT_DISABLED_COLOR = Colors . dark70 ;
14
14
15
+ interface CheckboxProps {
16
+ /**
17
+ * The value of the Checkbox. If true the switch will be turned on. Default value is false.
18
+ */
19
+ value ?: boolean ;
20
+ /**
21
+ * Invoked with the new value when the value changes.
22
+ */
23
+ onValueChange ?: Function ;
24
+ /**
25
+ * Whether the checkbox should be disabled
26
+ */
27
+ disabled ?: boolean ;
28
+ /**
29
+ * The Checkbox color
30
+ */
31
+ color ?: string ;
32
+ /**
33
+ * The size of the checkbox. affect both width and height
34
+ */
35
+ size ?: number ;
36
+ /**
37
+ * The Checkbox border radius
38
+ */
39
+ borderRadius ?: number ;
40
+ /**
41
+ * The icon asset to use for the selected indication (accept only local assets)
42
+ */
43
+ selectedIcon ?: number ;
44
+ /**
45
+ * The selected icon color
46
+ */
47
+ iconColor ?: string ;
48
+ /**
49
+ * Additional styling
50
+ */
51
+ style ?: StyleProp < ViewStyle >
52
+ }
53
+
54
+ type CheckboxState = {
55
+ isChecked : Animated . Value ;
56
+ } ;
57
+
58
+ type Props = CheckboxProps & TouchableOpacityProps ;
59
+
15
60
/**
16
- * Checkbox component for toggling boolean value related to some context
61
+ * @description : Checkbox component for toggling boolean value related to some context
62
+ * @extends : TouchableOpacity
63
+ * @extendslink : docs/TouchableOpacity
64
+ * @gif : https://media.giphy.com/media/xULW8j5WzsuPytqklq/giphy.gif
65
+ * @example : https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/CheckboxScreen.tsx
17
66
*/
18
- class Checkbox extends BaseComponent {
67
+ class Checkbox extends Component < Props , CheckboxState > {
19
68
static displayName = 'Checkbox' ;
20
- static propTypes = {
21
- /**
22
- * The value of the Checkbox. If true the switch will be turned on. Default value is false.
23
- */
24
- value : PropTypes . bool ,
25
- /**
26
- * Invoked with the new value when the value changes.
27
- */
28
- onValueChange : PropTypes . func ,
29
- /**
30
- * Whether the checkbox should be disabled
31
- */
32
- disabled : PropTypes . bool ,
33
- /**
34
- * The Checkbox color
35
- */
36
- color : PropTypes . string ,
37
- /**
38
- * The size of the checkbox. affect both width and height
39
- */
40
- size : PropTypes . number ,
41
- /**
42
- * The Checkbox border radius
43
- */
44
- borderRadius : PropTypes . number ,
45
- /**
46
- * The icon asset to use for the selected indication (accept only local assets)
47
- */
48
- selectedIcon : PropTypes . number ,
49
- /**
50
- * The selected icon color
51
- */
52
- iconColor : PropTypes . string
69
+
70
+ styles : {
71
+ container : StyleProp < ViewStyle > ;
72
+ selectedIcon : StyleProp < ViewStyle > ;
53
73
} ;
54
74
55
- constructor ( props ) {
75
+ animationStyle : {
76
+ opacity : CheckboxState [ 'isChecked' ] ;
77
+ transform : [
78
+ {
79
+ scaleX : CheckboxState [ 'isChecked' ] ;
80
+ } ,
81
+ {
82
+ scaleY : CheckboxState [ 'isChecked' ] ;
83
+ }
84
+ ] ;
85
+ } ;
86
+
87
+ constructor ( props : Props ) {
56
88
super ( props ) ;
57
89
58
90
this . state = {
59
91
isChecked : new Animated . Value ( this . props . value ? 1 : 0 )
60
92
} ;
61
93
94
+ this . styles = createStyles ( props ) ;
95
+
62
96
this . animationStyle = {
63
97
opacity : this . state . isChecked ,
64
98
transform : [
@@ -72,15 +106,15 @@ class Checkbox extends BaseComponent {
72
106
} ;
73
107
}
74
108
75
- componentDidUpdate ( prevProps ) {
76
- const { value} = this . getThemeProps ( ) ;
109
+ componentDidUpdate ( prevProps : Props ) {
110
+ const { value} = this . props ;
77
111
if ( prevProps . value !== value ) {
78
112
this . animateCheckbox ( value ) ;
79
113
}
80
114
}
81
115
82
116
getAccessibilityProps ( ) {
83
- const { accessibilityLabel, disabled, value} = this . getThemeProps ( ) ;
117
+ const { accessibilityLabel, disabled, value} = this . props ;
84
118
const checkedState = value ? 'checked' : 'unchecked' ;
85
119
86
120
return {
@@ -91,11 +125,7 @@ class Checkbox extends BaseComponent {
91
125
} ;
92
126
}
93
127
94
- generateStyles ( ) {
95
- this . styles = createStyles ( this . getThemeProps ( ) ) ;
96
- }
97
-
98
- animateCheckbox ( value ) {
128
+ animateCheckbox ( value : CheckboxProps [ 'value' ] ) {
99
129
const { isChecked} = this . state ;
100
130
101
131
Animated . timing ( isChecked , {
@@ -107,35 +137,35 @@ class Checkbox extends BaseComponent {
107
137
}
108
138
109
139
onPress = ( ) => {
110
- const { disabled} = this . getThemeProps ( ) ;
140
+ const { disabled} = this . props ;
111
141
112
142
if ( ! disabled ) {
113
143
_ . invoke ( this . props , 'onValueChange' , ! this . props . value ) ;
114
144
}
115
145
} ;
116
146
117
147
getColor ( ) {
118
- const { color, disabled} = this . getThemeProps ( ) ;
148
+ const { color, disabled} = this . props ;
119
149
return disabled ? DEFAULT_DISABLED_COLOR : color || DEFAULT_COLOR ;
120
150
}
121
151
122
152
getBorderStyle ( ) {
123
- const { style : propsStyle } = this . getThemeProps ( ) ;
124
153
const borderColor = { borderColor : this . getColor ( ) } ;
125
- const style = [ this . styles . container , { borderWidth : 2 } , borderColor , propsStyle ] ;
154
+ const borderStyle = [ this . styles . container , { borderWidth : 2 } , borderColor ] ;
126
155
127
- return style ;
156
+ return borderStyle ;
128
157
}
129
158
130
159
render ( ) {
131
- const { selectedIcon, color, iconColor, disabled, testID, ...others } = this . getThemeProps ( ) ;
160
+ const { selectedIcon, color, iconColor, disabled, testID, style , ...others } = this . props ;
132
161
return (
162
+ // @ts -ignore
133
163
< TouchableOpacity
134
164
{ ...this . getAccessibilityProps ( ) }
135
165
activeOpacity = { 1 }
136
166
testID = { testID }
137
167
{ ...others }
138
- style = { this . getBorderStyle ( ) }
168
+ style = { [ this . getBorderStyle ( ) , style ] }
139
169
onPress = { this . onPress }
140
170
>
141
171
{
@@ -159,7 +189,9 @@ class Checkbox extends BaseComponent {
159
189
}
160
190
}
161
191
162
- function createStyles ( { color = DEFAULT_COLOR , iconColor = DEFAULT_ICON_COLOR , size = DEFAULT_SIZE , borderRadius} ) {
192
+ function createStyles ( props : Props ) {
193
+ const { color = DEFAULT_COLOR , iconColor = DEFAULT_ICON_COLOR , size = DEFAULT_SIZE , borderRadius} = props ;
194
+
163
195
return StyleSheet . create ( {
164
196
container : {
165
197
width : size ,
@@ -177,4 +209,4 @@ function createStyles({color = DEFAULT_COLOR, iconColor = DEFAULT_ICON_COLOR, si
177
209
} ) ;
178
210
}
179
211
180
- export default Checkbox ;
212
+ export default asBaseComponent < Props > ( Checkbox ) ;
0 commit comments