Skip to content
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

React Native showSoftInputOnFocus is not working properly. #34540

Open
sumanbhattarai opened this issue Aug 31, 2022 · 5 comments
Open

React Native showSoftInputOnFocus is not working properly. #34540

sumanbhattarai opened this issue Aug 31, 2022 · 5 comments
Labels
Needs: Triage 🔍 Never gets stale Prevent those issues and PRs from getting stale

Comments

@sumanbhattarai
Copy link

Description

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:

When the second input field is focused and the keyboard is open, how to make the first TextInput get focus immediately when I press it and show the calculator, not the keyboard?

Version

0.68.2

Output of npx react-native info

System:
OS: macOS 12.2.1
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Memory: 181.72 MB / 8.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node
Yarn: 1.22.18 - ~/.yarn/bin/yarn
npm: 6.14.16 - ~/.nvm/versions/node/v14.19.0/bin/npm
Watchman: 2021.06.07.00 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.11.2 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 21.0.1, iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0
Android SDK:
API Levels: 29, 30, 31
Build Tools: 28.0.3, 29.0.2, 30.0.2, 31.0.0
System Images: android-30 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 2021.2 AI-212.5712.43.2112.8512546
Xcode: 13.1/13A1030d - /usr/bin/xcodebuild
Languages:
Java: 11.0.15 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 17.0.2 => 17.0.2
react-native: 0.68.2 => 0.68.2
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Steps to reproduce

  • Create two input fields within a view wrapped inside a scroll view.
  • Add showSoftInputOnFocus={false} to the first input only.
  • Run the application.
  • Click on the first input. It doesn't open the keyboard.
  • Click on the second input. It will make it focused and opens the keyboard.
  • Now when you are on the second input and the keyboard is open, click on the first input. It doesn't dismiss the keyboard. The keyboard still exists for the first input having showSoftInputOnFocus props as false.
  • Now, to solve this, add onBlur={Keyboard.dismiss} on second input.
  • Now when you are on the second input and the keyboard is open, click on the first input. It hides the keyboard but the first input field is not focused, instead, you've to tap the first input again to get focused.

Snack, code example, screenshot, or link to a repository

import React, {useState} from 'react';
import { Text, View, TextInput, ScrollView, SafeAreaView } from 'react-native';

export default function App() {
  const [showCalculator, setShowCalculator] = useState(false);
  return (
    <ScrollView style={{flex:1 , marginTop: 60}}>
  <View>
    <TextInput
      placeholder="Enter amount:"
      showSoftInputOnFocus={false}
      onFocus={()=>setShowCalculator(true)}
      onBlur={()=>setShowCalculator(false)}
    />
    <TextInput
      placeholder="Enter description:"
    />
  </View>
</ScrollView>
  );
}



@jjenzz
Copy link

jjenzz commented Sep 22, 2022

i am also experiencing this, on android only tho (if that helps). currently solving this by doing the following (pseudo-code):

const Input = (props) => {
  const [isKeyboardOpen, setKeyboardOpen] = React.useState(false);
  const ref = React.useRef();

  React.useEffect(() => {
    const showListener = ReactNative.Keyboard.addListener('keyboardDidShow', () = setKeyboardOpen(true));
    const hideListener = ReactNative.Keyboard.addListener('keyboardDidHide', () = setKeyboardOpen(false));
    return () => {
      showListener.remove();
      hideListener.remove();
    };
  }, []);
  
  return (
    <ReactNative.Input 
      {...props} 
      ref={ref}
      onFocus={(event) => {
        const isAndroid = ReactNative.Plaftorm.OS === 'android';
        if (isAndroid && !props.showSoftInputOnFocus && isKeyboardOpen) {
          ReactNative.Keyboard.dismiss();
          ref.current.focus();
        } else {
          props.onFocus?.(event);
        }
      }} 
    />
  );
}

then use this custom Input instead of the react native one throughout your app.

@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 27, 2023
@jjenzz
Copy link

jjenzz commented Mar 28, 2023

this is still a reproducible issue.

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Mar 28, 2023
@github-actions
Copy link

github-actions bot commented Oct 1, 2023

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 1, 2023
@bryanltobing
Copy link

this is still a reproducible issue.

@cortinico cortinico added Never gets stale Prevent those issues and PRs from getting stale and removed Stale There has been a lack of activity on this issue and it may be closed soon. labels Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Triage 🔍 Never gets stale Prevent those issues and PRs from getting stale
Projects
None yet
Development

No branches or pull requests

4 participants