7
7
* @flow
8
8
*/
9
9
10
- import type { ElementRef } from 'react' ;
11
- import type {
12
- HostComponent ,
13
- MeasureInWindowOnSuccessCallback ,
14
- MeasureLayoutOnSuccessCallback ,
15
- MeasureOnSuccessCallback ,
16
- NativeMethods ,
17
- ViewConfig ,
18
- TouchedViewDataAtPoint ,
19
- } from './ReactNativeTypes' ;
20
-
10
+ import type { TouchedViewDataAtPoint , ViewConfig } from './ReactNativeTypes' ;
21
11
import {
22
- mountSafeCallback_NOT_REALLY_SAFE ,
23
- warnForStyleProps ,
24
- } from './NativeMethodsMixinUtils ' ;
12
+ createPublicInstance ,
13
+ type ReactFabricHostComponent ,
14
+ } from './ReactFabricPublicInstance ' ;
25
15
import { create , diff } from './ReactNativeAttributePayload' ;
26
-
27
16
import { dispatchEvent } from './ReactFabricEventEmitter' ;
28
-
29
17
import {
30
18
DefaultEventPriority ,
31
19
DiscreteEventPriority ,
@@ -34,7 +22,6 @@ import {
34
22
// Modules provided by RN:
35
23
import {
36
24
ReactNativeViewConfigRegistry ,
37
- TextInputState ,
38
25
deepFreezeAndThrowOnMutationInDev ,
39
26
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface' ;
40
27
@@ -49,14 +36,9 @@ const {
49
36
appendChildToSet : appendChildNodeToSet ,
50
37
completeRoot,
51
38
registerEventHandler,
52
- measure : fabricMeasure ,
53
- measureInWindow : fabricMeasureInWindow ,
54
- measureLayout : fabricMeasureLayout ,
55
39
unstable_DefaultEventPriority : FabricDefaultPriority ,
56
40
unstable_DiscreteEventPriority : FabricDiscretePriority ,
57
41
unstable_getCurrentEventPriority : fabricGetCurrentEventPriority ,
58
- setNativeProps,
59
- getBoundingClientRect : fabricGetBoundingClientRect ,
60
42
} = nativeFabricUIManager ;
61
43
62
44
const { get : getViewConfigForType } = ReactNativeViewConfigRegistry ;
@@ -71,9 +53,18 @@ type Node = Object;
71
53
export type Type = string ;
72
54
export type Props = Object ;
73
55
export type Instance = {
56
+ // Reference to the shadow node.
74
57
node : Node ,
75
- canonical : ReactFabricHostComponent ,
76
- ...
58
+ // Exposed through refs.
59
+ publicInstance : ReactFabricHostComponent ,
60
+ // We define this as an object instead of as separate fields to simplify
61
+ // making copies of instance where `internals` don't change.
62
+ internals : {
63
+ nativeTag : number ,
64
+ viewConfig : ViewConfig ,
65
+ currentProps : Props ,
66
+ internalInstanceHandle : Object ,
67
+ } ,
77
68
} ;
78
69
export type TextInstance = { node : Node , ...} ;
79
70
export type HydratableInstance = Instance | TextInstance ;
@@ -107,114 +98,6 @@ if (registerEventHandler) {
107
98
registerEventHandler ( dispatchEvent ) ;
108
99
}
109
100
110
- /**
111
- * This is used for refs on host components.
112
- */
113
- class ReactFabricHostComponent implements NativeMethods {
114
- _nativeTag : number ;
115
- viewConfig : ViewConfig ;
116
- currentProps : Props ;
117
- _internalInstanceHandle : Object ;
118
-
119
- constructor (
120
- tag : number ,
121
- viewConfig : ViewConfig ,
122
- props : Props ,
123
- internalInstanceHandle : Object ,
124
- ) {
125
- this . _nativeTag = tag ;
126
- this . viewConfig = viewConfig ;
127
- this . currentProps = props ;
128
- this . _internalInstanceHandle = internalInstanceHandle ;
129
- }
130
-
131
- blur ( ) {
132
- TextInputState . blurTextInput ( this ) ;
133
- }
134
-
135
- focus ( ) {
136
- TextInputState . focusTextInput ( this ) ;
137
- }
138
-
139
- measure ( callback : MeasureOnSuccessCallback ) {
140
- const { stateNode} = this . _internalInstanceHandle ;
141
- if ( stateNode != null ) {
142
- fabricMeasure (
143
- stateNode . node ,
144
- mountSafeCallback_NOT_REALLY_SAFE ( this , callback ) ,
145
- ) ;
146
- }
147
- }
148
-
149
- measureInWindow ( callback : MeasureInWindowOnSuccessCallback ) {
150
- const { stateNode} = this . _internalInstanceHandle ;
151
- if ( stateNode != null ) {
152
- fabricMeasureInWindow (
153
- stateNode . node ,
154
- mountSafeCallback_NOT_REALLY_SAFE ( this , callback ) ,
155
- ) ;
156
- }
157
- }
158
-
159
- measureLayout (
160
- relativeToNativeNode : number | ElementRef < HostComponent < mixed >> ,
161
- onSuccess : MeasureLayoutOnSuccessCallback ,
162
- onFail ?: ( ) => void /* currently unused */ ,
163
- ) {
164
- if (
165
- typeof relativeToNativeNode === 'number' ||
166
- ! ( relativeToNativeNode instanceof ReactFabricHostComponent )
167
- ) {
168
- if ( __DEV__ ) {
169
- console . error (
170
- 'Warning: ref.measureLayout must be called with a ref to a native component.' ,
171
- ) ;
172
- }
173
-
174
- return ;
175
- }
176
-
177
- const toStateNode = this . _internalInstanceHandle . stateNode ;
178
- const fromStateNode =
179
- relativeToNativeNode . _internalInstanceHandle . stateNode ;
180
-
181
- if ( toStateNode != null && fromStateNode != null ) {
182
- fabricMeasureLayout (
183
- toStateNode . node ,
184
- fromStateNode . node ,
185
- mountSafeCallback_NOT_REALLY_SAFE ( this , onFail ) ,
186
- mountSafeCallback_NOT_REALLY_SAFE ( this , onSuccess ) ,
187
- ) ;
188
- }
189
- }
190
-
191
- unstable_getBoundingClientRect ( ) : DOMRect {
192
- const { stateNode } = this . _internalInstanceHandle ;
193
- if ( stateNode != null ) {
194
- const rect = fabricGetBoundingClientRect ( stateNode . node ) ;
195
-
196
- if ( rect ) {
197
- return new DOMRect ( rect [ 0 ] , rect [ 1 ] , rect [ 2 ] , rect [ 3 ] ) ;
198
- }
199
- }
200
-
201
- // Empty rect if any of the above failed
202
- return new DOMRect ( 0 , 0 , 0 , 0 ) ;
203
- }
204
-
205
- setNativeProps ( nativeProps : Object ) {
206
- if ( __DEV__ ) {
207
- warnForStyleProps ( nativeProps , this . viewConfig . validAttributes ) ;
208
- }
209
- const updatePayload = create ( nativeProps , this . viewConfig . validAttributes ) ;
210
-
211
- const { stateNode } = this . _internalInstanceHandle ;
212
- if ( stateNode != null && updatePayload != null ) {
213
- setNativeProps ( stateNode . node , updatePayload ) ;
214
- }
215
- }
216
- }
217
-
218
101
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoMutation' ;
219
102
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoHydration' ;
220
103
export * from 'react-reconciler/src/ReactFiberHostConfigWithNoScopes' ;
@@ -260,16 +143,21 @@ export function createInstance(
260
143
internalInstanceHandle , // internalInstanceHandle
261
144
) ;
262
145
263
- const component = new ReactFabricHostComponent (
146
+ const component = createPublicInstance (
264
147
tag ,
265
148
viewConfig ,
266
- props ,
267
149
internalInstanceHandle ,
268
150
) ;
269
151
270
152
return {
271
153
node : node ,
272
- canonical : component ,
154
+ publicInstance : component ,
155
+ internals : {
156
+ nativeTag : tag ,
157
+ viewConfig,
158
+ currentProps : props ,
159
+ internalInstanceHandle,
160
+ } ,
273
161
} ;
274
162
}
275
163
@@ -339,12 +227,15 @@ export function getChildHostContext(
339
227
}
340
228
341
229
export function getPublicInstance ( instance : Instance ) : null | PublicInstance {
342
- if ( instance . canonical ) {
343
- return instance . canonical ;
230
+ if ( instance . publicInstance != null ) {
231
+ return instance . publicInstance ;
344
232
}
345
233
346
- // For compatibility with Paper
234
+ // For compatibility with the legacy renderer, in case it's used with Fabric
235
+ // in the same app.
236
+ // $FlowExpectedError[prop-missing]
347
237
if ( instance . _nativeTag != null ) {
238
+ // $FlowExpectedError[incompatible-return]
348
239
return instance ;
349
240
}
350
241
@@ -363,12 +254,12 @@ export function prepareUpdate(
363
254
newProps : Props ,
364
255
hostContext : HostContext ,
365
256
) : null | Object {
366
- const viewConfig = instance . canonical . viewConfig ;
257
+ const viewConfig = instance . internals . viewConfig ;
367
258
const updatePayload = diff ( oldProps , newProps , viewConfig . validAttributes ) ;
368
259
// TODO: If the event handlers have changed, we need to update the current props
369
260
// in the commit phase but there is no host config hook to do it yet.
370
261
// So instead we hack it by updating it in the render phase.
371
- instance . canonical . currentProps = newProps ;
262
+ instance . internals . currentProps = newProps ;
372
263
return updatePayload ;
373
264
}
374
265
@@ -447,7 +338,8 @@ export function cloneInstance(
447
338
}
448
339
return {
449
340
node : clone ,
450
- canonical : instance . canonical ,
341
+ publicInstance : instance . publicInstance ,
342
+ internals : instance . internals ,
451
343
} ;
452
344
}
453
345
@@ -457,15 +349,16 @@ export function cloneHiddenInstance(
457
349
props : Props ,
458
350
internalInstanceHandle : Object ,
459
351
) : Instance {
460
- const viewConfig = instance . canonical . viewConfig ;
352
+ const viewConfig = instance . internals . viewConfig ;
461
353
const node = instance . node ;
462
354
const updatePayload = create (
463
355
{ style : { display : 'none' } } ,
464
356
viewConfig . validAttributes ,
465
357
) ;
466
358
return {
467
359
node : cloneNodeWithNewProps ( node , updatePayload ) ,
468
- canonical : instance . canonical ,
360
+ publicInstance : instance . publicInstance ,
361
+ internals : instance . internals ,
469
362
} ;
470
363
}
471
364
0 commit comments