Skip to content

Commit

Permalink
Add HostComponent type to ReactNative (#16898)
Browse files Browse the repository at this point in the history
* Add HostComponent type to ReactNative

* Use type alias imports instead of wildcard

* Fix forgotten Object in measureLayout type
  • Loading branch information
elicwhite authored Sep 26, 2019
1 parent fad5102 commit db8afe4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/react-native-renderer/src/NativeMethodsMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default function(
measureLayout: function(
relativeToNativeNode: number | Object,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail: () => void /* currently unused */,
onFail?: () => void /* currently unused */,
) {
let maybeInstance;

Expand Down
8 changes: 4 additions & 4 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
MeasureInWindowOnSuccessCallback,
MeasureLayoutOnSuccessCallback,
MeasureOnSuccessCallback,
NativeMethodsMixinType,
NativeMethods,
ReactNativeBaseComponentViewConfig,
ReactNativeResponderEvent,
ReactNativeResponderContext,
Expand Down Expand Up @@ -151,9 +151,9 @@ class ReactFabricHostComponent {
}

measureLayout(
relativeToNativeNode: number | Object,
relativeToNativeNode: number | ReactFabricHostComponent,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail: () => void /* currently unused */,
onFail?: () => void /* currently unused */,
) {
if (
typeof relativeToNativeNode === 'number' ||
Expand Down Expand Up @@ -186,7 +186,7 @@ class ReactFabricHostComponent {
}

// eslint-disable-next-line no-unused-expressions
(ReactFabricHostComponent.prototype: NativeMethodsMixinType);
(ReactFabricHostComponent.prototype: NativeMethods);

export * from 'shared/HostConfigWithNoMutation';
export * from 'shared/HostConfigWithNoHydration';
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-renderer/src/ReactNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
MeasureInWindowOnSuccessCallback,
MeasureLayoutOnSuccessCallback,
MeasureOnSuccessCallback,
NativeMethodsMixinType,
NativeMethods,
ReactNativeBaseComponentViewConfig,
} from './ReactNativeTypes';

Expand Down Expand Up @@ -172,7 +172,7 @@ export default function(
measureLayout(
relativeToNativeNode: number | Object,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail: () => void /* currently unused */,
onFail?: () => void /* currently unused */,
): void {
let maybeInstance;

Expand Down Expand Up @@ -295,7 +295,7 @@ export default function(
}

// eslint-disable-next-line no-unused-expressions
(ReactNativeComponent.prototype: NativeMethodsMixinType);
(ReactNativeComponent.prototype: NativeMethods);

return ReactNativeComponent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
MeasureInWindowOnSuccessCallback,
MeasureLayoutOnSuccessCallback,
MeasureOnSuccessCallback,
NativeMethodsMixinType,
NativeMethods,
ReactNativeBaseComponentViewConfig,
} from './ReactNativeTypes';
import type {Instance} from './ReactNativeHostConfig';
Expand Down Expand Up @@ -72,21 +72,25 @@ class ReactNativeFiberHostComponent {
}

measureLayout(
relativeToNativeNode: number | Object,
relativeToNativeNode: number | ReactNativeFiberHostComponent,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail: () => void /* currently unused */,
onFail?: () => void /* currently unused */,
) {
let relativeNode;
let relativeNode: ?number;

if (typeof relativeToNativeNode === 'number') {
// Already a node handle
relativeNode = relativeToNativeNode;
} else if (relativeToNativeNode._nativeTag) {
relativeNode = relativeToNativeNode._nativeTag;
} else if (
/* $FlowFixMe canonical doesn't exist on the node.
I think this branch is dead and will remove it in a followup */
relativeToNativeNode.canonical &&
relativeToNativeNode.canonical._nativeTag
) {
/* $FlowFixMe canonical doesn't exist on the node.
I think this branch is dead and will remove it in a followup */
relativeNode = relativeToNativeNode.canonical._nativeTag;
}

Expand Down Expand Up @@ -137,6 +141,6 @@ class ReactNativeFiberHostComponent {
}

// eslint-disable-next-line no-unused-expressions
(ReactNativeFiberHostComponent.prototype: NativeMethodsMixinType);
(ReactNativeFiberHostComponent.prototype: NativeMethods);

export default ReactNativeFiberHostComponent;
21 changes: 16 additions & 5 deletions packages/react-native-renderer/src/ReactNativeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow
*/

import React from 'react';
import React, {type ElementRef, type AbstractComponent} from 'react';

export type MeasureOnSuccessCallback = (
x: number,
Expand Down Expand Up @@ -89,30 +89,41 @@ class ReactNativeComponent<Props> extends React.Component<Props> {
measure(callback: MeasureOnSuccessCallback): void {}
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void {}
measureLayout(
relativeToNativeNode: number | Object,
relativeToNativeNode: number | ElementRef<HostComponent<mixed>>,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail?: () => void,
): void {}
setNativeProps(nativeProps: Object): void {}
}

// This type is only used for FlowTests. It shouldn't be imported directly
export type _InternalReactNativeComponentClass<Props> = Class<
ReactNativeComponent<Props>,
>;

/**
* This type keeps ReactNativeFiberHostComponent and NativeMethodsMixin in sync.
* It can also provide types for ReactNative applications that use NMM or refs.
*/
export type NativeMethodsMixinType = {
export type NativeMethods = {
blur(): void,
focus(): void,
measure(callback: MeasureOnSuccessCallback): void,
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void,
measureLayout(
relativeToNativeNode: number | Object,
relativeToNativeNode: number | ElementRef<HostComponent<mixed>>,
onSuccess: MeasureLayoutOnSuccessCallback,
onFail: () => void,
onFail?: () => void,
): void,
setNativeProps(nativeProps: Object): void,
};

export type NativeMethodsMixinType = NativeMethods;
export type HostComponent<T> = AbstractComponent<
T,
$ReadOnly<$Exact<NativeMethods>>,
>;

type SecretInternalsType = {
NativeMethodsMixin: NativeMethodsMixinType,
computeComponentStackForErrorReporting(tag: number): string,
Expand Down

0 comments on commit db8afe4

Please sign in to comment.