2015-03-09 20:19:07 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-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-03-09 20:19:07 +00:00
|
|
|
*
|
|
|
|
* @providesModule SwitchIOS
|
2015-03-25 19:55:10 +00:00
|
|
|
* @flow
|
2015-03-09 20:19:07 +00:00
|
|
|
*
|
2015-03-17 10:08:46 +00:00
|
|
|
* This is a controlled component version of RCTSwitch.
|
2015-03-09 20:19:07 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var NativeMethodsMixin = require('NativeMethodsMixin');
|
|
|
|
var PropTypes = require('ReactPropTypes');
|
|
|
|
var React = require('React');
|
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
|
2015-04-22 04:07:17 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
2015-03-09 20:19:07 +00:00
|
|
|
|
|
|
|
var SWITCH = 'switch';
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
type DefaultProps = {
|
|
|
|
value: boolean;
|
|
|
|
disabled: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Event = Object;
|
|
|
|
|
2015-03-09 20:19:07 +00:00
|
|
|
/**
|
|
|
|
* Use `SwitchIOS` to render a boolean input on iOS. This is
|
|
|
|
* a controlled component, so you must hook in to the `onValueChange` callback
|
|
|
|
* and update the `value` prop in order for the component to update, otherwise
|
|
|
|
* the user's change will be reverted immediately to reflect `props.value` as the
|
|
|
|
* source of truth.
|
|
|
|
*/
|
|
|
|
var SwitchIOS = React.createClass({
|
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
/**
|
|
|
|
* The value of the switch, if true the switch will be turned on.
|
|
|
|
* Default value is false.
|
|
|
|
*/
|
|
|
|
value: PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true the user won't be able to toggle the switch.
|
|
|
|
* Default value is false.
|
|
|
|
*/
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback that is called when the user toggles the switch.
|
|
|
|
*/
|
|
|
|
onValueChange: PropTypes.func,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Background color when the switch is turned on.
|
|
|
|
*/
|
|
|
|
onTintColor: PropTypes.string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Background color for the switch round button.
|
|
|
|
*/
|
|
|
|
thumbTintColor: PropTypes.string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Background color when the switch is turned off.
|
|
|
|
*/
|
|
|
|
tintColor: PropTypes.string,
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
getDefaultProps: function(): DefaultProps {
|
2015-03-09 20:19:07 +00:00
|
|
|
return {
|
|
|
|
value: false,
|
|
|
|
disabled: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_onChange: function(event: Event) {
|
2015-03-09 20:19:07 +00:00
|
|
|
// The underlying switch might have changed, but we're controlled,
|
|
|
|
// and so want to ensure it represents our value.
|
2015-04-22 04:07:17 +00:00
|
|
|
this.refs[SWITCH].setNativeProps({value: this.props.value});
|
2015-09-16 22:31:08 +00:00
|
|
|
|
|
|
|
if (this.props.value === event.nativeEvent.value || this.props.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.onChange && this.props.onChange(event);
|
|
|
|
this.props.onValueChange && this.props.onValueChange(event.nativeEvent.value);
|
2015-03-09 20:19:07 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
return (
|
2015-03-17 10:08:46 +00:00
|
|
|
<RCTSwitch
|
2015-04-22 04:07:17 +00:00
|
|
|
{...this.props}
|
2015-03-09 20:19:07 +00:00
|
|
|
ref={SWITCH}
|
|
|
|
onChange={this._onChange}
|
2015-04-22 04:07:17 +00:00
|
|
|
style={[styles.rkSwitch, this.props.style]}
|
2015-03-09 20:19:07 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
rkSwitch: {
|
|
|
|
height: 31,
|
|
|
|
width: 51,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
var RCTSwitch = requireNativeComponent('RCTSwitch', SwitchIOS, {
|
|
|
|
nativeOnly: { onChange: true }
|
|
|
|
});
|
2015-03-09 20:19:07 +00:00
|
|
|
|
|
|
|
module.exports = SwitchIOS;
|