@samitouri / QOS-React-2 / commits / 3dcec3a925

[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

Eli White committed Nov 11, 2019 at 12:42 UTC 3dcec3a9258e644f5409405ed22103fa1a90b10d
4 files changed +52 -12
packages/react-native-renderer/src/ReactFabricComponentTree.js
+14 -3
@@ -7,14 +7,25 @@
7
8 import invariant from 'shared/invariant';
9
10 +import {enableNativeTargetAsInstance} from 'shared/ReactFeatureFlags';
11 +
12 function getInstanceFromInstance(instanceHandle) {
13 return instanceHandle;
14 }
15
16 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 + }
29 }
30
31 export {
packages/react-native-renderer/src/ReactNativeComponentTree.js
+18 -5
@@ -7,6 +7,8 @@
7
8 import invariant from 'shared/invariant';
9
10 +import {enableNativeTargetAsInstance} from 'shared/ReactFeatureFlags';
11 +
12 const instanceCache = new Map();
13 const instanceProps = new Map();
14
@@ -24,12 +26,23 @@ function getInstanceFromTag(tag) {
26 }
27
28 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;
45 }
31 - invariant(tag, 'All native instances should have a tag.');
32 - return tag;
46 }
47
48 export {
packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js
+10 -2
@@ -826,6 +826,9 @@ describe('ReactFabric', () => {
826 expect(ReactFabric.findNodeHandle(ref1.current)).toEqual(
827 event.target,
828 );
829 + expect(ReactFabric.findNodeHandle(ref1.current)).toEqual(
830 + event.currentTarget,
831 + );
832 }}
833 onStartShouldSetResponder={() => true}
834 />
@@ -837,6 +840,9 @@ describe('ReactFabric', () => {
840 expect(ReactFabric.findNodeHandle(ref2.current)).toEqual(
841 event.target,
842 );
843 + expect(ReactFabric.findNodeHandle(ref2.current)).toEqual(
844 + event.currentTarget,
845 + );
846 }}
847 onStartShouldSetResponder={() => true}
848 />
@@ -875,7 +881,7 @@ describe('ReactFabric', () => {
881 changedTouches: [],
882 });
883
878 - expect.assertions(4);
884 + expect.assertions(6);
885 });
886
887 it('dispatches event with target as instance', () => {
@@ -922,6 +928,7 @@ describe('ReactFabric', () => {
928 expect(ref1.current).not.toBeNull();
929 // Check for referential equality
930 expect(ref1.current).toBe(event.target);
931 + expect(ref1.current).toBe(event.currentTarget);
932 }}
933 onStartShouldSetResponder={() => true}
934 />
@@ -932,6 +939,7 @@ describe('ReactFabric', () => {
939 expect(ref2.current).not.toBeNull();
940 // Check for referential equality
941 expect(ref2.current).toBe(event.target);
942 + expect(ref2.current).toBe(event.currentTarget);
943 }}
944 onStartShouldSetResponder={() => true}
945 />
@@ -970,7 +978,7 @@ describe('ReactFabric', () => {
978 changedTouches: [],
979 });
980
973 - expect.assertions(4);
981 + expect.assertions(6);
982 });
983
984 it('findHostInstance_DEPRECATED should warn if used to find a host component inside StrictMode', () => {
packages/react-native-renderer/src/__tests__/ReactNativeEvents-test.internal.js
+10 -2
@@ -484,6 +484,9 @@ it('dispatches event with target as reactTag', () => {
484 expect(ReactNative.findNodeHandle(ref1.current)).toEqual(
485 event.target,
486 );
487 + expect(ReactNative.findNodeHandle(ref1.current)).toEqual(
488 + event.currentTarget,
489 + );
490 }}
491 onStartShouldSetResponder={() => true}
492 />
@@ -495,6 +498,9 @@ it('dispatches event with target as reactTag', () => {
498 expect(ReactNative.findNodeHandle(ref2.current)).toEqual(
499 event.target,
500 );
501 + expect(ReactNative.findNodeHandle(ref2.current)).toEqual(
502 + event.currentTarget,
503 + );
504 }}
505 onStartShouldSetResponder={() => true}
506 />
@@ -526,7 +532,7 @@ it('dispatches event with target as reactTag', () => {
532 [0],
533 );
534
529 - expect.assertions(4);
535 + expect.assertions(6);
536 });
537
538 it('dispatches event with target as instance', () => {
@@ -553,6 +559,7 @@ it('dispatches event with target as instance', () => {
559 expect(ref1.current).not.toBeNull();
560 // Check for referential equality
561 expect(ref1.current).toBe(event.target);
562 + expect(ref1.current).toBe(event.currentTarget);
563 }}
564 onStartShouldSetResponder={() => true}
565 />
@@ -563,6 +570,7 @@ it('dispatches event with target as instance', () => {
570 expect(ref2.current).not.toBeNull();
571 // Check for referential equality
572 expect(ref2.current).toBe(event.target);
573 + expect(ref2.current).toBe(event.currentTarget);
574 }}
575 onStartShouldSetResponder={() => true}
576 />
@@ -594,5 +602,5 @@ it('dispatches event with target as instance', () => {
602 [0],
603 );
604
597 - expect.assertions(4);
605 + expect.assertions(6);
606 });