-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement TextInput autoFocus natively on iOS #27803
Conversation
cc @TheSavior |
This needs integration with TextInputState, that is what lets dismissKeyboard and ScrollResponder properly blur the currently focused element. All focus and blur work currently happens in JS, which isn’t ideal, but it is at least consistent, so it makes me a little nervous to split it up and have one piece implemented natively. |
@TheSavior Oh I forgot to include it in the test plan but I tested this and onFocus gets called so TextInputState gets updated properly. |
Maybe I’m reading the code wrong, but it looks like you early return on iOS, so focus only gets called for autoFocus on Android. What is the code path that is keeping it calling into TextInputState? |
The JS autoFocus code path doesn't actually call into
The focus management is already set up in a way that no matter how focus is triggered (could be tapping the input or programmatically) |
Ah, onFocus, right |
@TheSavior Anything I can do to help moving this forward? |
Ah whoops, I forgot to look at this when I got into the office the next day. Thanks for the ping. Can you split this into two different PRs? One with the native change and one with the JS change? I'll need to land the native change to support this behavior first and separately than the JS change that depends on it. |
022e79c
to
f2ed867
Compare
This should work. Actually calling focus from JS if the Input is already focused from native will just result in a noop. We can land the JS change separately. |
Also, have you investigated implementing this natively on Android as well to make the implementation consistent? |
I haven't tried but it should be possible. I can test focusing the input in onAttachedToWindow which would be similar to what this does on iOS. |
I'm going to run this by the people working on TextInput for Fabric as they will need to implement this behavior for Fabric as well now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TheSavior is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Yay that was easy! #27924 |
Summary: Follow up to #27803. To keep consistency between the implementations this also implements autoFocus natively on Android. This will allow removing the JS auto focus logic completely. ## Changelog [Android] [Fixed] - Implement native TextInput autoFocus on Android Pull Request resolved: #27924 Test Plan: Test using the TextInput example in RN tester. Differential Revision: D19674506 Pulled By: TheSavior fbshipit-source-id: 9cbb517fc69bccd11f5292a1ccb4acea2e3713e9
This pull request was successfully merged by @janicduplessis in 6adba40. When will my fix make it into a release? | Upcoming Releases |
Summary: This implement the autoFocus functionality natively instead of calling the focus method from JS on mount. This is needed to properly fix the issue described in #27217, where when using native navigation (UINavigationController) text input focus needs to happen in the same frame transition starts or it leads to an animation bug in UIKit. My previous attempt fixed the problem only partially and the bug could still happen since there is no guaranty code executed in useEffect will end up in the same frame as the native view being created and attached. To fix this I added an autoFocus prop to the native component on iOS and in didAttachToWindow we focus the input if it is set. This makes sure the focus is set in the same frame as the view hierarchy containing the input is created. ## Changelog [iOS] [Fixed] - Add native support for TextInput autoFocus on iOS Pull Request resolved: #27803 Test Plan: - Tested that the UI glitch when transitionning to a screen with an input with autofocus no longer happens in my app. - Tested that autofocus still works in RNTester - Made sure that onFocus does get called and TextInputState is updated properly Differential Revision: D19673369 Pulled By: TheSavior fbshipit-source-id: 14d8486ac635901622ca667c0e61c75fb446e493
Summary: Follow up to #27803 and #27924. We no longer need to call focus on mount from JS as both iOS and Android implements it natively now. ## Changelog [General] [Fixed] - Remove JS autoFocus implementation Pull Request resolved: #27923 Test Plan: Test that focus works in RN Tester with this, #27803 and #27924 Differential Revision: D19956373 Pulled By: TheSavior fbshipit-source-id: 5d99ead55011663b3edaf499ac7616765a24cb50
Summary: This implement the autoFocus functionality natively instead of calling the focus method from JS on mount. This is needed to properly fix the issue described in facebook#27217, where when using native navigation (UINavigationController) text input focus needs to happen in the same frame transition starts or it leads to an animation bug in UIKit. My previous attempt fixed the problem only partially and the bug could still happen since there is no guaranty code executed in useEffect will end up in the same frame as the native view being created and attached. To fix this I added an autoFocus prop to the native component on iOS and in didAttachToWindow we focus the input if it is set. This makes sure the focus is set in the same frame as the view hierarchy containing the input is created. [iOS] [Fixed] - Add native support for TextInput autoFocus on iOS Pull Request resolved: facebook#27803 Test Plan: - Tested that the UI glitch when transitionning to a screen with an input with autofocus no longer happens in my app. - Tested that autofocus still works in RNTester - Made sure that onFocus does get called and TextInputState is updated properly Differential Revision: D19673369 Pulled By: TheSavior fbshipit-source-id: 14d8486ac635901622ca667c0e61c75fb446e493
Summary: Follow up to facebook#27803. To keep consistency between the implementations this also implements autoFocus natively on Android. This will allow removing the JS auto focus logic completely. ## Changelog [Android] [Fixed] - Implement native TextInput autoFocus on Android Pull Request resolved: facebook#27924 Test Plan: Test using the TextInput example in RN tester. Differential Revision: D19674506 Pulled By: TheSavior fbshipit-source-id: 9cbb517fc69bccd11f5292a1ccb4acea2e3713e9
Summary: This implement the autoFocus functionality natively instead of calling the focus method from JS on mount. This is needed to properly fix the issue described in facebook#27217, where when using native navigation (UINavigationController) text input focus needs to happen in the same frame transition starts or it leads to an animation bug in UIKit. My previous attempt fixed the problem only partially and the bug could still happen since there is no guaranty code executed in useEffect will end up in the same frame as the native view being created and attached. To fix this I added an autoFocus prop to the native component on iOS and in didAttachToWindow we focus the input if it is set. This makes sure the focus is set in the same frame as the view hierarchy containing the input is created. ## Changelog [iOS] [Fixed] - Add native support for TextInput autoFocus on iOS Pull Request resolved: facebook#27803 Test Plan: - Tested that the UI glitch when transitionning to a screen with an input with autofocus no longer happens in my app. - Tested that autofocus still works in RNTester - Made sure that onFocus does get called and TextInputState is updated properly Differential Revision: D19673369 Pulled By: TheSavior fbshipit-source-id: 14d8486ac635901622ca667c0e61c75fb446e493
Summary: Follow up to facebook#27803 and facebook#27924. We no longer need to call focus on mount from JS as both iOS and Android implements it natively now. ## Changelog [General] [Fixed] - Remove JS autoFocus implementation Pull Request resolved: facebook#27923 Test Plan: Test that focus works in RN Tester with this, facebook#27803 and facebook#27924 Differential Revision: D19956373 Pulled By: TheSavior fbshipit-source-id: 5d99ead55011663b3edaf499ac7616765a24cb50
Summary
This implement the autoFocus functionality natively instead of calling the focus method from JS on mount. This is needed to properly fix the issue described in #27217, where when using native navigation (UINavigationController) text input focus needs to happen in the same frame transition starts or it leads to an animation bug in UIKit.
My previous attempt fixed the problem only partially and the bug could still happen since there is no guaranty code executed in useEffect will end up in the same frame as the native view being created and attached.
To fix this I added an autoFocus prop to the native component on iOS and in didAttachToWindow we focus the input if it is set. This makes sure the focus is set in the same frame as the view hierarchy containing the input is created.
Changelog
[iOS] [Fixed] - Add native support for TextInput autoFocus on iOS
Test Plan