Skip to content

Commit 6877263

Browse files
coadofacebook-github-bot
authored andcommitted
Align Flow with TS types and export relevant modules (#50322)
Summary: Pull Request resolved: #50322 Changelog: [Internal] Reviewed By: huntie Differential Revision: D71973327 fbshipit-source-id: ef3a6ad4c0ba6d32bfd1e4cc2ac8e0afac51e9fe
1 parent 1f8d824 commit 6877263

File tree

9 files changed

+87
-22
lines changed

9 files changed

+87
-22
lines changed

packages/react-native/Libraries/Components/Pressable/Pressable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type PressableBaseProps = $ReadOnly<{
4343
* Either children or a render prop that receives a boolean reflecting whether
4444
* the component is currently pressed.
4545
*/
46-
children: React.Node | ((state: PressableStateCallbackType) => React.Node),
46+
children?: React.Node | ((state: PressableStateCallbackType) => React.Node),
4747

4848
/**
4949
* Duration to wait after hover in before calling `onHoverIn`.

packages/react-native/Libraries/EventEmitter/NativeEventEmitter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface NativeModule {
2525
}
2626

2727
export type {EventSubscription};
28+
/** @deprecated Use `EventSubscription` instead. */
29+
export type NativeEventSubscription = EventSubscription;
2830

2931
// $FlowFixMe[unclear-type] unclear type of events
3032
type UnsafeObject = Object;

packages/react-native/Libraries/ReactNative/ReactFabricPublicInstance/ReactFabricHostComponent.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import type {
1212
HostInstance,
13-
LegacyHostInstanceMethods,
1413
MeasureInWindowOnSuccessCallback,
1514
MeasureLayoutOnSuccessCallback,
1615
MeasureOnSuccessCallback,
16+
NativeMethods,
1717
} from '../../../src/private/types/HostInstance';
1818
import type {
1919
InternalInstanceHandle,
@@ -40,9 +40,7 @@ const noop = () => {};
4040
/**
4141
* This is used for refs on host components.
4242
*/
43-
export default class ReactFabricHostComponent
44-
implements LegacyHostInstanceMethods
45-
{
43+
export default class ReactFabricHostComponent implements NativeMethods {
4644
// These need to be accessible from `ReactFabricPublicInstanceUtils`.
4745
__nativeTag: number;
4846
__internalInstanceHandle: InternalInstanceHandle;

packages/react-native/Libraries/ReactPrivate/ReactNativePrivateInterface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type {
4141
HostInstance as PublicInstance,
4242

4343
// These types are only necessary for Paper
44-
LegacyHostInstanceMethods as LegacyPublicInstance,
44+
NativeMethods as LegacyPublicInstance,
4545
MeasureOnSuccessCallback,
4646
MeasureInWindowOnSuccessCallback,
4747
MeasureLayoutOnSuccessCallback,

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ export type PressableStateCallbackType = $ReadOnly<{
17391739
}>;
17401740
type PressableBaseProps = $ReadOnly<{
17411741
cancelable?: ?boolean,
1742-
children: React.Node | ((state: PressableStateCallbackType) => React.Node),
1742+
children?: React.Node | ((state: PressableStateCallbackType) => React.Node),
17431743
delayHoverIn?: ?number,
17441744
delayHoverOut?: ?number,
17451745
delayLongPress?: ?number,
@@ -4424,6 +4424,7 @@ exports[`public API should not change unintentionally Libraries/EventEmitter/Nat
44244424
removeListeners(count: number): void;
44254425
}
44264426
export type { EventSubscription };
4427+
export type NativeEventSubscription = EventSubscription;
44274428
type UnsafeObject = Object;
44284429
declare export default class NativeEventEmitter<
44294430
TEventToArgsMap: $ReadOnly<
@@ -7077,9 +7078,7 @@ declare export default typeof UIManagerJS;
70777078
`;
70787079

70797080
exports[`public API should not change unintentionally Libraries/ReactNative/ReactFabricPublicInstance/ReactFabricHostComponent.js 1`] = `
7080-
"declare export default class ReactFabricHostComponent
7081-
implements LegacyHostInstanceMethods
7082-
{
7081+
"declare export default class ReactFabricHostComponent implements NativeMethods {
70837082
constructor(
70847083
tag: number,
70857084
viewConfig: ViewConfig,
@@ -9305,7 +9304,8 @@ declare export default typeof rejectionTrackingOptions;
93059304
`;
93069305

93079306
exports[`public API should not change unintentionally Libraries/vendor/core/ErrorUtils.js 1`] = `
9308-
"declare export default ErrorUtilsT;
9307+
"export type ErrorUtils = ErrorUtilsT;
9308+
declare export default ErrorUtilsT;
93099309
"
93109310
`;
93119311

@@ -9575,6 +9575,7 @@ export { default as LogBox } from \\"./Libraries/LogBox/LogBox\\";
95759575
export { default as NativeAppEventEmitter } from \\"./Libraries/EventEmitter/RCTNativeAppEventEmitter\\";
95769576
export { default as NativeDialogManagerAndroid } from \\"./Libraries/NativeModules/specs/NativeDialogManagerAndroid\\";
95779577
export { default as NativeEventEmitter } from \\"./Libraries/EventEmitter/NativeEventEmitter\\";
9578+
export type { NativeEventSubscription } from \\"./Libraries/EventEmitter/NativeEventEmitter\\";
95789579
export { default as NativeModules } from \\"./Libraries/BatchedBridge/NativeModules\\";
95799580
export { default as Networking } from \\"./Libraries/Network/RCTNetworking\\";
95809581
export type {
@@ -9660,11 +9661,14 @@ export type {
96609661
export type * from \\"./Libraries/Types/CodegenTypesNamespace\\";
96619662
export type {
96629663
HostInstance,
9664+
NativeMethods,
96639665
MeasureInWindowOnSuccessCallback,
96649666
MeasureLayoutOnSuccessCallback,
96659667
MeasureOnSuccessCallback,
96669668
} from \\"./src/private/types/HostInstance\\";
96679669
export type { HostComponent } from \\"./src/private/types/HostComponent\\";
9670+
export type { ColorSchemeName } from \\"./src/private/specs_DEPRECATED/modules/NativeAppearance\\";
9671+
export type { ErrorUtils } from \\"./Libraries/vendor/core/ErrorUtils\\";
96689672
"
96699673
`;
96709674

@@ -9717,7 +9721,7 @@ export type MeasureLayoutOnSuccessCallback = (
97179721
width: number,
97189722
height: number
97199723
) => void;
9720-
export interface LegacyHostInstanceMethods {
9724+
export interface NativeMethods {
97219725
blur(): void;
97229726
focus(): void;
97239727
measure(callback: MeasureOnSuccessCallback): void;
@@ -9729,7 +9733,7 @@ export interface LegacyHostInstanceMethods {
97299733
): void;
97309734
setNativeProps(nativeProps: { ... }): void;
97319735
}
9732-
export type HostInstance = LegacyHostInstanceMethods;
9736+
export type HostInstance = NativeMethods;
97339737
"
97349738
`;
97359739

@@ -9758,7 +9762,7 @@ declare export function createReactNativeDocument(
97589762
exports[`public API should not change unintentionally src/private/webapis/dom/nodes/ReactNativeElement.js 1`] = `
97599763
"declare class ReactNativeElement
97609764
extends ReadOnlyElement
9761-
implements LegacyHostInstanceMethods
9765+
implements NativeMethods
97629766
{
97639767
constructor(
97649768
tag: number,

packages/react-native/Libraries/vendor/core/ErrorUtils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import type {ErrorUtilsT} from '@react-native/js-polyfills/error-guard';
1212

13+
export type ErrorUtils = ErrorUtilsT;
14+
1315
/**
1416
* The particular require runtime that we are using looks for a global
1517
* `ErrorUtils` object and if it exists, then it requires modules with the

packages/react-native/index.js.flow

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ export {default as NativeAppEventEmitter} from './Libraries/EventEmitter/RCTNati
296296

297297
export {default as NativeDialogManagerAndroid} from './Libraries/NativeModules/specs/NativeDialogManagerAndroid';
298298
export {default as NativeEventEmitter} from './Libraries/EventEmitter/NativeEventEmitter';
299+
export type {NativeEventSubscription} from './Libraries/EventEmitter/NativeEventEmitter';
299300
export {default as NativeModules} from './Libraries/BatchedBridge/NativeModules';
300301
export {default as Networking} from './Libraries/Network/RCTNetworking';
301302

@@ -401,10 +402,13 @@ export type * from './Libraries/Types/CodegenTypesNamespace';
401402

402403
export type {
403404
HostInstance,
405+
NativeMethods,
404406
MeasureInWindowOnSuccessCallback,
405407
MeasureLayoutOnSuccessCallback,
406408
MeasureOnSuccessCallback,
407409
} from './src/private/types/HostInstance';
408410
export type {HostComponent} from './src/private/types/HostComponent';
411+
export type {ColorSchemeName} from './src/private/specs_DEPRECATED/modules/NativeAppearance';
412+
export type {ErrorUtils} from './Libraries/vendor/core/ErrorUtils';
409413

410414
// #endregion

packages/react-native/src/private/types/HostInstance.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,77 @@ export type MeasureLayoutOnSuccessCallback = (
3232
) => void;
3333

3434
/**
35-
* Current usages should migrate to this definition
35+
* NativeMethods provides methods to access the underlying native component directly.
36+
* This can be useful in cases when you want to focus a view or measure its on-screen dimensions,
37+
* for example.
38+
* The methods described here are available on most of the default components provided by React Native.
39+
* Note, however, that they are not available on composite components that aren't directly backed by a
40+
* native view. This will generally include most components that you define in your own app.
41+
* For more information, see [Direct Manipulation](https://reactnative.dev/docs/direct-manipulation).
42+
* @see https://github.com/facebook/react-native/blob/master/Libraries/Renderer/shims/ReactNativeTypes.js#L87
3643
*/
37-
export interface LegacyHostInstanceMethods {
44+
export interface NativeMethods {
45+
/**
46+
* Removes focus from an input or view. This is the opposite of `focus()`.
47+
*/
3848
blur(): void;
49+
/**
50+
* Requests focus for the given input or view. The exact behavior triggered
51+
* will depend on the platform and type of view.
52+
*/
3953
focus(): void;
54+
/**
55+
* Determines the location on screen, width, and height of the given view and
56+
* returns the values via an async callback. If successful, the callback will
57+
* be called with the following arguments:
58+
*
59+
* - x
60+
* - y
61+
* - width
62+
* - height
63+
* - pageX
64+
* - pageY
65+
*
66+
* Note that these measurements are not available until after the rendering
67+
* has been completed in native. If you need the measurements as soon as
68+
* possible, consider using the [`onLayout`
69+
* prop](docs/view.html#onlayout) instead.
70+
*/
4071
measure(callback: MeasureOnSuccessCallback): void;
72+
/**
73+
* Determines the location of the given view in the window and returns the
74+
* values via an async callback. If the React root view is embedded in
75+
* another native view, this will give you the absolute coordinates. If
76+
* successful, the callback will be called with the following
77+
* arguments:
78+
*
79+
* - x
80+
* - y
81+
* - width
82+
* - height
83+
*
84+
* Note that these measurements are not available until after the rendering
85+
* has been completed in native.
86+
*/
4187
measureInWindow(callback: MeasureInWindowOnSuccessCallback): void;
88+
/**
89+
* Like [`measure()`](#measure), but measures the view relative an ancestor,
90+
* specified as `relativeToNativeComponentRef`. This means that the returned x, y
91+
* are relative to the origin x, y of the ancestor view.
92+
* _Can also be called with a relativeNativeNodeHandle but is deprecated._
93+
*/
4294
measureLayout(
4395
relativeToNativeNode: number | HostInstance,
4496
onSuccess: MeasureLayoutOnSuccessCallback,
4597
onFail?: () => void,
4698
): void;
99+
/**
100+
* This function sends props straight to native. They will not participate in
101+
* future diff process - this means that if you do not include them in the
102+
* next render, they will remain active (see [Direct
103+
* Manipulation](https://reactnative.dev/docs/direct-manipulation)).
104+
*/
47105
setNativeProps(nativeProps: {...}): void;
48106
}
49107

50-
export type HostInstance = LegacyHostInstanceMethods;
108+
export type HostInstance = NativeMethods;

packages/react-native/src/private/webapis/dom/nodes/ReactNativeElement.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import type {
1717
} from '../../../../../Libraries/Renderer/shims/ReactNativeTypes';
1818
import type {
1919
HostInstance,
20-
LegacyHostInstanceMethods,
2120
MeasureInWindowOnSuccessCallback,
2221
MeasureLayoutOnSuccessCallback,
2322
MeasureOnSuccessCallback,
23+
NativeMethods,
2424
} from '../../../types/HostInstance';
2525
import type {InstanceHandle} from './internals/NodeInternals';
2626
import type ReactNativeDocument from './ReactNativeDocument';
@@ -60,10 +60,7 @@ const noop = () => {};
6060
// was slower than this method because the engine has to create an object than
6161
// we then discard to create a new one.
6262

63-
class ReactNativeElement
64-
extends ReadOnlyElement
65-
implements LegacyHostInstanceMethods
66-
{
63+
class ReactNativeElement extends ReadOnlyElement implements NativeMethods {
6764
// These need to be accessible from `ReactFabricPublicInstanceUtils`.
6865
__nativeTag: number;
6966
__internalInstanceHandle: InstanceHandle;

0 commit comments

Comments
 (0)