question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicated letters when autoCapitalize="characters" on android

See original GitHub issue

Description

When trying to implement an upper case only input, adding autoCapitalize="characters" seems to work on Android by setting the keyboard to be upper case by default. One can hit the shift key and then type a lower case letter.

To ensure that we only let the user enter (and see) upper case letters, I thought that I might be able to handle it by ensuring that when we update the react state of the component we capture the text in upper case.

By using a toUpperCase on top of what is a pretty standard state update cycle (i.e. a very similar update method ala the examples at https://facebook.github.io/react-native/releases/next/docs/textinput.html ), this saves the input into the state, uppercased, ready for the next render cycle. (I’m not concerned about the dangers of toUpperCase at this point.)

Unfortunately, the behaviour is a bit strange when you start typing both upper and lowercase letters, where you start getting repeated letters, e.g. if I type AbC, I will end up with ABABC, AbcdE I get ABABCDABABCDE.

Reproduction

I created an example app here: https://rnplay.org/apps/t-gBOA Note that the behaviour seems fine on the iOS simulator, but is ‘wrong’ on the android simulator.

or see the component below:

import React, {Component} from "react";
import {View, TextInput} from "react-native";

export class UpperCaseTextEntry extends Component {
    
    constructor() {
        super();
        this.state = {
            text: ""
        }
    }
    
    upperCaseIt(text) {
        var textUpperCase = text.toUpperCase();

        this.setState({text: textUpperCase});
    }
    
    render() {
        var text = this.state.text;
        return (
            <View>
                <TextInput value={text} autoCapitalize="characters"
                           onChangeText={(text) => {this.upperCaseIt(text)}}
                />
            </View>
        )
        
    }
}

Solution

I suspect that there’s something going awry with the syncing of state between the react state and the state of the underlying components, quite possibly some case-ignoring checks are being in some places, but not others.

Additional Information

  • React Native version: 0.35.0
  • Platform: Android
  • Operating System: Windows (building/testing on windows)

I’ve also noted during that it’s more than possible to fire multiple onChangeTexts before a render is triggered, which could lead to unexpected app behaviour. This could be expected unexpected behaviour though 😃

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:123
  • Comments:104 (2 by maintainers)

github_iconTop GitHub Comments

56reactions
john1jancommented, Nov 6, 2017

ezgif com-optimize

22reactions
RogerPariscommented, Feb 14, 2020
secureTextEntry={Platform.OS === 'ios' ? false : true}
keyboardType={Platform.OS === 'ios' ? null : 'visible-password'}
autoCapitalize="characters"

This kinda makes the trick for both platforms

Read more comments on GitHub >

github_iconTop Results From Across the Web

toLowerCase on TextInput value is creating duplicate text if ...
On my android physical device, if you force a capital letter ( autocapitalize is set to none), and then quickly tap other letters, ......
Read more >
Duplicated letters when autoCapitalize="characters" on ...
When trying to implement an upper case only input, adding autoCapitalize="characters" seems to work on Android by setting the keyboard to be ...
Read more >
InputFilter.AllCaps
This filter will capitalize all the lowercase and titlecase letters that are added through edits. (Note that if there are no lowercase or...
Read more >
How To Turn Off Or On Auto-capitalization And ... - YouTube
How To Turn Off Or On Auto-capitalization And Auto-correction On Your Android Devices GboardIn this video, I will show you how to turn...
Read more >
Autocapitalization
Control how virtual keyboards capitalize words and characters by default. ... keyboard on Android showing lowercase letters with autocapitalize set to none.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found