-
Notifications
You must be signed in to change notification settings - Fork 50
[Fix RN TextInput focus bug with react-navigation] Add a condition to blur the currently focused textinput #6
base: master
Are you sure you want to change the base?
[Fix RN TextInput focus bug with react-navigation] Add a condition to blur the currently focused textinput #6
Conversation
I create this snack with the code you added here and I did not see any errors with the keyboard. can you edit the code and replicate the error? |
You have to remove the "autoFocus" props on the textinput in Page2 (which is not supposed to be necessary since "this.textInput.focus()" is called in the componentDidMount). |
oh yeah I added there after it. but its also working without that prop being set. :S am I missing something? |
The problem is that the keyboard won't close when you click on the "dismiss keyboard" button. I tried in the snack and I see well the error :). |
aaah ok! I thought the problem was that the keyboard will be always visible after clicking the "dismiss keyboard" button, even if you go back to Page1, and that was not happening :) cool! now we have the example running :) thanks! |
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.
I think we need a solution that doesn't require calling into UIManager
prevTransitionProps.scene.route.params && | ||
prevTransitionProps.scene.route.params.nodeTag; | ||
if (previousSceneTag) { | ||
UIManager.viewIsDescendantOf( |
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.
this native call will be slow, and we shouldn't need to call into native APIs for this use case, because JS should already have awareness of what is focused
@@ -43,9 +43,26 @@ export default (Navigator, navigatorConfig) => | |||
// in the case where the index did not change, I believe. We | |||
// should revisit this after 2.0 release. | |||
if (transitionProps.index !== prevTransitionProps.index) { |
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.
This also looks like a real bug to me. Instead of comparing the index, we should see if the active key has changed. Something like this:
const lastActiveKey = prevTransitionProps.state.routes[prevTransitionProps.state.index].key;
const activeKey = transitionProps.state.routes[transitionProps.state.index].key;
if (lastActiveKey !== activeKey) {
...
Theoretically this could be an issue if a screen index changes but it stays active
Specifically, I think we need a context API that allows text inputs to negotiate focus with navigators. I welcome any proposals surrounding that, and will help with the implementation and code review. |
I noticed a bug while using react-navigation + the react-native TextInput component.
HOW TO REPRODUCE THE BUG :
Let’s consider the following app with only 2 pages :
Page1.js :
Page2.js :
App.js :
On Page1 you have a single button to navigate to Page2. On Page2, you’d like that a TextInput is focused when you arrive on this page. You also have a « dismiss » button to close the keyboard when the textinput is focused.
You could use the TextInput props « autoFocus » so that it is focused at the beginning. There is absolutely no bug in this case.
But instead, I tried to create a ref « textinput », and called this.textinput.focus() in the componentDidMount method to focus it at the beginning. It works, but when I press the dismiss button, the keyboard won’t close.
After digging, I found that a blur call is made when you arrive on Page2, coming from the @react-navigation/native/src/KeyboardAwareNavigator.js file. In _handleTransitionStart, transitionProps.index !== prevTransitionProps.index is true because you are switching pages, and !!currentField too in the « this.textinput.focus() » case. As a consequence, it blurs the textinput, the TextInput.State.currentlyFocusedField() becomes null, and you cannot dismiss the keyboard anymore.
MY FIX :
I tried to add another condition in addition to the if(currentField) one, to decide when we should blur the currently focused textinput. According to me, it should be something like if(currentField && previousPage includes currentField) or if(currentField && currentPage does not include currentField).
To do so, I used the viewIsDescendantOf method from UIManager, react-native (to check if the currently focused field is a child of the previous scene). In addition to the tag of the currently focused field (which is precisely currentField), I needed the tag of the previous scene.
In the PR react-navigation/core#19, I tried to add the node tag of a sceneview in the nav params.
In this one, I use the two tags to decide when we should blur the textinput.