Skip to content

Commit e494e4b

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Fix onPress event for nested Text in RN Android
Summary: This bug is caused by RNAndroid dispatching an incorrect sequence of events to JS when the user taps on a Text. Taking into consideration the example P462662009, when the user taps of the "Inner" text, RN Android is dispatching three events: topTouchStart, topTouchStart and topTouchEnd. The information stored on the first two JS events is correct, but the problem is that it is duplicated. This sequence of events makes Pressability to dispatch the event to the incorrect target. This was originally introduced in D3035589 (39fdce2) (2016) In this diff I'm changing the way RN Android bubbles events when the user taps on a ReactTextView. From now on, events won't be bubbled anymore, and they will be handled by the ReactRootView.onInterceptTouchEvent: https://fburl.com/code/rbt8$ Additionally, I'm creating a FeatureFlag in case this change has a unknown side effect that's only detected in production. I will create a MC for FB4A in the next diffs of the stack changelog: [Fixed][Android] Fix onPress event for nested Text in RN Android Reviewed By: javache Differential Revision: D31628461 fbshipit-source-id: 177397d4369191a3c97e2f86e801757b27ee5121
1 parent d5689b9 commit e494e4b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java

+3
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,7 @@ public static boolean isMapBufferSerializationEnabled() {
105105
public static boolean useDispatchUniqueForCoalescableEvents = false;
106106

107107
public static boolean useUpdatedTouchPreprocessing = false;
108+
109+
/** TODO: T103427072 Delete ReactFeatureFlags.enableNestedTextOnPressEventFix */
110+
public static boolean enableNestedTextOnPressEventFix = true;
108111
}

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.text.method.LinkMovementMethod;
2020
import android.text.util.Linkify;
2121
import android.view.Gravity;
22+
import android.view.MotionEvent;
2223
import android.view.View;
2324
import android.view.ViewGroup;
2425
import androidx.annotation.Nullable;
@@ -31,6 +32,7 @@
3132
import com.facebook.react.bridge.WritableArray;
3233
import com.facebook.react.bridge.WritableMap;
3334
import com.facebook.react.common.ReactConstants;
35+
import com.facebook.react.config.ReactFeatureFlags;
3436
import com.facebook.react.uimanager.PixelUtil;
3537
import com.facebook.react.uimanager.ReactCompoundView;
3638
import com.facebook.react.uimanager.UIManagerModule;
@@ -382,6 +384,16 @@ public int reactTagForTouch(float touchX, float touchY) {
382384
return target;
383385
}
384386

387+
@Override
388+
public boolean onTouchEvent(MotionEvent ev) {
389+
// The root view always assumes any view that was tapped wants the touch
390+
// and sends the event to JS as such.
391+
// We don't need to do bubbling in native (it's already happening in JS).
392+
// For an explanation of bubbling and capturing, see
393+
// http://javascript.info/tutorial/bubbling-and-capturing#capturing
394+
return ReactFeatureFlags.enableNestedTextOnPressEventFix;
395+
}
396+
385397
@Override
386398
protected boolean verifyDrawable(Drawable drawable) {
387399
if (mContainsImages && getText() instanceof Spanned) {

0 commit comments

Comments
 (0)