Skip to content

Commit a9d9fc7

Browse files
committed
[Native] Add FeatureFlag to dispatch events with instance currentTarget (#17345)
* [Native] Add FeatureFlag to dispatch events with instance targets * Prettier * [Native] Change currentTarget to be an instance behind a flag 2/2
1 parent 731456a commit a9d9fc7

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

packages/react-native-renderer/src/ReactFabricComponentTree.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77

88
import invariant from 'shared/invariant';
99

10+
import {enableNativeTargetAsInstance} from 'shared/ReactFeatureFlags';
11+
1012
function getInstanceFromInstance(instanceHandle) {
1113
return instanceHandle;
1214
}
1315

1416
function getTagFromInstance(inst) {
15-
let tag = inst.stateNode.canonical._nativeTag;
16-
invariant(tag, 'All native instances should have a tag.');
17-
return tag;
17+
if (enableNativeTargetAsInstance) {
18+
let nativeInstance = inst.stateNode.canonical;
19+
invariant(
20+
nativeInstance._nativeTag,
21+
'All native instances should have a tag.',
22+
);
23+
return nativeInstance;
24+
} else {
25+
let tag = inst.stateNode.canonical._nativeTag;
26+
invariant(tag, 'All native instances should have a tag.');
27+
return tag;
28+
}
1829
}
1930

2031
export {

packages/react-native-renderer/src/ReactNativeComponentTree.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import invariant from 'shared/invariant';
99

10+
import {enableNativeTargetAsInstance} from 'shared/ReactFeatureFlags';
11+
1012
const instanceCache = new Map();
1113
const instanceProps = new Map();
1214

@@ -24,12 +26,23 @@ function getInstanceFromTag(tag) {
2426
}
2527

2628
function getTagFromInstance(inst) {
27-
let tag = inst.stateNode._nativeTag;
28-
if (tag === undefined) {
29-
tag = inst.stateNode.canonical._nativeTag;
29+
if (enableNativeTargetAsInstance) {
30+
let nativeInstance = inst.stateNode;
31+
let tag = nativeInstance._nativeTag;
32+
if (tag === undefined) {
33+
nativeInstance = nativeInstance.canonical;
34+
tag = nativeInstance._nativeTag;
35+
}
36+
invariant(tag, 'All native instances should have a tag.');
37+
return nativeInstance;
38+
} else {
39+
let tag = inst.stateNode._nativeTag;
40+
if (tag === undefined) {
41+
tag = inst.stateNode.canonical._nativeTag;
42+
}
43+
invariant(tag, 'All native instances should have a tag.');
44+
return tag;
3045
}
31-
invariant(tag, 'All native instances should have a tag.');
32-
return tag;
3346
}
3447

3548
export {

packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,9 @@ describe('ReactFabric', () => {
826826
expect(ReactFabric.findNodeHandle(ref1.current)).toEqual(
827827
event.target,
828828
);
829+
expect(ReactFabric.findNodeHandle(ref1.current)).toEqual(
830+
event.currentTarget,
831+
);
829832
}}
830833
onStartShouldSetResponder={() => true}
831834
/>
@@ -837,6 +840,9 @@ describe('ReactFabric', () => {
837840
expect(ReactFabric.findNodeHandle(ref2.current)).toEqual(
838841
event.target,
839842
);
843+
expect(ReactFabric.findNodeHandle(ref2.current)).toEqual(
844+
event.currentTarget,
845+
);
840846
}}
841847
onStartShouldSetResponder={() => true}
842848
/>
@@ -875,7 +881,7 @@ describe('ReactFabric', () => {
875881
changedTouches: [],
876882
});
877883

878-
expect.assertions(4);
884+
expect.assertions(6);
879885
});
880886

881887
it('dispatches event with target as instance', () => {
@@ -922,6 +928,7 @@ describe('ReactFabric', () => {
922928
expect(ref1.current).not.toBeNull();
923929
// Check for referential equality
924930
expect(ref1.current).toBe(event.target);
931+
expect(ref1.current).toBe(event.currentTarget);
925932
}}
926933
onStartShouldSetResponder={() => true}
927934
/>
@@ -932,6 +939,7 @@ describe('ReactFabric', () => {
932939
expect(ref2.current).not.toBeNull();
933940
// Check for referential equality
934941
expect(ref2.current).toBe(event.target);
942+
expect(ref2.current).toBe(event.currentTarget);
935943
}}
936944
onStartShouldSetResponder={() => true}
937945
/>
@@ -970,7 +978,7 @@ describe('ReactFabric', () => {
970978
changedTouches: [],
971979
});
972980

973-
expect.assertions(4);
981+
expect.assertions(6);
974982
});
975983

976984
it('findHostInstance_DEPRECATED should warn if used to find a host component inside StrictMode', () => {

packages/react-native-renderer/src/__tests__/ReactNativeEvents-test.internal.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ it('dispatches event with target as reactTag', () => {
484484
expect(ReactNative.findNodeHandle(ref1.current)).toEqual(
485485
event.target,
486486
);
487+
expect(ReactNative.findNodeHandle(ref1.current)).toEqual(
488+
event.currentTarget,
489+
);
487490
}}
488491
onStartShouldSetResponder={() => true}
489492
/>
@@ -495,6 +498,9 @@ it('dispatches event with target as reactTag', () => {
495498
expect(ReactNative.findNodeHandle(ref2.current)).toEqual(
496499
event.target,
497500
);
501+
expect(ReactNative.findNodeHandle(ref2.current)).toEqual(
502+
event.currentTarget,
503+
);
498504
}}
499505
onStartShouldSetResponder={() => true}
500506
/>
@@ -526,7 +532,7 @@ it('dispatches event with target as reactTag', () => {
526532
[0],
527533
);
528534

529-
expect.assertions(4);
535+
expect.assertions(6);
530536
});
531537

532538
it('dispatches event with target as instance', () => {
@@ -553,6 +559,7 @@ it('dispatches event with target as instance', () => {
553559
expect(ref1.current).not.toBeNull();
554560
// Check for referential equality
555561
expect(ref1.current).toBe(event.target);
562+
expect(ref1.current).toBe(event.currentTarget);
556563
}}
557564
onStartShouldSetResponder={() => true}
558565
/>
@@ -563,6 +570,7 @@ it('dispatches event with target as instance', () => {
563570
expect(ref2.current).not.toBeNull();
564571
// Check for referential equality
565572
expect(ref2.current).toBe(event.target);
573+
expect(ref2.current).toBe(event.currentTarget);
566574
}}
567575
onStartShouldSetResponder={() => true}
568576
/>
@@ -594,5 +602,5 @@ it('dispatches event with target as instance', () => {
594602
[0],
595603
);
596604

597-
expect.assertions(4);
605+
expect.assertions(6);
598606
});

0 commit comments

Comments
 (0)