2015-10-06 16:24:19 +00:00
|
|
|
/**
|
2016-03-25 22:16:43 +00:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-10-06 16:24:19 +00:00
|
|
|
*
|
|
|
|
* @flow
|
2018-08-01 03:54:02 +00:00
|
|
|
* @format
|
2015-10-06 16:24:19 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-10-06 16:24:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-08-08 21:50:58 +00:00
|
|
|
const SwitchNativeComponent = require('SwitchNativeComponent');
|
2018-03-03 23:04:46 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
const React = require('React');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
2015-10-06 16:24:19 +00:00
|
|
|
|
2018-08-01 03:54:02 +00:00
|
|
|
import type {SwitchChangeEvent} from 'CoreEventTypes';
|
2018-05-14 07:09:18 +00:00
|
|
|
import type {ColorValue} from 'StyleSheetTypes';
|
|
|
|
import type {ViewProps} from 'ViewPropTypes';
|
2018-08-08 21:50:58 +00:00
|
|
|
import type {NativeAndroidProps, NativeIOSProps} from 'SwitchNativeComponent';
|
2018-05-14 07:09:18 +00:00
|
|
|
|
2018-07-27 13:34:06 +00:00
|
|
|
export type Props = $ReadOnly<{|
|
2018-05-14 07:09:18 +00:00
|
|
|
...ViewProps,
|
2018-08-01 03:54:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the switch is disabled. Defaults to false.
|
|
|
|
*/
|
|
|
|
disabled?: ?boolean,
|
|
|
|
|
2018-06-03 05:35:54 +00:00
|
|
|
/**
|
2018-08-01 03:54:02 +00:00
|
|
|
* Boolean value of the switch. Defaults to false.
|
2018-06-03 05:35:54 +00:00
|
|
|
*/
|
2018-05-14 07:09:18 +00:00
|
|
|
value?: ?boolean,
|
2018-06-03 05:35:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-01 03:54:02 +00:00
|
|
|
* Custom color for the switch thumb.
|
2018-06-03 05:35:54 +00:00
|
|
|
*/
|
2018-08-01 03:54:02 +00:00
|
|
|
thumbColor?: ?ColorValue,
|
2018-06-03 05:35:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-01 03:54:02 +00:00
|
|
|
* Custom colors for the switch track.
|
2018-06-03 05:35:54 +00:00
|
|
|
*
|
2018-08-01 03:54:02 +00:00
|
|
|
* NOTE: On iOS when the switch value is false, the track shrinks into the
|
|
|
|
* border. If you want to change the color of the background exposed by the
|
|
|
|
* shrunken track, use `ios_backgroundColor`.
|
2018-06-03 05:35:54 +00:00
|
|
|
*/
|
2018-08-01 03:54:02 +00:00
|
|
|
trackColor?: ?$ReadOnly<{|
|
|
|
|
false?: ?ColorValue,
|
|
|
|
true?: ?ColorValue,
|
|
|
|
|}>,
|
2018-06-03 05:35:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-01 03:54:02 +00:00
|
|
|
* On iOS, custom color for the background. This background color can be seen
|
|
|
|
* either when the switch value is false or when the switch is disabled (and
|
|
|
|
* the switch is translucent).
|
2018-06-03 05:35:54 +00:00
|
|
|
*/
|
2018-08-01 03:54:02 +00:00
|
|
|
ios_backgroundColor?: ?ColorValue,
|
2018-06-03 05:35:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-01 03:54:02 +00:00
|
|
|
* Called when the user tries to change the value of the switch.
|
|
|
|
*
|
|
|
|
* Receives the change event as an argument. If you want to only receive the
|
|
|
|
* new value, use `onValueChange` instead.
|
2018-06-03 05:35:54 +00:00
|
|
|
*/
|
2018-08-01 03:54:02 +00:00
|
|
|
onChange?: ?(event: SwitchChangeEvent) => Promise<void> | void,
|
2018-06-03 05:35:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-01 03:54:02 +00:00
|
|
|
* Called when the user tries to change the value of the switch.
|
|
|
|
*
|
|
|
|
* Receives the new value as an argument. If you want to instead receive an
|
|
|
|
* event, use `onChange`.
|
2018-06-03 05:35:54 +00:00
|
|
|
*/
|
2018-08-01 03:54:02 +00:00
|
|
|
onValueChange?: ?(value: boolean) => Promise<void> | void,
|
|
|
|
|}>;
|
|
|
|
|
2015-10-06 16:24:19 +00:00
|
|
|
/**
|
2018-08-01 03:54:02 +00:00
|
|
|
* A visual toggle between two mutually exclusive states.
|
2016-03-25 22:16:43 +00:00
|
|
|
*
|
|
|
|
* This is a controlled component that requires an `onValueChange` callback that
|
|
|
|
* updates the `value` prop in order for the component to reflect user actions.
|
2018-08-01 03:54:02 +00:00
|
|
|
* 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.
|
2015-10-06 16:24:19 +00:00
|
|
|
*/
|
2018-06-03 05:35:54 +00:00
|
|
|
class Switch extends React.Component<Props> {
|
2018-08-08 21:50:58 +00:00
|
|
|
_nativeSwitchRef: ?React.ElementRef<typeof SwitchNativeComponent>;
|
2018-06-03 05:35:54 +00:00
|
|
|
|
|
|
|
render() {
|
2018-08-01 03:54:02 +00:00
|
|
|
const {
|
|
|
|
disabled,
|
|
|
|
ios_backgroundColor,
|
|
|
|
onChange,
|
|
|
|
onValueChange,
|
|
|
|
style,
|
|
|
|
thumbColor,
|
|
|
|
trackColor,
|
|
|
|
value,
|
|
|
|
...props
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
// Support deprecated color props.
|
|
|
|
let _thumbColor = thumbColor;
|
|
|
|
let _trackColorForFalse = trackColor?.false;
|
|
|
|
let _trackColorForTrue = trackColor?.true;
|
|
|
|
|
2018-08-01 03:54:09 +00:00
|
|
|
// TODO: Remove support for these props after a couple releases.
|
|
|
|
const {thumbTintColor, tintColor, onTintColor} = (props: $FlowFixMe);
|
2018-08-01 03:54:02 +00:00
|
|
|
if (thumbTintColor != null) {
|
|
|
|
_thumbColor = thumbTintColor;
|
2018-08-01 03:54:09 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
console.warn(
|
|
|
|
'Switch: `thumbTintColor` is deprecated, use `_thumbColor` instead.',
|
|
|
|
);
|
|
|
|
}
|
2018-08-01 03:54:02 +00:00
|
|
|
}
|
|
|
|
if (tintColor != null) {
|
|
|
|
_trackColorForFalse = tintColor;
|
2018-08-01 03:54:09 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
console.warn(
|
|
|
|
'Switch: `tintColor` is deprecated, use `trackColor` instead.',
|
|
|
|
);
|
|
|
|
}
|
2018-08-01 03:54:02 +00:00
|
|
|
}
|
|
|
|
if (onTintColor != null) {
|
|
|
|
_trackColorForTrue = onTintColor;
|
2018-08-01 03:54:09 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
console.warn(
|
|
|
|
'Switch: `onTintColor` is deprecated, use `trackColor` instead.',
|
|
|
|
);
|
|
|
|
}
|
2018-08-01 03:54:02 +00:00
|
|
|
}
|
2018-06-03 05:35:54 +00:00
|
|
|
|
|
|
|
const platformProps =
|
|
|
|
Platform.OS === 'android'
|
2018-08-01 03:54:02 +00:00
|
|
|
? ({
|
|
|
|
enabled: disabled !== true,
|
|
|
|
on: value === true,
|
|
|
|
style,
|
|
|
|
thumbTintColor: _thumbColor,
|
|
|
|
trackTintColor:
|
|
|
|
value === true ? _trackColorForTrue : _trackColorForFalse,
|
|
|
|
}: NativeAndroidProps)
|
|
|
|
: ({
|
|
|
|
disabled,
|
|
|
|
onTintColor: _trackColorForTrue,
|
2018-06-06 12:20:40 +00:00
|
|
|
style: StyleSheet.compose(
|
2018-08-01 03:54:02 +00:00
|
|
|
{height: 31, width: 51},
|
|
|
|
StyleSheet.compose(
|
|
|
|
style,
|
|
|
|
ios_backgroundColor == null
|
|
|
|
? null
|
|
|
|
: {
|
|
|
|
backgroundColor: ios_backgroundColor,
|
|
|
|
borderRadius: 16,
|
|
|
|
},
|
|
|
|
),
|
2018-06-06 12:20:40 +00:00
|
|
|
),
|
2018-08-01 03:54:02 +00:00
|
|
|
thumbTintColor: _thumbColor,
|
|
|
|
tintColor: _trackColorForFalse,
|
|
|
|
value: value === true,
|
|
|
|
}: NativeIOSProps);
|
2015-10-06 16:24:19 +00:00
|
|
|
|
|
|
|
return (
|
2018-08-08 21:50:58 +00:00
|
|
|
<SwitchNativeComponent
|
2015-10-06 16:24:19 +00:00
|
|
|
{...props}
|
2018-06-03 05:35:54 +00:00
|
|
|
{...platformProps}
|
2018-08-01 03:54:02 +00:00
|
|
|
onChange={this._handleChange}
|
|
|
|
onResponderTerminationRequest={returnsFalse}
|
|
|
|
onStartShouldSetResponder={returnsTrue}
|
2018-08-08 21:50:58 +00:00
|
|
|
ref={this._handleSwitchNativeComponentRef}
|
2015-10-06 16:24:19 +00:00
|
|
|
/>
|
|
|
|
);
|
2018-06-03 05:35:54 +00:00
|
|
|
}
|
2018-08-01 03:54:02 +00:00
|
|
|
|
|
|
|
_handleChange = (event: SwitchChangeEvent) => {
|
|
|
|
if (this._nativeSwitchRef == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force value of native switch in order to control it.
|
|
|
|
const value = this.props.value === true;
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
this._nativeSwitchRef.setNativeProps({on: value});
|
|
|
|
} else {
|
|
|
|
this._nativeSwitchRef.setNativeProps({value});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.onChange != null) {
|
|
|
|
this.props.onChange(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.onValueChange != null) {
|
|
|
|
this.props.onValueChange(event.nativeEvent.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-08 21:50:58 +00:00
|
|
|
_handleSwitchNativeComponentRef = (
|
|
|
|
ref: ?React.ElementRef<typeof SwitchNativeComponent>,
|
|
|
|
) => {
|
2018-08-01 03:54:02 +00:00
|
|
|
this._nativeSwitchRef = ref;
|
|
|
|
};
|
2018-06-03 05:35:54 +00:00
|
|
|
}
|
2015-10-06 16:24:19 +00:00
|
|
|
|
2018-08-01 03:54:02 +00:00
|
|
|
const returnsFalse = () => false;
|
|
|
|
const returnsTrue = () => true;
|
2015-10-06 16:24:19 +00:00
|
|
|
|
2018-06-03 05:35:54 +00:00
|
|
|
module.exports = Switch;
|