Skip to content

Commit

Permalink
Move Switch native require call to new file
Browse files Browse the repository at this point in the history
Summary: Moving out the requireNativeComponent call into a new file. We want this long term for all of our view managers to support codegen of the native side and so we can move the viewConfigs into JS.

Reviewed By: yungsters

Differential Revision: D9191214

fbshipit-source-id: d0bddbb50bb1cf6b5a727d72faf834b007ad9440
  • Loading branch information
elicwhite authored and facebook-github-bot committed Aug 8, 2018
1 parent 51b12dd commit 457a74c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
50 changes: 8 additions & 42 deletions Libraries/Components/Switch/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@

'use strict';

const SwitchNativeComponent = require('SwitchNativeComponent');
const Platform = require('Platform');
const React = require('React');
const ReactNative = require('ReactNative');
const StyleSheet = require('StyleSheet');

const requireNativeComponent = require('requireNativeComponent');

import type {SwitchChangeEvent} from 'CoreEventTypes';
import type {ColorValue} from 'StyleSheetTypes';
import type {ViewProps} from 'ViewPropTypes';
import type {NativeAndroidProps, NativeIOSProps} from 'SwitchNativeComponent';

export type Props = $ReadOnly<{|
...ViewProps,
Expand Down Expand Up @@ -75,41 +74,6 @@ export type Props = $ReadOnly<{|
onValueChange?: ?(value: boolean) => Promise<void> | void,
|}>;

// @see ReactSwitchManager.java
type NativeAndroidProps = $ReadOnly<{|
...ViewProps,
enabled?: ?boolean,
on?: ?boolean,
onChange?: ?(event: SwitchChangeEvent) => mixed,
thumbTintColor?: ?string,
trackTintColor?: ?string,
|}>;

// @see RCTSwitchManager.m
type NativeIOSProps = $ReadOnly<{|
...ViewProps,
disabled?: ?boolean,
onChange?: ?(event: SwitchChangeEvent) => mixed,
onTintColor?: ?string,
thumbTintColor?: ?string,
tintColor?: ?string,
value?: ?boolean,
|}>;

type NativeSwitchType = Class<
ReactNative.NativeComponent<
$ReadOnly<{|
...NativeAndroidProps,
...NativeIOSProps,
|}>,
>,
>;

const NativeSwitch: NativeSwitchType =
Platform.OS === 'android'
? (requireNativeComponent('AndroidSwitch'): any)
: (requireNativeComponent('RCTSwitch'): any);

/**
* A visual toggle between two mutually exclusive states.
*
Expand All @@ -119,7 +83,7 @@ const NativeSwitch: NativeSwitchType =
* supplied `value` prop instead of the expected result of any user actions.
*/
class Switch extends React.Component<Props> {
_nativeSwitchRef: ?React.ElementRef<NativeSwitchType>;
_nativeSwitchRef: ?React.ElementRef<typeof SwitchNativeComponent>;

render() {
const {
Expand Down Expand Up @@ -197,13 +161,13 @@ class Switch extends React.Component<Props> {
}: NativeIOSProps);

return (
<NativeSwitch
<SwitchNativeComponent
{...props}
{...platformProps}
onChange={this._handleChange}
onResponderTerminationRequest={returnsFalse}
onStartShouldSetResponder={returnsTrue}
ref={this._handleNativeSwitchRef}
ref={this._handleSwitchNativeComponentRef}
/>
);
}
Expand All @@ -230,7 +194,9 @@ class Switch extends React.Component<Props> {
}
};

_handleNativeSwitchRef = (ref: ?React.ElementRef<NativeSwitchType>) => {
_handleSwitchNativeComponentRef = (
ref: ?React.ElementRef<typeof SwitchNativeComponent>,
) => {
this._nativeSwitchRef = ref;
};
}
Expand Down
56 changes: 56 additions & 0 deletions Libraries/Components/Switch/SwitchNativeComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/

'use strict';

const Platform = require('Platform');
const ReactNative = require('ReactNative');

const requireNativeComponent = require('requireNativeComponent');

import type {SwitchChangeEvent} from 'CoreEventTypes';
import type {ViewProps} from 'ViewPropTypes';

// @see ReactSwitchManager.java
export type NativeAndroidProps = $ReadOnly<{|
...ViewProps,
enabled?: ?boolean,
on?: ?boolean,
onChange?: ?(event: SwitchChangeEvent) => mixed,
thumbTintColor?: ?string,
trackTintColor?: ?string,
|}>;

// @see RCTSwitchManager.m
export type NativeIOSProps = $ReadOnly<{|
...ViewProps,
disabled?: ?boolean,
onChange?: ?(event: SwitchChangeEvent) => mixed,
onTintColor?: ?string,
thumbTintColor?: ?string,
tintColor?: ?string,
value?: ?boolean,
|}>;

type SwitchNativeComponentType = Class<
ReactNative.NativeComponent<
$ReadOnly<{|
...NativeAndroidProps,
...NativeIOSProps,
|}>,
>,
>;

const SwitchNativeComponent: SwitchNativeComponentType =
Platform.OS === 'android'
? (requireNativeComponent('AndroidSwitch'): any)
: (requireNativeComponent('RCTSwitch'): any);

module.exports = SwitchNativeComponent;

0 comments on commit 457a74c

Please sign in to comment.