UNPKG

6.25 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 * @flow
9 */
10
11import React from 'react';
12
13export type MeasureOnSuccessCallback = (
14 x: number,
15 y: number,
16 width: number,
17 height: number,
18 pageX: number,
19 pageY: number,
20) => void;
21
22export type MeasureInWindowOnSuccessCallback = (
23 x: number,
24 y: number,
25 width: number,
26 height: number,
27) => void;
28
29export type MeasureLayoutOnSuccessCallback = (
30 left: number,
31 top: number,
32 width: number,
33 height: number,
34) => void;
35
36type AttributeType =
37 | true
38 | $ReadOnly<{|
39 diff?: <T>(arg1: T, arg2: T) => boolean,
40 process?: (arg1: any) => any,
41 |}>;
42
43export type AttributeConfiguration<
44 TProps = string,
45 TStyleProps = string,
46> = $ReadOnly<{
47 [propName: TProps]: AttributeType,
48 style: $ReadOnly<{
49 [propName: TStyleProps]: AttributeType,
50 }>,
51}>;
52
53export type ReactNativeBaseComponentViewConfig<
54 TProps = string,
55 TStyleProps = string,
56> = $ReadOnly<{|
57 baseModuleName?: string,
58 bubblingEventTypes?: $ReadOnly<{
59 [eventName: string]: $ReadOnly<{|
60 phasedRegistrationNames: $ReadOnly<{|
61 captured: string,
62 bubbled: string,
63 |}>,
64 |}>,
65 }>,
66 Commands?: $ReadOnly<{
67 [commandName: string]: number,
68 }>,
69 directEventTypes?: $ReadOnly<{
70 [eventName: string]: $ReadOnly<{|
71 registrationName: string,
72 |}>,
73 }>,
74 NativeProps?: $ReadOnly<{
75 [propName: string]: string,
76 }>,
77 uiViewClassName: string,
78 validAttributes: AttributeConfiguration<TProps, TStyleProps>,
79|}>;
80
81export type ViewConfigGetter = () => ReactNativeBaseComponentViewConfig<>;
82
83/**
84 * Class only exists for its Flow type.
85 */
86class ReactNativeComponent<Props> extends React.Component<Props> {
87 blur(): void {}
88 focus(): void {}
89 measure(callback: MeasureOnSuccessCallback): void {}
90 measureInWindow(callback: MeasureInWindowOnSuccessCallback): void {}
91 measureLayout(
92 relativeToNativeNode: number | Object,
93 onSuccess: MeasureLayoutOnSuccessCallback,
94 onFail?: () => void,
95 ): void {}
96 setNativeProps(nativeProps: Object): void {}
97}
98
99/**
100 * This type keeps ReactNativeFiberHostComponent and NativeMethodsMixin in sync.
101 * It can also provide types for ReactNative applications that use NMM or refs.
102 */
103export type NativeMethodsMixinType = {
104 blur(): void,
105 focus(): void,
106 measure(callback: MeasureOnSuccessCallback): void,
107 measureInWindow(callback: MeasureInWindowOnSuccessCallback): void,
108 measureLayout(
109 relativeToNativeNode: number | Object,
110 onSuccess: MeasureLayoutOnSuccessCallback,
111 onFail: () => void,
112 ): void,
113 setNativeProps(nativeProps: Object): void,
114};
115
116type SecretInternalsType = {
117 NativeMethodsMixin: NativeMethodsMixinType,
118 computeComponentStackForErrorReporting(tag: number): string,
119 // TODO (bvaughn) Decide which additional types to expose here?
120 // And how much information to fill in for the above types.
121};
122
123type SecretInternalsFabricType = {
124 NativeMethodsMixin: NativeMethodsMixinType,
125};
126
127/**
128 * Flat ReactNative renderer bundles are too big for Flow to parse efficiently.
129 * Provide minimal Flow typing for the high-level RN API and call it a day.
130 */
131export type ReactNativeType = {
132 NativeComponent: typeof ReactNativeComponent,
133 findNodeHandle(componentOrHandle: any): ?number,
134 dispatchCommand(handle: any, command: string, args: Array<any>): void,
135 setNativeProps(handle: any, nativeProps: Object): void,
136 render(
137 element: React$Element<any>,
138 containerTag: any,
139 callback: ?Function,
140 ): any,
141 unmountComponentAtNode(containerTag: number): any,
142 unmountComponentAtNodeAndRemoveContainer(containerTag: number): any,
143 unstable_batchedUpdates: any, // TODO (bvaughn) Add types
144
145 __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: SecretInternalsType,
146};
147
148export type ReactFabricType = {
149 NativeComponent: typeof ReactNativeComponent,
150 findNodeHandle(componentOrHandle: any): ?number,
151 dispatchCommand(handle: any, command: string, args: Array<any>): void,
152 setNativeProps(handle: any, nativeProps: Object): void,
153 render(
154 element: React$Element<any>,
155 containerTag: any,
156 callback: ?Function,
157 ): any,
158 unmountComponentAtNode(containerTag: number): any,
159 __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: SecretInternalsFabricType,
160};
161
162export type ReactNativeEventTarget = {
163 node: Object,
164 canonical: {
165 _nativeTag: number,
166 viewConfig: ReactNativeBaseComponentViewConfig<>,
167 currentProps: Object,
168 _internalInstanceHandle: Object,
169 },
170};
171
172export type ReactFaricEventTouch = {
173 identifier: number,
174 locationX: number,
175 locationY: number,
176 pageX: number,
177 pageY: number,
178 screenX: number,
179 screenY: number,
180 target: number,
181 timestamp: number,
182 force: number,
183};
184
185export type ReactFaricEvent = {
186 touches: Array<ReactFaricEventTouch>,
187 changedTouches: Array<ReactFaricEventTouch>,
188 targetTouches: Array<ReactFaricEventTouch>,
189 target: number,
190};
191
192export type ReactNativeResponderEvent = {
193 nativeEvent: ReactFaricEvent,
194 responderTarget: null | ReactNativeEventTarget,
195 target: null | ReactNativeEventTarget,
196 type: string,
197};
198
199export type ReactNativeResponderContext = {
200 dispatchEvent: (
201 eventValue: any,
202 listener: (any) => void,
203 eventPriority: EventPriority,
204 ) => void,
205 isTargetWithinNode: (
206 childTarget: ReactNativeEventTarget,
207 parentTarget: ReactNativeEventTarget,
208 ) => boolean,
209 getTargetBoundingRect(
210 target: ReactNativeEventTarget,
211 cb: ({
212 left: number,
213 right: number,
214 top: number,
215 bottom: number,
216 }) => void,
217 ): void,
218 addRootEventTypes: (rootEventTypes: Array<string>) => void,
219 removeRootEventTypes: (rootEventTypes: Array<string>) => void,
220 setTimeout: (func: () => void, timeout: number) => number,
221 clearTimeout: (timerId: number) => void,
222 getTimeStamp: () => number,
223};
224
225export type PointerType =
226 | ''
227 | 'mouse'
228 | 'keyboard'
229 | 'pen'
230 | 'touch'
231 | 'trackpad';
232
233export type EventPriority = 0 | 1 | 2;
234
235export const DiscreteEvent: EventPriority = 0;
236export const UserBlockingEvent: EventPriority = 1;
237export const ContinuousEvent: EventPriority = 2;