Skip to content

Commit

Permalink
Migrate TouchableNativeFeedback to use codegenNativeCommands
Browse files Browse the repository at this point in the history
Summary:
Instead of dispatching the command with findNodeHandle and the UIManager, go through the new API. This is safe because codegenNativeCommands can work at runtime as well as with the babel transform.

Changelog:
[Internal]

Reviewed By: rickhanlonii

Differential Revision: D16909599

fbshipit-source-id: 90252862374290dbeb7202483fa585b6a7051c12
  • Loading branch information
elicwhite authored and facebook-github-bot committed Aug 22, 2019
1 parent 3b7eb7e commit 2f7732b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
20 changes: 8 additions & 12 deletions Libraries/Components/Touchable/TouchableNativeFeedback.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
const Platform = require('../../Utilities/Platform');
const React = require('react');
const PropTypes = require('prop-types');
const ReactNative = require('../../Renderer/shims/ReactNative');
const Touchable = require('./Touchable');
const TouchableWithoutFeedback = require('./TouchableWithoutFeedback');
const UIManager = require('../../ReactNative/UIManager');
const View = require('../View/View');
const {Commands: ViewCommands} = require('../View/ViewNativeComponent');

const createReactClass = require('create-react-class');
const ensurePositiveDelayProps = require('./ensurePositiveDelayProps');
Expand Down Expand Up @@ -262,20 +261,16 @@ const TouchableNativeFeedback = createReactClass({
);
},

_handleRef: function(ref) {
this._viewRef = ref;
},

_dispatchHotspotUpdate: function(destX, destY) {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this),
UIManager.getViewManagerConfig('RCTView').Commands.hotspotUpdate,
[destX || 0, destY || 0],
);
ViewCommands.hotspotUpdate(this._viewRef, destX || 0, destY || 0);
},

_dispatchPressedStateChange: function(pressed) {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this),
UIManager.getViewManagerConfig('RCTView').Commands.setPressed,
[pressed],
);
ViewCommands.setPressed(this._viewRef, pressed);
},

render: function() {
Expand Down Expand Up @@ -318,6 +313,7 @@ const TouchableNativeFeedback = createReactClass({
accessibilityActions: this.props.accessibilityActions,
onAccessibilityAction: this.props.onAccessibilityAction,
children,
ref: this._handleRef,
testID: this.props.testID,
onLayout: this.props.onLayout,
hitSlop: this.props.hitSlop,
Expand Down
21 changes: 21 additions & 0 deletions Libraries/Components/View/ViewNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@

'use strict';

const React = require('react');
const Platform = require('../../Utilities/Platform');
const ReactNative = require('../../Renderer/shims/ReactNative');
const ReactNativeViewViewConfigAndroid = require('./ReactNativeViewViewConfigAndroid');

const registerGeneratedViewConfig = require('../../Utilities/registerGeneratedViewConfig');
const requireNativeComponent = require('../../ReactNative/requireNativeComponent');
const codegenNativeCommands = require('../../Utilities/codegenNativeCommands')
.default;

import type {Int32} from '../../Types/CodegenTypes';
import type {ViewProps} from './ViewPropTypes';

export type ViewNativeComponentType = Class<
Expand Down Expand Up @@ -66,5 +70,22 @@ if (__DEV__) {
NativeViewComponent = requireNativeComponent('RCTView');
}

// These commands are Android only
interface NativeCommands {
+hotspotUpdate: (
viewRef: React.ElementRef<ViewNativeComponentType>,
x: Int32,
y: Int32,
) => void;
+setPressed: (
viewRef: React.ElementRef<ViewNativeComponentType>,
pressed: boolean,
) => void;
}

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

export const __INTERNAL_VIEW_CONFIG = viewConfig;
export default ((NativeViewComponent: any): ViewNativeComponentType);

0 comments on commit 2f7732b

Please sign in to comment.