Skip to content

Commit 36cbe74

Browse files
sahrensfacebook-github-bot-4
authored andcommitted
introduce universal switch
Reviewed By: @vjeux, @mkonicek Differential Revision: D2441456 fb-gh-sync-id: c4050ec86f30234ea29c38205d668157bde65a35
1 parent 8f77487 commit 36cbe74

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*
4+
* @providesModule Switch
5+
* @flow
6+
*/
7+
'use strict';
8+
9+
var Platform = require('Platform');
10+
var NativeMethodsMixin = require('NativeMethodsMixin');
11+
var React = require('React');
12+
var StyleSheet = require('StyleSheet');
13+
14+
var requireNativeComponent = require('requireNativeComponent');
15+
16+
type DefaultProps = {
17+
value: boolean;
18+
disabled: boolean;
19+
};
20+
21+
/**
22+
* Universal two-state toggle component.
23+
*/
24+
var Switch = React.createClass({
25+
propTypes: {
26+
/**
27+
* The value of the switch. If true the switch will be turned on.
28+
* Default value is false.
29+
*/
30+
value: React.PropTypes.bool,
31+
/**
32+
* If true the user won't be able to toggle the switch.
33+
* Default value is false.
34+
*/
35+
disabled: React.PropTypes.bool,
36+
/**
37+
* Invoked with the new value when the value chages.
38+
*/
39+
onValueChange: React.PropTypes.func,
40+
/**
41+
* Used to locate this view in end-to-end tests.
42+
*/
43+
testID: React.PropTypes.string,
44+
45+
/**
46+
* Background color when the switch is turned off.
47+
* @platform ios
48+
*/
49+
tintColor: React.PropTypes.string,
50+
/**
51+
* Background color when the switch is turned on.
52+
* @platform ios
53+
*/
54+
onTintColor: React.PropTypes.string,
55+
/**
56+
* Color of the foreground switch grip.
57+
* @platform ios
58+
*/
59+
thumbTintColor: React.PropTypes.string,
60+
},
61+
62+
getDefaultProps: function(): DefaultProps {
63+
return {
64+
value: false,
65+
disabled: false,
66+
};
67+
},
68+
69+
mixins: [NativeMethodsMixin],
70+
71+
_rctSwitch: {},
72+
_onChange: function(event: Object) {
73+
this.props.onChange && this.props.onChange(event);
74+
this.props.onValueChange && this.props.onValueChange(event.nativeEvent.value);
75+
76+
// The underlying switch might have changed, but we're controlled,
77+
// and so want to ensure it represents our value.
78+
if (Platform.OS === 'android') {
79+
this._rctSwitch.setNativeProps({on: this.props.value});
80+
} else {
81+
this._rctSwitch.setNativeProps({value: this.props.value});
82+
}
83+
},
84+
85+
render: function() {
86+
var props = {...this.props};
87+
props.onStartShouldSetResponder = () => true;
88+
props.onResponderTerminationRequest = () => false;
89+
if (Platform.OS === 'android') {
90+
props.enabled = !this.props.disabled;
91+
props.on = this.props.value;
92+
props.style = [styles.rctSwitchAndroid, this.props.style];
93+
} else if (Platform.OS === 'ios') {
94+
props.style = [styles.rctSwitchIOS, this.props.style];
95+
}
96+
return (
97+
<RCTSwitch
98+
{...props}
99+
ref={(ref) => { this._rctSwitch = ref; }}
100+
onChange={this._onChange}
101+
/>
102+
);
103+
},
104+
});
105+
106+
var styles = StyleSheet.create({
107+
rctSwitchIOS: {
108+
height: 31,
109+
width: 51,
110+
},
111+
rctSwitchAndroid: {
112+
height: 27,
113+
width: 40,
114+
},
115+
});
116+
117+
if (Platform.OS === 'android') {
118+
var RCTSwitch = requireNativeComponent('AndroidSwitch', null, {
119+
nativeOnly: { onChange: true, on: true, enabled: true }
120+
});
121+
} else {
122+
var RCTSwitch = requireNativeComponent('RCTSwitch', Switch, {
123+
nativeOnly: { onChange: true }
124+
});
125+
}
126+
127+
module.exports = Switch;

Libraries/react-native/react-native.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var ReactNative = Object.assign(Object.create(require('React')), {
3535
SegmentedControlIOS: require('SegmentedControlIOS'),
3636
SliderIOS: require('SliderIOS'),
3737
SnapshotView: require('SnapshotView'),
38+
Switch: require('Switch'),
3839
SwitchAndroid: require('SwitchAndroid'),
3940
SwitchIOS: require('SwitchIOS'),
4041
TabBarIOS: require('TabBarIOS'),

0 commit comments

Comments
 (0)