react-nativereact-native-textinput

Is it possible to dismiss the keyboard without making the focused component lose it?


I need to give focus to a TextInput without having the keyboard to open, because I might have a physical keyboard connected to the device in my use case scenario and I might want to keep virtual keyboard enabled on Android.

The solution I thought to was to give autoFocus to the TextInput and, in the onFocus callback, dismiss the keyboard.

What happens, though, is that the TextInput loses focus too.

Is there any viable known solution?

<TextInput
  onChangeText={text => setState(text)}
  value={state}
  autoFocus
  onFocus={() => {
    console.log('physicalKeyboard set?', isPhysicalKeyboard())
    if (isPhysicalKeyboard()) Keyboard.dismiss()
  }}
/>

Solution

  • As suggested in the accepted answer of this question, there is a new prop that can be set to achieve what I need, that is showSoftInputOnFocus.

    So the code becomes like this

    <TextInput
      onChangeText={text => setState(text)}
      value={state}
      autoFocus
      showSoftInputOnFocus={() => !isPhysicalKeyboard()}
    />