From 4eb8a951ffd60dfdb4b05145d9d36a8b493a4cd7 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 4 Nov 2019 07:42:19 -0800 Subject: [PATCH] Use Switch command from JavaScript Summary: Changelog: Switch on iOS now uses command instead of `setNativeProps`. Reviewed By: lunaleaps Differential Revision: D17714895 fbshipit-source-id: 0e8784fc1d0a57c563b0a4c038febdc0320af11e --- Libraries/Components/Switch/Switch.js | 8 +++++--- .../Components/Switch/SwitchNativeComponent.js | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Libraries/Components/Switch/Switch.js b/Libraries/Components/Switch/Switch.js index 84a05067541..483d2dc2461 100644 --- a/Libraries/Components/Switch/Switch.js +++ b/Libraries/Components/Switch/Switch.js @@ -18,11 +18,13 @@ const StyleSheet = require('../../StyleSheet/StyleSheet'); import AndroidSwitchNativeComponent, { Commands as AndroidSwitchCommands, } from './AndroidSwitchNativeComponent'; +import SwitchNativeComponent, { + Commands as SwitchCommands, +} from './SwitchNativeComponent'; import type {ColorValue} from '../../StyleSheet/StyleSheetTypes'; import type {SyntheticEvent} from '../../Types/CoreEventTypes'; import type {ViewProps} from '../View/ViewPropTypes'; -import SwitchNativeComponent from './SwitchNativeComponent'; type SwitchChangeEvent = SyntheticEvent< $ReadOnly<{| @@ -208,7 +210,7 @@ class Switch extends React.Component { const nativeProps = {}; const value = this.props.value === true; - if (this._lastNativeValue !== value && typeof value === 'boolean') { + if (this._lastNativeValue !== value) { nativeProps.value = value; } @@ -223,7 +225,7 @@ class Switch extends React.Component { nativeProps.value, ); } else { - this._nativeSwitchRef.setNativeProps(nativeProps); + SwitchCommands.setValue(this._nativeSwitchRef, nativeProps.value); } } } diff --git a/Libraries/Components/Switch/SwitchNativeComponent.js b/Libraries/Components/Switch/SwitchNativeComponent.js index 13ac5b46d2c..262f3d0dea9 100644 --- a/Libraries/Components/Switch/SwitchNativeComponent.js +++ b/Libraries/Components/Switch/SwitchNativeComponent.js @@ -13,8 +13,10 @@ import type {BubblingEventHandler, WithDefault} from '../../Types/CodegenTypes'; import type {ColorValue} from '../../StyleSheet/StyleSheetTypes'; import type {ViewProps} from '../View/ViewPropTypes'; +import * as React from 'react'; import codegenNativeComponent from '../../Utilities/codegenNativeComponent'; +import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands'; import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes'; type SwitchChangeEvent = $ReadOnly<{| @@ -40,6 +42,17 @@ type NativeProps = $ReadOnly<{| onChange?: ?BubblingEventHandler, |}>; +type ComponentType = HostComponent; + +interface NativeCommands { + +setValue: (viewRef: React.ElementRef, value: boolean) => void; +} + +export const Commands: NativeCommands = codegenNativeCommands({ + supportedCommands: ['setValue'], +}); + export default (codegenNativeComponent('Switch', { paperComponentName: 'RCTSwitch', -}): HostComponent); + excludedPlatform: 'android', +}): ComponentType);