Skip to content

TextInput onChangeText callback was not invoked on Android #23663

@tianjyan

Description

@tianjyan

🐛 Bug Report

When I set TextInput's padding is 0 and keyboardType is numeric on Android, onChangeText was not invoked when I was typing numbers.

To Reproduce

See "Code Example" section.

Expected Behavior

onChangeText should be invoked when I am typing numbers.

Code Example

You can also get the whole project at https://github.com/tianjyan/textinput_issue .

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, TouchableOpacity, TextInput} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};

type State = {
  codes: Array<string>,
};

export default class App extends Component<Props, State> {
  textInput: Object;

  bindRef = (ref: Object) => { this.textInput = ref; };

  constructor(props: Props) {
    super(props);
    this.state = {
      codes: ['', '', '', '', '', ''],
    };
    this._onChangeText = this._onChangeText.bind(this);
  }

  render() {
    return (
      <View style={styles.container}>
        <TouchableOpacity
          activeOpacity={1}
          onPress={() => {
            this.textInput.focus();
          }}
        >
          <TextInput
            ref={this.bindRef}
            autoFocus
            maxLength={6}
            keyboardType="numeric"
            onChangeText={this._onChangeText}
            style={styles.textInput}
          />
          {this._renderItemsView()}
        </TouchableOpacity>
      </View>
    );
  }

  _onChangeText: (text: string) => void;

  _onChangeText(text: string) {
    const codes = text.split('');
    for (let i = 0; i < 6; i++) {
      if (i >= text.length) {
        codes[i] = '';
      }
    }
    this.setState({
      codes,
    });
  }

  _renderItemsView() {
    return (
      <View style={styles.itemsContainer}>
        {
          this.state.codes.map((code, index) => (
            <View
              key={index}
              style={styles.item}
            >
              <Text style={styles.text}>
                {code}
              </Text>
            </View>
          ))
        }
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'stretch',
    backgroundColor: '#F5FCFF',
  },
  textInput: {
    width: 0,
    height: 0,
    // If you change the padding to 1, the onChangeText will be invoked if you type numbers.
    padding: 0,
    backgroundColor: 'transparent',
  },
  itemsContainer: {
    marginTop: 52,
    marginEnd: 24,
    marginStart: 24,
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  item: {
    width: 32,
    height: 42,
    borderBottomWidth: 1,
    borderBottomColor: 'grey',
    alignItems: 'center',
    justifyContent: 'center',
  },
  text: {
    color: 'black',
    fontWeight: 'bold',
    fontSize: 32,
    textAlign: 'center',
  },
});

Environment

React Native Environment Info:
System:
    OS: macOS 10.14.3
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 111.20 MB / 16.00 GB
    Shell: 5.3 - /bin/zsh
Binaries:
    Node: 8.11.3 - /usr/local/opt/node@8/bin/node
    Yarn: 1.7.0 - /usr/local/bin/yarn
    npm: 5.6.0 - /usr/local/opt/node@8/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
    iOS SDK:
    Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
    Android SDK:
    API Levels: 18, 23, 25, 26, 27, 28
    Build Tools: 23.0.1, 23.0.2, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.1, 28.0.2, 28.0.3
    System Images: android-27 | Google Play Intel x86 Atom
IDEs:
    Android Studio: 3.2 AI-181.5540.7.32.5056338
    Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
    react: 16.6.3 => 16.6.3 
    react-native: 0.58.5 => 0.58.5 
npmGlobalPackages:
    create-react-native-app: 1.0.0
    react-native-cli: 2.0.1
    react-native-create-library: 3.1.2
    react-native-log-ios: 1.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugComponent: TextInputRelated to the TextInput component.Platform: AndroidAndroid applications.StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions