title: Input description: “The native text field: controlled and uncontrolled text, keyboards, autofill, selection, multiline inputs and focus.” order: 6 group: primitives demo: el-input

Input

Input is a native text field: a UITextField (single line) or UITextView (multiline) on iOS and Mac Catalyst, an EditText on Android. Typing, the caret, selection, autocorrection and autofill are the platform's own. Rust makes one with input(), TSX with <Input> from nsh.

use nsh_core::ui::prelude::*;

fn name_field() -> impl IntoElement {
    let name = signal(String::new());
    input()
        .value(name)
        .on_change(move |text: &str| name.set(text.to_string()))
        .placeholder("Your name")
        .auto_capitalize(AutoCapitalize::Words)
        .return_key_type(ReturnKeyType::Done)
        .text_base()
        .p_3()
        .border_1()
        .border_color(rgb(0xd0d7de))
        .rounded(px(8.0))
}
import { Input, signal } from "nsh";

function NameField() {
  const name = signal("");
  return (
    <Input
      value={name()}
      onChange={(text) => name.set(text)}
      placeholder="Your name"
      autoCapitalize="words"
      returnKeyType="done"
      style={{ fontSize: 16, padding: 12, borderWidth: 1, borderColor: "#d0d7de", borderRadius: 8 }}
    />
  );
}

Controlled and uncontrolled

  • Controlled: pass value and update it in onChange. The app can rewrite what the user typed (trim it, format it): the text shows the edit at once and is corrected when the new value arrives. Each native edit carries a count, and a value from the app is applied when it is based on the latest edit, so newer typing always wins over a slow update.
  • Uncontrolled: pass defaultValue (.default_value(..), or nothing) and read the text from onChange or onSubmit.

Rules that must hold while the user types are props, answered natively: maxLength limits the length and submitBehavior decides what return does. See forms and the keyboard.

Props

Besides the common props (with style typed as TextStyle: font, color, alignment and the view styles apply):

Text

PropRustTypeDefaultDescription
value.value(..)stringThe text, controlled by the app.
defaultValue.default_value(..)stringThe initial text.
placeholder.placeholder(..)stringShown while the input is empty.
multiline.multiline(..)booleanfalseSeveral lines; return inserts a line break unless submitBehavior says otherwise.
maxLength.max_length(..)numberThe most characters the user can enter.
readonly.readonly(..)booleanfalseThe user can select and copy the text but not change it.
masked.masked(..)booleanfalseThe text is hidden (passwords).
allowFontScalingbooleantrueFollow the system text size.
maxFontSizeMultipliernumber0The upper bound of the system scale (0 is none).

Keyboard

PropRustTypeDefaultDescription
keyboardType.keyboard_type(KeyboardType::Email)"default" | "number-pad" | "decimal-pad" | "numeric" | "numbers-and-punctuation" | "email-address" | "phone-pad" | "url""default"The keyboard layout (the values; numbers-and-punctuation has a minus sign on iOS).
returnKeyType.return_key_type(ReturnKeyType::Next)"default" | "done" | "go" | "next" | "search" | "send""default"The label of the return key.
autoCapitalize.auto_capitalize(AutoCapitalize::None)"none" | "sentences" | "words" | "characters""sentences"Automatic capitalization.
autoCorrect.auto_correct(..)booleantrueThe system's corrections.
autoComplete.auto_fill(AutoFill::OneTimeCode)AutoComplete | TextContentTypeWhat the input holds, for autofill: "username", "current-password", "new-password", "one-time-code", "email", "tel", "postal-code", "cc-number", "off", …
submitBehavior.submit_behavior(SubmitBehavior::Submit)"submit" | "blurAndSubmit" | "newline""blurAndSubmit" (single line), "newline" (multiline)What return does: "submit" reports onSubmit and keeps the focus, "blurAndSubmit" also unfocuses, "newline" inserts a line break (multiline; a single line submits). A multiline input that submits still takes line breaks from Shift+Return or Option / Alt+Return on hardware keyboards. "submit" keeps the keyboard up (e.g. to send several messages).
autoFocus.auto_focus(..)booleanfalseFocused when it appears.

Caret and selection

PropRustTypeDescription
caret.caret(..)booleanThe caret is drawn (default true; false hides it).
selectedRange.selected_range((start, end)){ start: number; end?: number }The controlled selection, in UTF-16 units; end defaults to start (a caret).

