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

Duplicated letters when autoCapitalize="characters" on android

Open joncrocks opened this issue 8 years ago • 106 comments

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 :-)

joncrocks avatar Nov 22 '16 14:11 joncrocks

Are there any updates on these issue already? , I am also experiencing this.

kenmasters avatar Jan 13 '17 03:01 kenmasters

having same issue with lowercasing, every time a capital case character is entered, the text that is passed to onChangeText function contains exact duplicate of existing text plus last entered character. it happens only on android devices, on ios it works fine

magneticz avatar Feb 03 '17 09:02 magneticz

Experiencing same with lowercase.

RN: 0.39.2 Phone: samsung galaxy S7

rob-64 avatar Feb 04 '17 00:02 rob-64

Any news on this issue?

klaasman avatar Mar 03 '17 10:03 klaasman

I have exactly the same problem and cannot release without a fix...

RN: 0.42.3 Phone: Galaxy s6 edge

Is anybody working on this? Thanks.

afilp avatar Mar 18 '17 13:03 afilp

Same issue.

RN: 0.44.3 Phone: Galaxy S8

Cannot reproduce this in emulator, so maybe something Samsung specific?

MSchmidt avatar Jun 20 '17 09:06 MSchmidt

Since the rnplay link is broken, I have recreated it on Expo Snack here:

https://snack.expo.io/S1ksBZsEW

Samsung Galaxy S8 is breaking for me.

skabbes avatar Jul 06 '17 01:07 skabbes

When I disable the "Predictive Text" at "Language and Input > Samsung keyboard settings" the issue is solved, but how to solve that programmatically? :/

andreiAndrade avatar Jul 21 '17 18:07 andreiAndrade

Same issue

RN: 0.44.0 Phone: Samsung Galaxy S7 Edge

Any fix?

JumalDB avatar Aug 14 '17 09:08 JumalDB

Same issue. Phone: Samsung Galaxy S7 / Moto Z2 Play Keyboards: Samsung Keyboard (with predict), Swiftkey Keyboard and Google Keyboard RN: [email protected]

sonikro avatar Aug 16 '17 13:08 sonikro

Same issue here. Phone: OnePlus 5 Keyboard: [email protected] RN: 0.46.2

renatoagds avatar Aug 17 '17 21:08 renatoagds

Same issue here. Android 7.0 emulator, google pixel hardware selected. RN: 0.46.2

ollyde avatar Aug 29 '17 07:08 ollyde

same for Samsung Galaxy @facebook ?

vko-online avatar Sep 07 '17 12:09 vko-online

What's the work around? Just don't use autocapitalize and your own?

danparker276 avatar Sep 08 '17 17:09 danparker276

Experienced same issue. I thought that in Android onChangeText() is triggered multiple times or so. But after checking, it's not what I thought it would be. Totally no hint of solving the issues at this moment.

Pianist038801 avatar Sep 11 '17 19:09 Pianist038801

Same issue here. It seems to be on any android devices with swipe / SwiftKey keyboards turned on. I think this issue should be escalated as these keyboards come pre-installed on a lot of devices.

conor909 avatar Sep 27 '17 09:09 conor909

Same issue, I was getting paranoid cause I couldn't find the reason For me it doesn't even matter if you set autoCapitalize or not

Do anyone know a workaround?

"react-native": "^0.46.1", Xiaomi Redmi Note 4 Google Keyboard

williamoliveira avatar Oct 03 '17 09:10 williamoliveira

I stumbled upon the same issue. Found out the autoCapitalize was not working due to fontFamily specified in style prop.

Seems like it was failing silently. Will update here, if I find anything else.

ankitpopli1891 avatar Oct 03 '17 12:10 ankitpopli1891

Try to set autoCorrect to false and check if problem exist yet.

avynarchuk avatar Oct 13 '17 12:10 avynarchuk

It does. autoCorrect has nothing to do with it.

MSchmidt avatar Oct 13 '17 13:10 MSchmidt

I have the same issue but found a workaround for my current needs.

The issue can't be re-produce via the simulator using your keyboard. However, if you use the onscreen keyboard, then you can see the text is duplicated.

For my case, since the user can't use a 'keyboard' except for the on-screen keyboard provided. In adding, it only became an issue when I use the text.toUpperCase() Thus, setting the text state in lower case and autoCapitalize="characters" seems to work fine for my case. Of course, if you do not want the user to ever be able to enter lowercase, then this is an issue.

PS: I specified a fontFamily in style and autoCapitalize still work as expected

M1chaelTran avatar Oct 27 '17 05:10 M1chaelTran

I am facing the same issue..

john1jan avatar Nov 06 '17 11:11 john1jan

Any update on this issue . I just tried to use toUpperCase on my textinput. Its dupllicates the characters.

john1jan avatar Nov 06 '17 11:11 john1jan

ezgif com-optimize

john1jan avatar Nov 06 '17 11:11 john1jan

Has anyone found a solution for this bug?, I have the same problem in all samsung that have the predictive language activated

tastafur avatar Dec 12 '17 10:12 tastafur

the problem is toUpperCase() , i use <TextInput autoCapitalize='characters' ...

MedinaGitHub avatar Feb 07 '18 15:02 MedinaGitHub

@MedinaGitHub of course the problem is the use of toUpperCase(). We use this to enforce uppercase and this causes this bug on Samsung devices.

MSchmidt avatar Feb 08 '18 09:02 MSchmidt

Not only Samsung

williamoliveira avatar Feb 08 '18 13:02 williamoliveira

Not only Samsung devices and I have the same behaviour with toLowerCase()

BrunoJAzevedo avatar Feb 08 '18 14:02 BrunoJAzevedo

try this: https://github.com/nikolaiwarner/react-native-text-input-reset

kasterlod avatar Feb 08 '18 17:02 kasterlod