How to activate text input on press of a button in react native? - react-native

I'm creating a basic note pad app. In the app, the user can press a button to "add a note". When pressed, a modal is rendered displaying a text input for the user to type their note.
The issue I'm having is once the modal has opened, the user has to manually tap the text input to pull up the keyboard and type their note. Instead I want the text input to be activated automatically the moment the modal is displayed on press of the button. So in other words, allow the user to start typing their note, once the modal opens up.
So I assume I have to some how activate a text input on press of a button? Is this possible?
Thanks.

Use the autoFocus prop.
<TextInput autoFocus={true}
Note that the input will focus when the component is mounted, so you may want to render the modal conditionally.

Related

React Native showSoftInputOnFocus not working properly

I've two input fields in my application.
<ScrollView>
<View>
<TextInput
placeholder="Enter amount:"
value={firstInput}
showSoftInputOnFocus={false}
onFocus={()=>setShowCalculator(true)}
onBlur={()=>setShowCalculator(false)}
/>
<TextInput
placeholder="Enter description:"
value={secondInput}
/>
</View>
</ScrollView>
When we click the first input is clicked, the keyboard is not shown instead calculator is shown. And when we click the second input, it opens the default keyboard. It works completely fine when I click one input, unfocus it by clicking outside the input and then click another input. But the issue arises when I click the second input and without unfocusing it, I click on the first input.
Issue:
When the second TextInput is focused, the keyboard is displayed. Then when I press the first input (when the second input is focused and the keyboard is open), it focuses the first input but the keyboard is not dismissed.
To solve this, i added onBlur={()=>Keyboard.dismiss()} to the second input. After adding this prop, when I switch the cursor from the second TextInput to the first, the keyboard is hidden this time, but the first TextInput is not focused. I must press the first TextInput again to make it focused.
Question:
How to make the first TextInput get focus immediately when I press it and show the calculator, not the keyboard?
Example
https://snack.expo.dev/#ersumanbhattarai/93b02d?platform=android

React native text input onblur event gets called on its own, preventing keyboard input to be processed

trying to build an OTP input screen. From what I read, it can be achieved by hiding the TextInput component and displaying the inputted text in views. I have shared a basic Expo snack link. The text input is not taking the values I press on the keyboard. here is the expo link - https://snack.expo.dev/oD72PI5Xch. Can someone help me with what I am doing wrong?
Another issue I have noticed is that the keyboard doesn't show up if I press the back button and then again press the <Text> component from where I have called the onPress to call focus() on the TextInput. It will come up if I press the back button and then press the Text component only once. If I again close it using the back button, after that the keyboard stops coming up.
Below is the console log output based on the logging statements I have in the code snack (my actions are marked with >>>>>) -
1>>>>> App opens up
text input onfocus called
text input onblur called
keyboard did show
2>>>>> App stabilizes
3>>>>> I press the back button
keyboard did hide
4>>>>> I press the text component
calling focus
keyboard did show
5>>>>> I press the back button
keyboard did hide
6>>>>> I keep pressing the text component
calling focus
calling focus
calling focus
calling focus
calling focus
calling focus
I think the issue might be before the 2>>>>> part. The TextInput's onBlur is already called. That might be the reason why it is not taking keyboard inputs. Also, even after calling focus, the onFocus logging is absent, meaning that the text input never came into focus. What am I doing wrong? Thanks in advance :)
And if you solve this, please help me out with the issue of keyboard not appearing on pressing the Text component multiple times (6>>>>> part of the console output).
I don't clearly understand what are you trying to do by giving 0 height and width to TextInput, If you are trying to create an OTP input field you can simply use any third-party libraries like react-native-otp-input or try to use code from this existing snack

Press on textinput hides modal react-native-modal

I am rendering Flatlist which render list of items each item have button and modal inside it when the button pressed the modal shall open, each modal have some text inputs inside it
so the hierarchy is as the following :
Screen
-FlatList
--Item
---Button
---Modal
----TextInput
When I open press on the first item's button the modal opens and everything works great, but when I scroll down in the Flatlist and press on the last item and open the modal open then I try to write on the modal's textinput the modal disappears.
I am sure that the modal exists but I cannot see it, Any help?
GIF explaining what I mean
I suffered from the same issue and after digging into this subject, I do not think there is any other way than taking the modal out of FlatList.
Seems that there is a bug in Android with a modal with textinput inside a FlatList, which caused when the keyboard expands.
When modal is outside of the FlatList, works like a charm.

How to Bring button (which opens modal) on top of modal as well? React native

I am using react-native-modal. I am using modal's overlay, and the button which opens up modal, changes to cross button, but it stays behind the overlay of modal. I want that button to be on top of modal when modal is visible. I have already tried zIndex with the button but it didn't work. Need help with this. Thanks in advance.
Something like this but with modal overlay:
The Modal component of react native traps the focus inside the modal content while visible. The only way to capture a click on the button is to display another copy of it inside the Modal component. If you manage to display it at the same position, the user won't see that it's another button.

How can we provide default Back key press behaviour to a custom button tap event in Windows Phone 8?

I'm developing an Windows Phone 8 application. In a page i have placed one Button and one textbox. When user Taps on textbox, the default keyboard comes up. Now, keyboard is on display mode and i clicked on button which navigates me to another screen. After navigation it is giving me half the screen size to be in black and that is actually keyboard. So can anyone tell me how to hide the keyboard first and then navigate to another screen?
Is it possible to assign override OnBackKeyPress method to the button control? If not, what is the other way to get out of this problem?
All you have to do is changing the focus from textbox to page. Just add this code to button's tap event:
this.Focus();