Colors

The placeholder's color and the color of the caret and the selection are style keys, with their Tailwind variants, and methods in Rust:

Style keyRustClassDescription
placeholderColor.placeholder_color(..)placeholder:text-*The placeholder's color.
selectionColor.selection_color(..)selection:text-*The color of the caret and the selection.
input()
    .placeholder("Search")
    .text_color(rgb(0x18181b))
    .placeholder_color(rgb(0x9ca3af))
    .selection_color(rgb(0x2563eb))
<Input placeholder="Search" class="text-zinc-900 placeholder:text-zinc-400 selection:text-blue-600" />
<Input placeholder="Search" style={{ placeholderColor: "#9ca3af", selectionColor: "#2563eb" }} />

iOS

ios keyRustTypeDescription
textContentType.auto_fill(..)AutoComplete | TextContentTypeiOS's names for autoComplete ("oneTimeCode", "newPassword", …).
inputAccessoryViewID.input_accessory(id)stringThe nativeID of an InputAccessoryView (nsh/ios) shown over the keyboard while the input has focus.
keyboardAppearance.keyboard_appearance(KeyboardAppearance::Dark)"default" | "light" | "dark"The keyboard‘s look; "default" follows the system’s appearance. Android‘s keyboards follow the system’s theme.
use nsh_core::ui::AutoFill;

input().auto_fill(AutoFill::OneTimeCode).keyboard_type(KeyboardType::Number)
<Input autoComplete="one-time-code" keyboardType="number-pad" ios={{ textContentType: "oneTimeCode" }} />

Events

EventRustPayloadWhen
onChange.on_change(|text: &str| ..)(text: string)The user changed the text.
onSubmit.on_submit(|text: &str| ..){ text }The return key was pressed in an input that submits (single line, or submitBehavior "submit" / "blurAndSubmit").
onFocus / onBlur.on_focus / .on_blurnoneThe input gained / lost focus.
onSelectionChange.on_selection_change(|s: &Selection| ..){ start, end }The caret or selection moved (UTF-16 units).
onKeyPress.on_key_press(|key: &str| ..){ key }Before the text changes: key is the typed text, "Backspace" or "Enter".
onContentSizeChange.on_content_size_change(|size: &Size<Points>| ..){ width, height }The size of the text changed (at the input's width when multiline), as it is typed; an empty input reports one empty line, whatever its placeholder.
onCompositionChange.on_composition_change(|e: &Composition| ..){ composing, text }An input method's composition started, changed or ended: text is what is being composed (pinyin before a character is chosen, kana before conversion), "" once it ends. The committed text reaches onChange before the end.

A multiline input that grows with its text:

let height = signal(px(40.0));

input()
    .multiline(true)
    .on_content_size_change(move |size: &Size<Points>| {
        height.set(px((size.height.0 + 16.0).clamp(40.0, 120.0)));
    })
    .h(move || height.get())
    .px_3()
    .py_2()
const height = signal(40);
<Input
  multiline
  onContentSizeChange={(e) => height.set(Math.min(120, Math.max(40, e.height + 16)))}
  style={{ height: height(), paddingHorizontal: 12, paddingVertical: 8 }}
/>;

Ref methods

MethodRustDescription
focus()focus()Focuses the input and shows the keyboard.
blur()blur()Unfocuses it.
setSelection(start, end?)set_selection(start, end)Selects start..end (UTF-16 units); a caret when they are equal.

Moving focus to the next field on return:

use nsh_core::ui::AutoFill;

let password = element_ref();

view()
    .child(input().return_key_type(ReturnKeyType::Next).on_submit(move |_: &str| password.focus()))
    .child(input().element_ref(&password).masked(true).auto_fill(AutoFill::Password))
let password!: YNode;
<>
  <Input returnKeyType="next" onSubmit={() => password.focus()} />
  <Input ref={password} masked autoComplete="current-password" />
</>;

Keyboard.dismiss() from nsh/base (dismiss_keyboard() in Rust) hides the keyboard whichever input has it.

Platform notes

  • Inputs inside a scroll view are kept clear of the keyboard natively. Elsewhere use KeyboardAvoidingView.
  • disabled disables the input (no focus) as well as applying the disabled styles; a readonly input keeps its focus and selection.
  • inputAccessoryViewID applies on iOS, where the keyboard has an accessory bar.