Keyboards, Passwords and the Return Key

+15 Mana ✨

In HTML, the inputmode attribute picks which on-screen keyboard opens. A TextInput sets its keyboard with props too, but some of its props override others and some don't work together, so a field can end up ignoring a prop you set.

Key terms

  • keyboardType: the prop that chooses which on-screen keyboard opens, such as a number pad or an email keyboard.
  • inputMode: a prop that picks the keyboard by the kind of input, like the HTML attribute, and takes precedence over keyboardType.
  • submitBehavior: sets whether pressing return submits, submits and blurs the field, or inserts a newline.
  • returnKeyType: sets how the return key looks, such as a search or send key, without running any code (on iOS number pads, which have no return key, a value such as done also adds a submit button above the keys).
  • onSubmitEditing: the callback TextInput calls when the keyboard's submit button is pressed.

Picking the keyboard

Seven keyboardType values work on both iOS and Android: default, number-pad, decimal-pad, numeric, email-address, phone-pad and url. The rest are platform-specific. ascii-capable, ascii-capable-number-pad, numbers-and-punctuation, name-phone-pad, twitter and web-search work on iOS only, and visible-password works on Android only.

When inputMode is set, TextInput converts it to a keyboard type and ignores any keyboardType you pass.

inputModekeyboardType it becomes
textdefault
numericnumber-pad
decimaldecimal-pad
emailemail-address
telphone-pad
urlurl
searchweb-search on iOS, default on Android

inputMode="none" maps to default as well, but it also turns off showSoftInputOnFocus, so focusing the field opens no on-screen keyboard at all.

Hiding a password

secureTextEntry obscures the text as it is entered. It defaults to false, and it does not work with multiline={true}, so keep password fields on one line. Android has a known issue on top of that: secureTextEntry is not supported together with keyboardType="email-address" or keyboardType="phone-pad", so give the email address its own unmasked field.

A PIN field in one line

The line below is a complete PIN field.

tsx
<TextInput placeholder="PIN" keyboardType="number-pad" secureTextEntry maxLength={4} />

It opens a number pad, masks the digits and stops at four characters, and none of that needs a handler.

Multiline fields and submitBehavior

multiline={true} lets the text span several lines, and it defaults to false. On iOS a multiline field aligns its text to the top, and on Android it centers the text. Setting textAlignVertical: 'top' in the style gives both platforms top alignment.

What return does depends on whether the field is multiline. submitBehavior takes one of these strings:

  • 'blurAndSubmit' sends the submit event and blurs the field. It is the default for single-line fields.
  • 'newline' inserts a line break. It is the default for multiline fields. On a single-line field, TextInput replaces it with 'blurAndSubmit', because a single-line field cannot insert a newline.
  • 'submit' sends the submit event and keeps the field focused, in single-line and multiline fields alike.

A chat field that sends on return and stays ready for the next message therefore pairs multiline with 'submit'. You may still meet blurOnSubmit in existing code. It is deprecated, and submitBehavior takes its place and overrides whatever blurOnSubmit sets.

What the return key shows and what it runs

The cross-platform values of returnKeyType are done, go, next, search and send. A related prop, enterKeyHint, sets the same key label and takes precedence over returnKeyType when both are present. Neither of them runs any code. returnKeyType="search" changes the key, and the search itself belongs in onSubmitEditing.

On iOS, returnKeyType does one more thing on keyboards that have no return key: number-pad, decimal-pad, numeric, ascii-capable-number-pad and phone-pad. When such a field has a returnKeyType like done or go, React Native adds a toolbar above the pad with a button labeled for that key. Tapping the button runs the same logic as return, so onSubmitEditing fires and, with the single-line default, the field blurs. inputAccessoryViewButtonLabel sets the button's text and adds the toolbar too. With neither prop, nothing on the pad submits the field, so a phone number field that should submit from the keyboard needs a returnKeyType.

SignInForm and MessageComposer

SignInForm, the first of the two code examples, opens the email keyboard with inputMode="email" and masks only the password. The password field is the one with onSubmitEditing, so return there calls onSignIn(email, password), and its returnKeyType="go" only relabels the key.

MessageComposer combines multiline, submitBehavior="submit" and a send return key. Its submit handler calls onSend(message) and then setMessage(''), so return sends the message, empties the controlled field and leaves it focused for the next one. The textAlignVertical: 'top' style keeps the text at the top on Android, matching iOS.

Code Examples

tsx
import {useState} from 'react';
import {TextInput, View} from 'react-native';

type SignInFormProps = {
  onSignIn: (email: string, password: string) => void;
};

export function SignInForm({onSignIn}: SignInFormProps) {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  return (
    <View style={{padding: 16, gap: 12}}>
      <TextInput
        placeholder="Email"
        inputMode="email"
        value={email}
        onChangeText={setEmail}
      />
      <TextInput
        placeholder="Password"
        secureTextEntry
        returnKeyType="go"
        value={password}
        onChangeText={setPassword}
        onSubmitEditing={() => onSignIn(email, password)}
      />
    </View>
  );
}
tsx
import {useState} from 'react';
import {TextInput} from 'react-native';

type MessageComposerProps = {
  onSend: (message: string) => void;
};

export function MessageComposer({onSend}: MessageComposerProps) {
  const [message, setMessage] = useState('');

  return (
    <TextInput
      placeholder="Message"
      multiline
      submitBehavior="submit"
      returnKeyType="send"
      value={message}
      onChangeText={setMessage}
      onSubmitEditing={() => {
        onSend(message);
        setMessage('');
      }}
      style={{minHeight: 44, padding: 8, textAlignVertical: 'top'}}
    />
  );
}
✓ Completed