From 132d1d00f885fe5a45d712fd7698db285c22bc4b Mon Sep 17 00:00:00 2001
From: fabriziobertoglio1987
Date: Tue, 28 Sep 2021 16:43:46 -0700
Subject: [PATCH] nested text onPress not working on last character (#30928)
Summary:
This issue fixes https://github.com/facebook/react-native/issues/22747 nested text does not allow you to press on the last character.
The method reactTagForTouch filters touches based on coordinates x and y. Nested Texts are converted into Spans on Android
https://github.com/facebook/react-native/blob/28fb41a0ab48cc01d606b64744c84e2ac3805f3f/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L111-L112
https://developer.android.com/guide/topics/text/spans
reactTagForTouch iterates over the span and triggers the onPress handler if the x,y coordinates correspond to one of the span characters.
## Changelog
[Android] [Fixed] - Nested Text Android onPress does not work with last character
Pull Request resolved: https://github.com/facebook/react-native/pull/30928
Test Plan:
This changes fix the Java API which can not be tested as explained in commit https://github.com/facebook/react-native/commit/709a441ecf54cd9465f5946af0454ee7d10d5cbe
The java TextTest was excluded from the test suite in commit https://github.com/facebook/react-native/commit/709a441ecf54cd9465f5946af0454ee7d10d5cbe as they need the Yoga libraries to run
**TEST - Clicking on the last letter triggers the callback
**
Clicking on the last letter does not invoke the onPress callback (in this case a console.warn)
| **BEFORE** |
|:-------------------------:|
| |
Clicking on the last letter does invoke the onPress callback (in this case a console.warn)
| **AFTER** |
|:-------------------------:|
| |
**TEST - Adding and removing Text
**
**TEST - Different type of languages
**
**TEST - Testing other Examples that use onPress handler
**
**TEST - Text with Inline Images
**
| Inline View | Inline Image is clipped |
|:-------------------------:|:-------------------------:|
| | |
Reviewed By: yungsters
Differential Revision: D31061832
Pulled By: charlesbdudley
fbshipit-source-id: 3034b4f35d4042bfcf1e899a59d5b2f73a990f31
---
.../main/java/com/facebook/react/views/text/ReactTextView.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java
index 64135eda4f5eca..a6d48f57d17aa2 100644
--- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java
+++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java
@@ -371,7 +371,7 @@ public int reactTagForTouch(float touchX, float touchY) {
for (int i = 0; i < spans.length; i++) {
int spanStart = spannedText.getSpanStart(spans[i]);
int spanEnd = spannedText.getSpanEnd(spans[i]);
- if (spanEnd > index && (spanEnd - spanStart) <= targetSpanTextLength) {
+ if (spanEnd >= index && (spanEnd - spanStart) <= targetSpanTextLength) {
target = spans[i].getReactTag();
targetSpanTextLength = (spanEnd - spanStart);
}