react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Android] First TextInput focus automatically blurred

Open Lulunx opened this issue 3 years ago • 7 comments

Description

Hey, I've a little problem with the TextInput of React-Native library on Android. When I want to focus an input for the 1st time, he's automatically blurred. Then when I focus a second time an input, it's working well. I don't know if it's due to the navigation or the TextInput. I'm also a little bit disappointed because it's working well on IOS, so I really don't know where I've to search for resolve this problem.

React Native version:

$ react-native info
info Fetching system and libraries information...
System:
    OS: macOS 10.15.6
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 25.53 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.14.1 - /usr/local/bin/node
    Yarn: 1.22.0 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.0 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.7, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 28
      Build Tools: 28.0.3
      Android NDK: Not Found
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6308749
    Xcode: 11.7/11E801a - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_242 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.3 => 0.63.3 
    react-native-macos: Not Found
  npmGlobalPackages:

Steps To Reproduce

You can see here a video of this issue.

Expected Results

I expect the input to be focused directly when I first click on it, and not to be blurred.

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

It's actually hard for me to provide you some code example because it's for a professional use, I will do my possible to give access to the repo for some people if someone want to help me to fix this, otherwise here is the component I'm using for my inputs


import React from 'react';
import { StyleSheet, TextInput, View } from 'react-native';

import colors from 'Resources/Colors/colors';
import fonts from 'Resources/Fonts/fonts';

class MyInput extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            backgroundColor: colors.textInputTransparentBackground,
        };
    }

    onFocus() {
        console.log('I focus yea');
        this.setState({ backgroundColor: colors.focusedColor });
    }

    onBlur() {
        console.log('I blur noo');
        this.setState({ backgroundColor: colors.textInputTransparentBackground });
    }

    render() {
        const {
            width = '100%',
            mode = 'outlined',
            textAlign = 'left',
            autoFocus = false,
            keyboardType = 'default',
            name,
            placeholder,
            style = null,
            color = colors.primary,
            placeholderTextColor = '#aaaaaa',
            innerRef,
            testID = null,
            onChangeText,
            onSubmitEditing = function() {
                return;
            },
        } = this.props;
        const { backgroundColor } = this.state;
        let value = this.props.value;
        if (value !== undefined && value !== null) {
            value = value.toString();
        }
        return (
            <View style={{ ...styles.container, width }}>
                <TextInput
                    textAlign={textAlign}
                    autoFocus={autoFocus}
                    keyboardType={keyboardType}
                    placeholder={placeholder}
                    selectTextOnFocus={true}
                    value={value}
                    onFocus={() => this.onFocus()}
                    onBlur={() => this.onBlur()}
                    style={[
                        style,
                        styles.input,
                        mode === 'outlined' ? styles.outlined : styles.flat,
                        {
                            borderColor: color,
                            color: colors.black,
                            backgroundColor: backgroundColor,
                        },
                    ]}
                    placeholderTextColor={placeholderTextColor}
                    ref={innerRef}
                    testID={testID}
                    onChangeText={value => {
                        onChangeText(name, value);
                    }}
                    onSubmitEditing={() => onSubmitEditing()}
                />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        alignItems: 'center',
        flexDirection: 'row',
        justifyContent: 'space-between',
    },
    outlined: {
        borderWidth: 1,
        borderRadius: 7,
    },
    flat: {
        borderBottomWidth: 3,
        borderRadius: 5,
    },
    input: {
        padding: 5,
        height: 40,
        ...fonts.mediumBoldItalic,
        flex: 1,
        width: '100%',
    },
});

const Input = React.forwardRef((props, ref) => <MyInput {...props} innerRef={ref} />);

export default Input;

Thanks for the time you will take for helping me. Lucas

Lulunx avatar Oct 12 '20 11:10 Lulunx

Up ! Still not fixed :/

Lulunx avatar Oct 16 '20 08:10 Lulunx

seeing a similar issue where autofocus prop set to true isn't focused on load. currently using a workaround with ref hook and setTimeout

jaytxng avatar Nov 10 '20 01:11 jaytxng

same problem

mingxin-yang avatar Dec 04 '20 10:12 mingxin-yang

Was there any solution for this?

kunsachdeva avatar Jan 25 '21 17:01 kunsachdeva

I also had the same issue.

I confirmed that this problem does not exist when only React Native's textinput is used.

I expected there to be a problem with React Navigation and searched for it to solve this issue.

Perhaps you too will use this open-source, If you are a React Native developer.

Unfortunately, there is indeed no better navigation open source than this.

There is a comment in KeyboardManager.tsx like this:

      // If the interaction was super short we should make sure keyboard won't hide again.

      // Too fast input refocus will result only in keyboard flashing on screen and hiding right away.
      // During first ~100ms keyboard will be dismissed no matter what,
      // so we have to make sure it won't interrupt input refocus logic.
      // That's why when the interaction is shorter than 100ms we add delay so it won't hide once again.
      // Subtracting timestamps makes us sure the delay is executed only when needed.

So, I used setTimeout. (cry... 😭 )

Good Luck ~ bro.

#Android #react-navigation #keyboard #dismiss #blur

wayne-tt avatar Aug 23 '21 07:08 wayne-tt

Yeah, I needed to disable autoFocus and do this instead:

  useEffect(() => {
    setTimeout(() => textInputRef.current?.focus(), 100);
  }, []);

jzxchiang1 avatar Feb 19 '22 19:02 jzxchiang1

I'm facing this issue on android with these versions. However, my issue is every time I tap on the input keyboard automatically closes after opening for a second. "react": "17.0.1", "react-native": "0.64.2",

AliRehman7141 avatar Jul 04 '22 13:07 AliRehman7141

I'm facing this issue on android with these versions. However, my issue is every time I tap on the input keyboard automatically closes after opening for a second. "react": "17.0.1", "react-native": "0.64.2",

I had the same problem on RN 0.64. Fixed it by upgrading to 0.66.

vitexikora avatar Nov 15 '22 17:11 vitexikora

Same problem when using autoFocus. React v18.1, React native v0.70.6

g-popovic avatar Feb 27 '23 15:02 g-popovic

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[bot] avatar Aug 27 '23 05:08 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Sep 03 '23 05:09 github-actions[bot]