|
9 | 9 | */
|
10 | 10 | 'use strict';
|
11 | 11 |
|
12 |
| -const DeprecatedViewPropTypes = require('DeprecatedViewPropTypes'); |
13 |
| -const NativeMethodsMixin = require('NativeMethodsMixin'); |
14 |
| -const PropTypes = require('prop-types'); |
15 | 12 | const React = require('React');
|
16 | 13 | const StyleSheet = require('StyleSheet');
|
17 | 14 |
|
18 |
| -const createReactClass = require('create-react-class'); |
19 | 15 | const requireNativeComponent = require('requireNativeComponent');
|
| 16 | +const nullthrows = require('nullthrows'); |
| 17 | +const setAndForwardRef = require('setAndForwardRef'); |
20 | 18 |
|
21 |
| -const RCTCheckBox = requireNativeComponent('AndroidCheckBox'); |
| 19 | +import type {ViewProps} from 'ViewPropTypes'; |
| 20 | +import type {SyntheticEvent} from 'CoreEventTypes'; |
| 21 | +import type {NativeComponent} from 'ReactNative'; |
22 | 22 |
|
23 |
| -type DefaultProps = { |
24 |
| - value: boolean, |
25 |
| - disabled: boolean, |
26 |
| -}; |
| 23 | +type CheckBoxEvent = SyntheticEvent< |
| 24 | + $ReadOnly<{| |
| 25 | + target: number, |
| 26 | + value: boolean, |
| 27 | + |}>, |
| 28 | +>; |
| 29 | + |
| 30 | +type CommonProps = $ReadOnly<{| |
| 31 | + ...ViewProps, |
| 32 | + |
| 33 | + /** |
| 34 | + * Used in case the props change removes the component. |
| 35 | + */ |
| 36 | + onChange?: ?(event: CheckBoxEvent) => mixed, |
| 37 | + |
| 38 | + /** |
| 39 | + * Invoked with the new value when the value changes. |
| 40 | + */ |
| 41 | + onValueChange?: ?(value: boolean) => mixed, |
| 42 | + |
| 43 | + /** |
| 44 | + * Used to locate this view in end-to-end tests. |
| 45 | + */ |
| 46 | + testID?: ?string, |
| 47 | +|}>; |
| 48 | + |
| 49 | +type NativeProps = $ReadOnly<{| |
| 50 | + ...CommonProps, |
| 51 | + |
| 52 | + on?: ?boolean, |
| 53 | + enabled?: boolean, |
| 54 | +|}>; |
| 55 | + |
| 56 | +type CheckBoxNativeType = Class<NativeComponent<NativeProps>>; |
| 57 | + |
| 58 | +type Props = $ReadOnly<{| |
| 59 | + ...CommonProps, |
| 60 | + |
| 61 | + /** |
| 62 | + * The value of the checkbox. If true the checkbox will be turned on. |
| 63 | + * Default value is false. |
| 64 | + */ |
| 65 | + value?: ?boolean, |
| 66 | + |
| 67 | + /** |
| 68 | + * If true the user won't be able to toggle the checkbox. |
| 69 | + * Default value is false. |
| 70 | + */ |
| 71 | + disabled?: ?boolean, |
| 72 | + |
| 73 | + /** |
| 74 | + * Used to get the ref for the native checkbox |
| 75 | + */ |
| 76 | + forwardedRef?: ?React.Ref<CheckBoxNativeType>, |
| 77 | +|}>; |
| 78 | + |
| 79 | +const RCTCheckBox = ((requireNativeComponent( |
| 80 | + 'AndroidCheckBox', |
| 81 | +): any): CheckBoxNativeType); |
27 | 82 |
|
28 | 83 | /**
|
29 | 84 | * Renders a boolean input (Android only).
|
@@ -80,85 +135,64 @@ type DefaultProps = {
|
80 | 135 | * @keyword checkbox
|
81 | 136 | * @keyword toggle
|
82 | 137 | */
|
83 |
| -let CheckBox = createReactClass({ |
84 |
| - displayName: 'CheckBox', |
85 |
| - propTypes: { |
86 |
| - ...DeprecatedViewPropTypes, |
87 |
| - /** |
88 |
| - * The value of the checkbox. If true the checkbox will be turned on. |
89 |
| - * Default value is false. |
90 |
| - */ |
91 |
| - value: PropTypes.bool, |
92 |
| - /** |
93 |
| - * If true the user won't be able to toggle the checkbox. |
94 |
| - * Default value is false. |
95 |
| - */ |
96 |
| - disabled: PropTypes.bool, |
97 |
| - /** |
98 |
| - * Used in case the props change removes the component. |
99 |
| - */ |
100 |
| - onChange: PropTypes.func, |
101 |
| - /** |
102 |
| - * Invoked with the new value when the value changes. |
103 |
| - */ |
104 |
| - onValueChange: PropTypes.func, |
105 |
| - /** |
106 |
| - * Used to locate this view in end-to-end tests. |
107 |
| - */ |
108 |
| - testID: PropTypes.string, |
109 |
| - }, |
| 138 | +class CheckBox extends React.Component<Props> { |
| 139 | + _nativeRef: ?React.ElementRef<CheckBoxNativeType> = null; |
| 140 | + _setNativeRef = setAndForwardRef({ |
| 141 | + getForwardedRef: () => this.props.forwardedRef, |
| 142 | + setLocalRef: ref => { |
| 143 | + this._nativeRef = ref; |
| 144 | + }, |
| 145 | + }); |
110 | 146 |
|
111 |
| - getDefaultProps: function(): DefaultProps { |
112 |
| - return { |
113 |
| - value: false, |
114 |
| - disabled: false, |
115 |
| - }; |
116 |
| - }, |
117 |
| - |
118 |
| - mixins: [NativeMethodsMixin], |
119 |
| - |
120 |
| - _rctCheckBox: {}, |
121 |
| - _onChange: function(event: Object) { |
122 |
| - this._rctCheckBox.setNativeProps({value: this.props.value}); |
| 147 | + _onChange = (event: CheckBoxEvent) => { |
| 148 | + const value = this.props.value ?? false; |
| 149 | + nullthrows(this._nativeRef).setNativeProps({value: value}); |
123 | 150 | // Change the props after the native props are set in case the props
|
124 | 151 | // change removes the component
|
125 | 152 | this.props.onChange && this.props.onChange(event);
|
126 | 153 | this.props.onValueChange &&
|
127 | 154 | this.props.onValueChange(event.nativeEvent.value);
|
128 |
| - }, |
| 155 | + }; |
129 | 156 |
|
130 |
| - render: function() { |
131 |
| - let props = {...this.props}; |
132 |
| - props.onStartShouldSetResponder = () => true; |
133 |
| - props.onResponderTerminationRequest = () => false; |
134 |
| - /* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found |
135 |
| - * when making Flow check .android.js files. */ |
136 |
| - props.enabled = !this.props.disabled; |
137 |
| - /* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found |
138 |
| - * when making Flow check .android.js files. */ |
139 |
| - props.on = this.props.value; |
140 |
| - props.style = [styles.rctCheckBox, this.props.style]; |
| 157 | + render() { |
| 158 | + const {disabled: _, value: __, style, forwardedRef, ...props} = this.props; |
| 159 | + const disabled = this.props.disabled ?? false; |
| 160 | + const value = this.props.value ?? false; |
| 161 | + |
| 162 | + const nativeProps = { |
| 163 | + ...props, |
| 164 | + onStartShouldSetResponder: () => true, |
| 165 | + onResponderTerminationRequest: () => false, |
| 166 | + enabled: !disabled, |
| 167 | + on: value, |
| 168 | + style: [styles.rctCheckBox, style], |
| 169 | + }; |
141 | 170 |
|
142 | 171 | return (
|
143 | 172 | <RCTCheckBox
|
144 |
| - {...props} |
145 |
| - ref={ref => { |
146 |
| - /* $FlowFixMe(>=0.53.0 site=react_native_fb,react_native_oss) This |
147 |
| - * comment suppresses an error when upgrading Flow's support for |
148 |
| - * React. To see the error delete this comment and run Flow. */ |
149 |
| - this._rctCheckBox = ref; |
150 |
| - }} |
| 173 | + {...nativeProps} |
| 174 | + ref={this._setNativeRef} |
151 | 175 | onChange={this._onChange}
|
152 | 176 | />
|
153 | 177 | );
|
154 |
| - }, |
155 |
| -}); |
| 178 | + } |
| 179 | +} |
156 | 180 |
|
157 |
| -let styles = StyleSheet.create({ |
| 181 | +const styles = StyleSheet.create({ |
158 | 182 | rctCheckBox: {
|
159 | 183 | height: 32,
|
160 | 184 | width: 32,
|
161 | 185 | },
|
162 | 186 | });
|
163 | 187 |
|
164 |
| -module.exports = CheckBox; |
| 188 | +/** |
| 189 | + * Can't use CheckBoxNativeType because it has different props |
| 190 | + */ |
| 191 | +type CheckBoxType = Class<NativeComponent<Props>>; |
| 192 | + |
| 193 | +// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet. |
| 194 | +const CheckBoxWithRef = React.forwardRef(function CheckBoxWithRef(props, ref) { |
| 195 | + return <CheckBox {...props} forwardedRef={ref} />; |
| 196 | +}); |
| 197 | + |
| 198 | +module.exports = (CheckBoxWithRef: CheckBoxType); |
0 commit comments