Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Set TextInput Value Uppercase

I'm new in React Native and not sure why it shows unexpected result. If I type 'a' then 'a' it shows 'AAA', and so on.

export default class App extends Component {
  constructor(props) {
    super(props)

    this.state = {
      userName: ''
    }
  }

  formatUserName = (textValue) => {
    // If I remove toUpperCase() below, it shows expected result.
    this.setState({ userName: textValue.toUpperCase() });
  }

  render() {
    ...
    <TextInput
      onChangeText={textValue => this.formatUserName(textValue)}
      value={this.state.userName} />
    ...
  }
}
like image 482
Jeaf Gilbert Avatar asked Jun 03 '20 16:06

Jeaf Gilbert


People also ask

How do you style TextInput in react native?

TextInput basics You can make your element look even better like so: const styles = StyleSheet. create({ input: { borderColor: "gray", width: "100%", borderWidth: 1, borderRadius: 10, padding: 10, }, }); In the piece of code above, we styled the text box's border and gave it some padding.

How do you automatically set text box to uppercase in react?

It can be done using React. Approach: Using event listener, we will change lower case values to uppercase. When user begins to type, an onChange event is created, we are updating the state of value with the upperCase value of the entered value using toUpperCase() function.

How do you increase TextInput height in react native?

We are going to use onContentSizeChange prop provided by TextInput to watch input box for content size changes and accordingly call our updateSize function which will update the height of our TextInput. In our render process we are going to use the height to create anew style to use with our TextInput.


1 Answers

If you want to change input string to uppercase string, then you can use autoCapitalize props in TextInput.

<TextInput
  autoCapitalize = {"characters"}
  onChangeText={(text) => this.setState({ userName: text })}
  value={this.state.userName} />

Props autoCapitalize has following options:

  • characters: all characters.
  • words: first letter of each word.
  • sentences: first letter of each sentence (default).
  • none: don't auto capitalize anything.

Default value is sentences

like image 146
Emre Kayaoglu Avatar answered Nov 09 '22 06:11

Emre Kayaoglu