1
1
import _ from 'lodash' ;
2
- import PropTypes from 'prop-types' ;
3
- import React from 'react' ;
4
- import { StyleSheet , Animated , Easing } from 'react-native' ;
2
+ import React , { Component } from 'react' ;
3
+ import { StyleSheet , Animated , Easing , StyleProp , ViewStyle } from 'react-native' ;
5
4
import { Constants } from '../../helpers' ;
6
5
import { Colors , BorderRadiuses } from '../../style' ;
7
- import { BaseComponent } from '../../commons' ;
6
+ import { asBaseComponent } from '../../commons/new ' ;
8
7
import TouchableOpacity from '../touchableOpacity' ;
9
8
10
9
const INNER_PADDING = 2 ;
11
10
const DEFAULT_WIDTH = 42 ;
12
11
const DEFAULT_HEIGHT = 24 ;
13
12
const DEFAULT_THUMB_SIZE = 20 ;
14
13
14
+ export type SwitchProps = {
15
+ /**
16
+ * The value of the switch. If true the switch will be turned on. Default value is false
17
+ */
18
+ value ?: boolean ;
19
+ /**
20
+ * Invoked with the new value when the value changes
21
+ */
22
+ onValueChange ?: ( value : boolean ) => void ;
23
+ /**
24
+ * Whether the switch should be disabled
25
+ */
26
+ disabled ?: boolean ;
27
+ /**
28
+ * The Switch width
29
+ */
30
+ width ?: number ;
31
+ /**
32
+ * The Switch height
33
+ */
34
+ height ?: number ;
35
+ /**
36
+ * The Switch background color when it's turned on
37
+ */
38
+ onColor ?: string ;
39
+ /**
40
+ * The Switch background color when it's turned off
41
+ */
42
+ offColor ?: string ;
43
+ /**
44
+ * The Switch background color when it's disabled
45
+ */
46
+ disabledColor ?: string ;
47
+ /**
48
+ * The Switch's thumb color
49
+ */
50
+ thumbColor ?: string ;
51
+ /**
52
+ * The Switch's thumb size (width & height)
53
+ */
54
+ thumbSize ?: number ;
55
+ /**
56
+ * The Switch's thumb style
57
+ */
58
+ thumbStyle ?: object | number | [ ] ;
59
+ style ?: StyleProp < ViewStyle > ;
60
+ testID ?: string ;
61
+ }
62
+
15
63
/**
16
64
* Switch component for toggling boolean value related to some context
17
65
*/
18
- class Switch extends BaseComponent {
66
+ class Switch extends Component < SwitchProps > {
19
67
static displayName = 'Switch' ;
20
-
21
- static propTypes = {
22
- /**
23
- * The value of the switch. If true the switch will be turned on. Default value is false
24
- */
25
- value : PropTypes . bool ,
26
- /**
27
- * Invoked with the new value when the value changes
28
- */
29
- onValueChange : PropTypes . func ,
30
- /**
31
- * Whether the switch should be disabled
32
- */
33
- disabled : PropTypes . bool ,
34
- /**
35
- * The Switch width
36
- */
37
- width : PropTypes . number ,
38
- /**
39
- * The Switch height
40
- */
41
- height : PropTypes . number ,
42
- /**
43
- * The Switch background color when it's turned on
44
- */
45
- onColor : PropTypes . string ,
46
- /**
47
- * The Switch background color when it's turned off
48
- */
49
- offColor : PropTypes . string ,
50
- /**
51
- * The Switch background color when it's disabled
52
- */
53
- disabledColor : PropTypes . string ,
54
- /**
55
- * The Switch's thumb color
56
- */
57
- thumbColor : PropTypes . string ,
58
- /**
59
- * The Switch's thumb size (width & height)
60
- */
61
- thumbSize : PropTypes . number ,
62
- /**
63
- * The Switch's thumb style
64
- */
65
- thumbStyle : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . number , PropTypes . array ] )
66
- } ;
67
-
68
+
68
69
state = {
69
70
thumbPosition : new Animated . Value ( this . props . value ? 1 : 0 )
70
71
} ;
71
72
72
- generateStyles ( ) {
73
- this . styles = createStyles ( this . getThemeProps ( ) ) ;
74
- }
75
-
76
- componentDidUpdate ( prevProps ) {
77
- const { value} = this . getThemeProps ( ) ;
73
+ styles = createStyles ( this . props ) ;
74
+
75
+ componentDidUpdate ( prevProps : SwitchProps ) {
76
+ const { value} = this . props ;
78
77
if ( prevProps . value !== value ) {
79
78
this . toggle ( value ) ;
80
79
}
81
80
}
82
81
83
82
getAccessibilityProps ( ) {
84
- const { disabled, value} = this . getThemeProps ( ) ;
83
+ const { disabled, value} = this . props ;
85
84
86
85
87
86
return {
@@ -92,7 +91,7 @@ class Switch extends BaseComponent {
92
91
} ;
93
92
}
94
93
95
- toggle ( value ) {
94
+ toggle ( value ?: boolean ) {
96
95
const { thumbPosition} = this . state ;
97
96
98
97
Animated . timing ( thumbPosition , {
@@ -104,15 +103,15 @@ class Switch extends BaseComponent {
104
103
}
105
104
106
105
onPress = ( ) => {
107
- const { disabled} = this . getThemeProps ( ) ;
106
+ const { disabled} = this . props ;
108
107
109
108
if ( ! disabled ) {
110
- _ . invoke ( this . getThemeProps ( ) , 'onValueChange' , ! this . props . value ) ;
109
+ _ . invoke ( this . props , 'onValueChange' , ! this . props . value ) ;
111
110
}
112
111
} ;
113
112
114
113
calcThumbOnPosition ( ) {
115
- const props = this . getThemeProps ( ) ;
114
+ const props = this . props ;
116
115
const width = props . width || DEFAULT_WIDTH ;
117
116
const thumbSize = props . thumbSize || DEFAULT_THUMB_SIZE ;
118
117
let position = width - ( 2 * INNER_PADDING + thumbSize ) ;
@@ -121,8 +120,8 @@ class Switch extends BaseComponent {
121
120
}
122
121
123
122
getSwitchStyle ( ) {
124
- const { value, onColor, offColor, style : propsStyle , disabled, disabledColor} = this . getThemeProps ( ) ;
125
- const style = [ this . styles . switch ] ;
123
+ const { value, onColor, offColor, style : propsStyle , disabled, disabledColor} = this . props ;
124
+ const style : SwitchProps [ 'style' ] = [ this . styles . switch ] ;
126
125
127
126
if ( disabled ) {
128
127
style . push ( disabledColor ? { backgroundColor : disabledColor } : this . styles . switchDisabled ) ;
@@ -137,7 +136,7 @@ class Switch extends BaseComponent {
137
136
}
138
137
139
138
renderThumb ( ) {
140
- const { thumbStyle} = this . getThemeProps ( ) ;
139
+ const { thumbStyle} = this . props ;
141
140
const { thumbPosition} = this . state ;
142
141
143
142
const interpolatedTranslateX = thumbPosition . interpolate ( {
@@ -153,9 +152,9 @@ class Switch extends BaseComponent {
153
152
}
154
153
155
154
render ( ) {
156
- const { ...others } = this . getThemeProps ( ) ;
157
-
155
+ const { ...others } = this . props ;
158
156
return (
157
+ // @ts -ignore
159
158
< TouchableOpacity
160
159
{ ...this . getAccessibilityProps ( ) }
161
160
activeOpacity = { 1 }
@@ -204,4 +203,4 @@ function createStyles({
204
203
} ) ;
205
204
}
206
205
207
- export default Switch ;
206
+ export default asBaseComponent < SwitchProps > ( Switch ) ;
0 commit comments