2017-08-25 10:22:17 -07:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-08-25 10:22:17 -07:00
|
|
|
*
|
2018-02-16 18:24:55 -08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-08-25 10:22:17 -07:00
|
|
|
*
|
2018-08-08 10:39:16 -07:00
|
|
|
* @flow
|
2017-08-25 10:22:17 -07:00
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const React = require('React');
|
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
|
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
2018-11-02 10:29:47 -07:00
|
|
|
const nullthrows = require('nullthrows');
|
|
|
|
const setAndForwardRef = require('setAndForwardRef');
|
2017-08-25 10:22:17 -07:00
|
|
|
|
2018-11-02 10:29:47 -07:00
|
|
|
import type {ViewProps} from 'ViewPropTypes';
|
|
|
|
import type {SyntheticEvent} from 'CoreEventTypes';
|
|
|
|
import type {NativeComponent} from 'ReactNative';
|
2018-06-10 15:34:37 -07:00
|
|
|
|
2018-11-02 10:29:47 -07:00
|
|
|
type CheckBoxEvent = SyntheticEvent<
|
|
|
|
$ReadOnly<{|
|
|
|
|
target: number,
|
|
|
|
value: boolean,
|
|
|
|
|}>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
type CommonProps = $ReadOnly<{|
|
|
|
|
...ViewProps,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used in case the props change removes the component.
|
|
|
|
*/
|
|
|
|
onChange?: ?(event: CheckBoxEvent) => mixed,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked with the new value when the value changes.
|
|
|
|
*/
|
|
|
|
onValueChange?: ?(value: boolean) => mixed,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to locate this view in end-to-end tests.
|
|
|
|
*/
|
|
|
|
testID?: ?string,
|
|
|
|
|}>;
|
|
|
|
|
|
|
|
type NativeProps = $ReadOnly<{|
|
|
|
|
...CommonProps,
|
|
|
|
|
|
|
|
on?: ?boolean,
|
|
|
|
enabled?: boolean,
|
|
|
|
|}>;
|
|
|
|
|
|
|
|
type CheckBoxNativeType = Class<NativeComponent<NativeProps>>;
|
|
|
|
|
|
|
|
type Props = $ReadOnly<{|
|
|
|
|
...CommonProps,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The value of the checkbox. If true the checkbox will be turned on.
|
|
|
|
* Default value is false.
|
|
|
|
*/
|
|
|
|
value?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true the user won't be able to toggle the checkbox.
|
|
|
|
* Default value is false.
|
|
|
|
*/
|
|
|
|
disabled?: ?boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to get the ref for the native checkbox
|
|
|
|
*/
|
|
|
|
forwardedRef?: ?React.Ref<CheckBoxNativeType>,
|
|
|
|
|}>;
|
|
|
|
|
|
|
|
const RCTCheckBox = ((requireNativeComponent(
|
|
|
|
'AndroidCheckBox',
|
|
|
|
): any): CheckBoxNativeType);
|
2017-08-25 10:22:17 -07:00
|
|
|
|
|
|
|
/**
|
2017-10-07 17:17:09 -07:00
|
|
|
* Renders a boolean input (Android only).
|
2017-08-25 10:22:17 -07: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.
|
|
|
|
* 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.
|
|
|
|
*
|
2017-11-07 10:57:39 -08:00
|
|
|
* ```
|
|
|
|
* import React from 'react';
|
|
|
|
* import { AppRegistry, StyleSheet, Text, View, CheckBox } from 'react-native';
|
|
|
|
*
|
|
|
|
* export default class App extends React.Component {
|
|
|
|
* constructor(props) {
|
|
|
|
* super(props);
|
|
|
|
* this.state = {
|
|
|
|
* checked: false
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* toggle() {
|
|
|
|
* this.setState(({checked}) => {
|
|
|
|
* return {
|
|
|
|
* checked: !checked
|
|
|
|
* };
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* render() {
|
|
|
|
* const {checked} = this.state;
|
|
|
|
* return (
|
|
|
|
* <View style={styles.container}>
|
|
|
|
* <Text>Checked</Text>
|
|
|
|
* <CheckBox value={checked} onChange={this.toggle.bind(this)} />
|
|
|
|
* </View>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* const styles = StyleSheet.create({
|
|
|
|
* container: {
|
|
|
|
* flex: 1,
|
|
|
|
* flexDirection: 'row',
|
|
|
|
* alignItems: 'center',
|
|
|
|
* justifyContent: 'center',
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // skip this line if using Create React Native App
|
|
|
|
* AppRegistry.registerComponent('App', () => App);
|
|
|
|
* ```
|
|
|
|
*
|
2017-08-25 10:22:17 -07:00
|
|
|
* @keyword checkbox
|
|
|
|
* @keyword toggle
|
|
|
|
*/
|
2018-11-02 10:29:47 -07:00
|
|
|
class CheckBox extends React.Component<Props> {
|
|
|
|
_nativeRef: ?React.ElementRef<CheckBoxNativeType> = null;
|
|
|
|
_setNativeRef = setAndForwardRef({
|
|
|
|
getForwardedRef: () => this.props.forwardedRef,
|
|
|
|
setLocalRef: ref => {
|
|
|
|
this._nativeRef = ref;
|
|
|
|
},
|
|
|
|
});
|
2017-08-25 10:22:17 -07:00
|
|
|
|
2018-11-02 10:29:47 -07:00
|
|
|
_onChange = (event: CheckBoxEvent) => {
|
|
|
|
const value = this.props.value ?? false;
|
|
|
|
nullthrows(this._nativeRef).setNativeProps({value: value});
|
2017-08-25 10:22:17 -07: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);
|
2018-11-02 10:29:47 -07:00
|
|
|
};
|
2017-08-25 10:22:17 -07:00
|
|
|
|
2018-11-02 10:29:47 -07:00
|
|
|
render() {
|
|
|
|
const {disabled: _, value: __, style, forwardedRef, ...props} = this.props;
|
|
|
|
const disabled = this.props.disabled ?? false;
|
|
|
|
const value = this.props.value ?? false;
|
|
|
|
|
|
|
|
const nativeProps = {
|
|
|
|
...props,
|
|
|
|
onStartShouldSetResponder: () => true,
|
|
|
|
onResponderTerminationRequest: () => false,
|
|
|
|
enabled: !disabled,
|
|
|
|
on: value,
|
|
|
|
style: [styles.rctCheckBox, style],
|
|
|
|
};
|
2017-08-25 10:22:17 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RCTCheckBox
|
2018-11-02 10:29:47 -07:00
|
|
|
{...nativeProps}
|
|
|
|
ref={this._setNativeRef}
|
2017-08-25 10:22:17 -07:00
|
|
|
onChange={this._onChange}
|
|
|
|
/>
|
|
|
|
);
|
2018-11-02 10:29:47 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-25 10:22:17 -07:00
|
|
|
|
2018-11-02 10:29:47 -07:00
|
|
|
const styles = StyleSheet.create({
|
2017-08-25 10:22:17 -07:00
|
|
|
rctCheckBox: {
|
|
|
|
height: 32,
|
|
|
|
width: 32,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-11-02 10:29:47 -07:00
|
|
|
/**
|
|
|
|
* Can't use CheckBoxNativeType because it has different props
|
|
|
|
*/
|
|
|
|
type CheckBoxType = Class<NativeComponent<Props>>;
|
|
|
|
|
|
|
|
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
|
|
|
|
const CheckBoxWithRef = React.forwardRef(function CheckBoxWithRef(props, ref) {
|
|
|
|
return <CheckBox {...props} forwardedRef={ref} />;
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = (CheckBoxWithRef: CheckBoxType);
|