2015-10-06 16:24:19 +00:00
|
|
|
/**
|
2016-03-25 22:16:43 +00:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-10-06 16:24:19 +00:00
|
|
|
*
|
|
|
|
* @providesModule Switch
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-12-23 03:29:01 +00:00
|
|
|
var ColorPropType = require('ColorPropType');
|
2016-11-04 12:40:26 +00:00
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
2015-11-18 13:32:46 +00:00
|
|
|
var Platform = require('Platform');
|
2015-10-06 16:24:19 +00:00
|
|
|
var React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
const PropTypes = require('prop-types');
|
2015-10-06 16:24:19 +00:00
|
|
|
var StyleSheet = require('StyleSheet');
|
2017-03-24 07:22:57 +00:00
|
|
|
const ViewPropTypes = require('ViewPropTypes');
|
2015-10-06 16:24:19 +00:00
|
|
|
|
2017-07-07 21:24:25 +00:00
|
|
|
var createReactClass = require('create-react-class');
|
2015-10-06 16:24:19 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
|
|
|
|
|
|
|
type DefaultProps = {
|
2016-08-09 13:32:41 +00:00
|
|
|
value: boolean,
|
|
|
|
disabled: boolean,
|
2015-10-06 16:24:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-03-25 22:16:43 +00:00
|
|
|
* Renders a boolean input.
|
|
|
|
*
|
|
|
|
* This is a controlled component that requires an `onValueChange` callback that
|
|
|
|
* updates the `value` prop in order for the component to reflect user actions.
|
|
|
|
* If the `value` prop is not updated, the component will continue to render
|
|
|
|
* the supplied `value` prop instead of the expected result of any user actions.
|
|
|
|
*
|
|
|
|
* @keyword checkbox
|
|
|
|
* @keyword toggle
|
2015-10-06 16:24:19 +00:00
|
|
|
*/
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2017-07-07 21:24:25 +00:00
|
|
|
var Switch = createReactClass({
|
|
|
|
displayName: 'Switch',
|
2015-10-06 16:24:19 +00:00
|
|
|
propTypes: {
|
2017-03-24 07:22:57 +00:00
|
|
|
...ViewPropTypes,
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
|
|
|
* The value of the switch. If true the switch will be turned on.
|
|
|
|
* Default value is false.
|
|
|
|
*/
|
2017-01-31 21:33:38 +00:00
|
|
|
value: PropTypes.bool,
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
|
|
|
* If true the user won't be able to toggle the switch.
|
|
|
|
* Default value is false.
|
|
|
|
*/
|
2017-01-31 21:33:38 +00:00
|
|
|
disabled: PropTypes.bool,
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
2015-12-15 17:08:39 +00:00
|
|
|
* Invoked with the new value when the value changes.
|
2015-10-06 16:24:19 +00:00
|
|
|
*/
|
2017-01-31 21:33:38 +00:00
|
|
|
onValueChange: PropTypes.func,
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
|
|
|
* Used to locate this view in end-to-end tests.
|
|
|
|
*/
|
2017-01-31 21:33:38 +00:00
|
|
|
testID: PropTypes.string,
|
2016-02-15 23:13:42 +00:00
|
|
|
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
2017-01-31 21:33:38 +00:00
|
|
|
* Border color on iOS and background color on Android when the switch is turned off.
|
2015-10-06 16:24:19 +00:00
|
|
|
*/
|
2015-12-23 03:29:01 +00:00
|
|
|
tintColor: ColorPropType,
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
|
|
|
* Background color when the switch is turned on.
|
|
|
|
*/
|
2015-12-23 03:29:01 +00:00
|
|
|
onTintColor: ColorPropType,
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
|
|
|
* Color of the foreground switch grip.
|
|
|
|
*/
|
2015-12-23 03:29:01 +00:00
|
|
|
thumbTintColor: ColorPropType,
|
2015-10-06 16:24:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function(): DefaultProps {
|
|
|
|
return {
|
|
|
|
value: false,
|
|
|
|
disabled: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
|
|
|
|
_rctSwitch: {},
|
|
|
|
_onChange: function(event: Object) {
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
this._rctSwitch.setNativeProps({on: this.props.value});
|
|
|
|
} else {
|
|
|
|
this._rctSwitch.setNativeProps({value: this.props.value});
|
|
|
|
}
|
2016-04-18 04:00:00 +00:00
|
|
|
//Change the props after the native props are set in case the props change removes the component
|
|
|
|
this.props.onChange && this.props.onChange(event);
|
|
|
|
this.props.onValueChange && this.props.onValueChange(event.nativeEvent.value);
|
2015-10-06 16:24:19 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var props = {...this.props};
|
|
|
|
props.onStartShouldSetResponder = () => true;
|
|
|
|
props.onResponderTerminationRequest = () => false;
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
props.enabled = !this.props.disabled;
|
|
|
|
props.on = this.props.value;
|
2016-01-06 15:41:13 +00:00
|
|
|
props.style = this.props.style;
|
2017-01-31 21:33:38 +00:00
|
|
|
props.trackTintColor = this.props.value ? this.props.onTintColor : this.props.tintColor;
|
2015-10-06 16:24:19 +00:00
|
|
|
} else if (Platform.OS === 'ios') {
|
|
|
|
props.style = [styles.rctSwitchIOS, this.props.style];
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<RCTSwitch
|
|
|
|
{...props}
|
|
|
|
ref={(ref) => { this._rctSwitch = ref; }}
|
|
|
|
onChange={this._onChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
rctSwitchIOS: {
|
|
|
|
height: 31,
|
|
|
|
width: 51,
|
2016-01-06 15:41:13 +00:00
|
|
|
}
|
2015-10-06 16:24:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
2015-11-18 13:32:46 +00:00
|
|
|
var RCTSwitch = requireNativeComponent('AndroidSwitch', Switch, {
|
2017-01-31 21:33:38 +00:00
|
|
|
nativeOnly: {
|
|
|
|
onChange: true,
|
|
|
|
on: true,
|
|
|
|
enabled: true,
|
|
|
|
trackTintColor: true,
|
|
|
|
}
|
2015-10-06 16:24:19 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var RCTSwitch = requireNativeComponent('RCTSwitch', Switch, {
|
2017-01-31 21:33:38 +00:00
|
|
|
nativeOnly: {
|
|
|
|
onChange: true
|
|
|
|
}
|
2015-10-06 16:24:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Switch;
|