Skip to content

Commit

Permalink
Use Switch command from JavaScript
Browse files Browse the repository at this point in the history
Summary: Changelog: Switch on iOS now uses command instead of `setNativeProps`.

Reviewed By: lunaleaps

Differential Revision: D17714895

fbshipit-source-id: 0e8784fc1d0a57c563b0a4c038febdc0320af11e
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Nov 4, 2019
1 parent b6a23d8 commit 4eb8a95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Libraries/Components/Switch/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<{|
Expand Down Expand Up @@ -208,7 +210,7 @@ class Switch extends React.Component<Props> {
const nativeProps = {};
const value = this.props.value === true;

if (this._lastNativeValue !== value && typeof value === 'boolean') {
if (this._lastNativeValue !== value) {
nativeProps.value = value;
}

Expand All @@ -223,7 +225,7 @@ class Switch extends React.Component<Props> {
nativeProps.value,
);
} else {
this._nativeSwitchRef.setNativeProps(nativeProps);
SwitchCommands.setValue(this._nativeSwitchRef, nativeProps.value);
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion Libraries/Components/Switch/SwitchNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<{|
Expand All @@ -40,6 +42,17 @@ type NativeProps = $ReadOnly<{|
onChange?: ?BubblingEventHandler<SwitchChangeEvent>,
|}>;

type ComponentType = HostComponent<NativeProps>;

interface NativeCommands {
+setValue: (viewRef: React.ElementRef<ComponentType>, value: boolean) => void;
}

export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
supportedCommands: ['setValue'],
});

export default (codegenNativeComponent<NativeProps>('Switch', {
paperComponentName: 'RCTSwitch',
}): HostComponent<NativeProps>);
excludedPlatform: 'android',
}): ComponentType);

0 comments on commit 4eb8a95

Please sign in to comment.