Duplicated letters when autoCapitalize="characters" on android
See original GitHub issueDescription
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:
- Created 7 years ago
- Reactions:123
- Comments:104 (2 by maintainers)
Top GitHub Comments
This kinda makes the trick for both platforms