Repository navigation
Let ref.focus() focus a View from a tap on Android - #60529
Open
dileepapeiris wants to merge 3 commits into
Open
dileepapeiris wants to merge 3 commits into
dileepapeiris wants to merge 3 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Fixes #60195.
With
enableImperativeFocuson,ref.focus()on aViewsends thefocusview command, andReactViewGroup.requestFocusFromJS()callsrequestFocus(FOCUS_DOWN, null). When the call comes from a tap'sonPressthe window is in touch mode, where Android'sView.requestFocus()refuses a view that is focusable but not focusable in touch mode. APressableorfocusableViewis exactly that (BaseViewManagerresetsfocusableInTouchModeto false), so the request is dropped and nothing holds focus. The sameonPressrun from Enter on a hardware keyboard works, because a key event leaves touch mode.focus()on aTextInputworks from a tap because anEditTextis focusable in touch mode.requestFocusFromJS()now marks the view focusable in touch mode for the duration of the request and restores the previous value afterwards, so the view's props keep their meaning and the next touch-mode transition behaves as before. A view that is not focusable is left alone, as before, sincesetFocusableInTouchMode(true)would also make it focusable.Changelog:
[ANDROID] [FIXED] -
ref.focus()on aViewnow focuses the view when called from a tapTest Plan:
Reproducer: https://github.com/dileepapeiris/react-native-view-focus-touch-mode-repro (Android 17 emulator, react-native 0.88.1 with
enableImperativeFocusforced on inMainApplication.kt).tools/run.shtaps the buttons withadb shell input tapand reads which view holds focus fromuiautomator dump.view targetfocusedtext input targettext input targetview targetfocusedview targetview targetAfter tapping "1. focus() the view", before on the left and after on the right:
Both flows (tap flow, then Tab, Enter and a tap on a fresh launch), before and after:
Recordings: before.mp4, after.mp4. Per-step screenshots and the focus log of each run are in the reproducer's
media/beforeandmedia/after.Unit tests:
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests 'com.facebook.react.views.view.ReactViewGroupTest'with two new cases, a focusable view attached to a window takes focus in touch mode and keepsisFocusableInTouchMode == false, and a non-focusable view stays non-focusable.