10
10
11
11
'use strict' ;
12
12
13
- const ColorPropType = require ( 'ColorPropType' ) ;
14
- const NativeMethodsMixin = require ( 'NativeMethodsMixin' ) ;
15
13
const Platform = require ( 'Platform' ) ;
16
14
const React = require ( 'React' ) ;
17
15
const ReactNative = require ( 'ReactNative' ) ;
18
- const PropTypes = require ( 'prop-types' ) ;
19
16
const StyleSheet = require ( 'StyleSheet' ) ;
20
- const ViewPropTypes = require ( 'ViewPropTypes' ) ;
21
17
22
- const createReactClass = require ( 'create-react-class ' ) ;
18
+ const nullthrows = require ( 'fbjs/lib/nullthrows ' ) ;
23
19
const requireNativeComponent = require ( 'requireNativeComponent' ) ;
24
20
25
21
import type { ColorValue } from 'StyleSheetTypes' ;
26
22
import type { ViewProps } from 'ViewPropTypes' ;
27
23
28
- const RCTSwitch =
29
- Platform . OS === 'android'
30
- ? requireNativeComponent ( 'AndroidSwitch' )
31
- : requireNativeComponent ( 'RCTSwitch' ) ;
32
-
33
- type DefaultProps = $ReadOnly < { |
34
- value : boolean ,
35
- disabled : boolean ,
36
- | } > ;
37
-
38
24
type Props = $ReadOnly < { |
39
25
...ViewProps ,
26
+ /**
27
+ * The value of the switch. If true the switch will be turned on.
28
+ * Default value is false.
29
+ */
40
30
value ?: ?boolean ,
31
+
32
+ /**
33
+ * If true the user won't be able to toggle the switch.
34
+ * Default value is false.
35
+ */
41
36
disabled ?: ?boolean ,
37
+
38
+ /**
39
+ * Switch change handler.
40
+ *
41
+ * Invoked with the event when the value changes. For getting the value
42
+ * the switch was changed to use onValueChange instead.
43
+ */
44
+ onChange ?: ?Function ,
45
+
46
+ /**
47
+ * Invoked with the new value when the value changes.
48
+ */
42
49
onValueChange ?: ?Function ,
50
+
51
+ /**
52
+ * Used to locate this view in end-to-end tests.
53
+ */
43
54
testID ?: ?string ,
55
+
56
+ /**
57
+ * Border color on iOS and background color on Android when the switch is turned off.
58
+ */
44
59
tintColor ?: ?ColorValue ,
60
+
61
+ /**
62
+ * Background color when the switch is turned on.
63
+ */
45
64
onTintColor ?: ?ColorValue ,
65
+
66
+ /**
67
+ * Color of the foreground switch grip.
68
+ */
46
69
thumbTintColor ?: ?ColorValue ,
47
70
| } > ;
48
71
72
+ type NativeSwitchType = Class <
73
+ ReactNative . NativeComponent <
74
+ $ReadOnly < { |
75
+ ...Props ,
76
+ enabled ?: ?boolean ,
77
+ on ?: ?boolean ,
78
+ trackTintColor ?: ?ColorValue ,
79
+ | } > ,
80
+ > ,
81
+ > ;
82
+
83
+ const RCTSwitch : NativeSwitchType =
84
+ Platform . OS === 'android'
85
+ ? ( requireNativeComponent ( 'AndroidSwitch' ) : any )
86
+ : ( requireNativeComponent ( 'RCTSwitch' ) : any ) ;
87
+
49
88
/**
50
89
* Renders a boolean input.
51
90
*
@@ -57,105 +96,59 @@ type Props = $ReadOnly<{|
57
96
* @keyword checkbox
58
97
* @keyword toggle
59
98
*/
60
- const Switch = createReactClass ( {
61
- displayName : 'Switch' ,
62
- propTypes : {
63
- ...ViewPropTypes ,
64
- /**
65
- * The value of the switch. If true the switch will be turned on.
66
- * Default value is false.
67
- */
68
- value : PropTypes . bool ,
69
- /**
70
- * If true the user won't be able to toggle the switch.
71
- * Default value is false.
72
- */
73
- disabled : PropTypes . bool ,
74
- /**
75
- * Invoked with the new value when the value changes.
76
- */
77
- onValueChange : PropTypes . func ,
78
- /**
79
- * Used to locate this view in end-to-end tests.
80
- */
81
- testID : PropTypes . string ,
82
-
83
- /**
84
- * Border color on iOS and background color on Android when the switch is turned off.
85
- */
86
- tintColor : ColorPropType ,
87
- /**
88
- * Background color when the switch is turned on.
89
- */
90
- onTintColor : ColorPropType ,
91
- /**
92
- * Color of the foreground switch grip.
93
- */
94
- thumbTintColor : ColorPropType ,
95
- } ,
96
-
97
- getDefaultProps : function ( ) : DefaultProps {
98
- return {
99
- value : false ,
100
- disabled : false ,
101
- } ;
102
- } ,
99
+ class Switch extends React . Component < Props > {
100
+ static defaultProps = {
101
+ value : false ,
102
+ disabled : false ,
103
+ } ;
103
104
104
- mixins : [ NativeMethodsMixin ] ,
105
+ _rctSwitch : ? React . ElementRef < NativeSwitchType > = null ;
105
106
106
- _rctSwitch : { } ,
107
- _onChange : function ( event : Object ) {
107
+ _onChange = ( event : Object ) => {
108
108
if ( Platform . OS === 'android' ) {
109
- this . _rctSwitch . setNativeProps ( { on : this . props . value } ) ;
109
+ nullthrows ( this . _rctSwitch ) . setNativeProps ( { on : this . props . value } ) ;
110
110
} else {
111
- this . _rctSwitch . setNativeProps ( { value : this . props . value } ) ;
111
+ nullthrows ( this . _rctSwitch ) . setNativeProps ( { value : this . props . value } ) ;
112
112
}
113
- //Change the props after the native props are set in case the props change removes the component
114
- /* $FlowFixMe(>=0.53.0 site=react_native_fb,react_native_oss) This comment
115
- * suppresses an error when upgrading Flow's support for React. To see the
116
- * error delete this comment and run Flow. */
113
+
117
114
this . props . onChange && this . props . onChange ( event ) ;
118
115
this . props . onValueChange &&
119
116
this . props . onValueChange ( event . nativeEvent . value ) ;
120
- } ,
117
+ } ;
118
+
119
+ render ( ) {
120
+ const props = {
121
+ ...this . props ,
122
+ onStartShouldSetResponder : ( ) => true ,
123
+ onResponderTerminationRequest : ( ) => false ,
124
+ } ;
125
+
126
+ const platformProps =
127
+ Platform . OS === 'android'
128
+ ? {
129
+ enabled : ! this . props . disabled ,
130
+ on : this . props . value ,
131
+ style : this . props . style ,
132
+ trackTintColor : this . props . value
133
+ ? this . props . onTintColor
134
+ : this . props . tintColor ,
135
+ }
136
+ : {
137
+ style : StyleSheet . compose ( styles . rctSwitchIOS , this . props . style ) ,
138
+ } ;
121
139
122
- render : function ( ) {
123
- const props = { ...this . props } ;
124
- props . onStartShouldSetResponder = ( ) => true ;
125
- props . onResponderTerminationRequest = ( ) => false ;
126
- if ( Platform . OS === 'android' ) {
127
- /* $FlowFixMe(>=0.70.0 site=react_native_fb) This comment suppresses an
128
- * error found when Flow v0.70 was deployed. To see the error delete
129
- * this comment and run Flow. */
130
- props . enabled = ! this . props . disabled ;
131
- /* $FlowFixMe(>=0.70.0 site=react_native_fb) This comment suppresses an
132
- * error found when Flow v0.70 was deployed. To see the error delete
133
- * this comment and run Flow. */
134
- props . on = this . props . value ;
135
- props . style = this . props . style ;
136
- /* $FlowFixMe(>=0.70.0 site=react_native_fb) This comment suppresses an
137
- * error found when Flow v0.70 was deployed. To see the error delete
138
- * this comment and run Flow. */
139
- props . trackTintColor = this . props . value
140
- ? this . props . onTintColor
141
- : this . props . tintColor ;
142
- } else if ( Platform . OS === 'ios' ) {
143
- props . style = [ styles . rctSwitchIOS , this . props . style ] ;
144
- }
145
140
return (
146
141
< RCTSwitch
147
142
{ ...props }
143
+ { ...platformProps }
148
144
ref = { ref => {
149
- /* $FlowFixMe(>=0.53.0 site=react_native_fb,react_native_oss) This
150
- * comment suppresses an error when upgrading Flow's support for React.
151
- * To see the error delete this comment and run Flow. */
152
145
this . _rctSwitch = ref ;
153
146
} }
154
147
onChange = { this . _onChange }
155
148
/>
156
149
) ;
157
- } ,
158
- } ) ;
150
+ }
151
+ }
159
152
160
153
const styles = StyleSheet . create ( {
161
154
rctSwitchIOS : {
@@ -164,4 +157,4 @@ const styles = StyleSheet.create({
164
157
} ,
165
158
} ) ;
166
159
167
- module . exports = ( ( Switch : any ) : Class < ReactNative . NativeComponent < Props >> ) ;
160
+ module . exports = Switch ;
0 commit comments