Skip to content

Commit 970caa4

Browse files
elicwhitefacebook-github-bot
authored andcommittedJun 3, 2018
Switch to ES6 Class
Reviewed By: yungsters Differential Revision: D8246980 fbshipit-source-id: fbd6998429e6791000ea093d3fa15c1a486914bd
1 parent 5259450 commit 970caa4

File tree

1 file changed

+90
-97
lines changed

1 file changed

+90
-97
lines changed
 

‎Libraries/Components/Switch/Switch.js

+90-97
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,81 @@
1010

1111
'use strict';
1212

13-
const ColorPropType = require('ColorPropType');
14-
const NativeMethodsMixin = require('NativeMethodsMixin');
1513
const Platform = require('Platform');
1614
const React = require('React');
1715
const ReactNative = require('ReactNative');
18-
const PropTypes = require('prop-types');
1916
const StyleSheet = require('StyleSheet');
20-
const ViewPropTypes = require('ViewPropTypes');
2117

22-
const createReactClass = require('create-react-class');
18+
const nullthrows = require('fbjs/lib/nullthrows');
2319
const requireNativeComponent = require('requireNativeComponent');
2420

2521
import type {ColorValue} from 'StyleSheetTypes';
2622
import type {ViewProps} from 'ViewPropTypes';
2723

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-
3824
type Props = $ReadOnly<{|
3925
...ViewProps,
26+
/**
27+
* The value of the switch. If true the switch will be turned on.
28+
* Default value is false.
29+
*/
4030
value?: ?boolean,
31+
32+
/**
33+
* If true the user won't be able to toggle the switch.
34+
* Default value is false.
35+
*/
4136
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+
*/
4249
onValueChange?: ?Function,
50+
51+
/**
52+
* Used to locate this view in end-to-end tests.
53+
*/
4354
testID?: ?string,
55+
56+
/**
57+
* Border color on iOS and background color on Android when the switch is turned off.
58+
*/
4459
tintColor?: ?ColorValue,
60+
61+
/**
62+
* Background color when the switch is turned on.
63+
*/
4564
onTintColor?: ?ColorValue,
65+
66+
/**
67+
* Color of the foreground switch grip.
68+
*/
4669
thumbTintColor?: ?ColorValue,
4770
|}>;
4871

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+
4988
/**
5089
* Renders a boolean input.
5190
*
@@ -57,105 +96,59 @@ type Props = $ReadOnly<{|
5796
* @keyword checkbox
5897
* @keyword toggle
5998
*/
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+
};
103104

104-
mixins: [NativeMethodsMixin],
105+
_rctSwitch: ?React.ElementRef<NativeSwitchType> = null;
105106

106-
_rctSwitch: {},
107-
_onChange: function(event: Object) {
107+
_onChange = (event: Object) => {
108108
if (Platform.OS === 'android') {
109-
this._rctSwitch.setNativeProps({on: this.props.value});
109+
nullthrows(this._rctSwitch).setNativeProps({on: this.props.value});
110110
} else {
111-
this._rctSwitch.setNativeProps({value: this.props.value});
111+
nullthrows(this._rctSwitch).setNativeProps({value: this.props.value});
112112
}
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+
117114
this.props.onChange && this.props.onChange(event);
118115
this.props.onValueChange &&
119116
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+
};
121139

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-
}
145140
return (
146141
<RCTSwitch
147142
{...props}
143+
{...platformProps}
148144
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. */
152145
this._rctSwitch = ref;
153146
}}
154147
onChange={this._onChange}
155148
/>
156149
);
157-
},
158-
});
150+
}
151+
}
159152

160153
const styles = StyleSheet.create({
161154
rctSwitchIOS: {
@@ -164,4 +157,4 @@ const styles = StyleSheet.create({
164157
},
165158
});
166159

167-
module.exports = ((Switch: any): Class<ReactNative.NativeComponent<Props>>);
160+
module.exports = Switch;

0 commit comments

Comments
 (0)
Please sign in to comment.