日本語

Text field

A text field with its label, and where given, a description and an error message tied to it.

Result

text_field.rs
use hypersig::html::div;
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals, if_else};
use hypersig_ui::{Autocomplete, InputKind, NumberField, TextField};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub struct Signup {
    email: String,
    bio: String,
    guests: i32,
}

pub fn page() -> Page<Signup> {
    let email = Signup::email();
    let looks_wrong = !email.is_empty().or(email.contains("@"));
    let initial = Signup {
        email: String::new(),
        bio: String::new(),
        guests: 1,
    };
    Page::new(
        "Sign up",
        initial,
    )
    .child(
        div()
            .css(stack(rem(1.25)).max_width(rem(24)))
            .child(
                TextField::new(
                    "Email",
                    email,
                )
                .kind(InputKind::Email)
                .autocomplete(Autocomplete::Email)
                .placeholder("ada@example.com")
                .description("We send the receipt here.")
                .error(if_else(
                    looks_wrong,
                    "Enter an email address.",
                    "",
                ))
                .required(),
            )
            .child(
                TextField::new(
                    "Bio",
                    Signup::bio(),
                )
                .multiline()
                .description("A sentence or two for your profile."),
            )
            .child(
                NumberField::new(
                    "Guests",
                    Signup::guests(),
                )
                .min(1)
                .max(10)
                .description("Up to 10 people."),
            ),

    )
}

Accessibility

A text field is a <label> and an <input>, or a <textarea> in place of the <input> with multiline. The ids that tie the label to the input are derived from the signal's path, so a label always names its own field. aria-describedby points at the description and the error, and aria-invalid is true while the error is not empty.

The error sits in an aria-live="polite" region, so a screen reader reads a new message when it appears. A screen reader reads "required" from the field itself, so it skips the asterisk that a required field shows. autocomplete tells the browser what the field holds, to meet WCAG 1.3.5.

NumberField is an <input type="number"> bound to an i32 or f64 signal, with the same label, ids, and messages. Its min, max, and step take the signal's own type. A field whose minimum is 0 or more asks for a keyboard of digits.

API

ItemSummary
TextField::newA single-line text field labeled label, bound to value.
TextField::in_formA single-line text field labeled label, bound to field of form.
kindSets the type of the input. A multiline field ignores it.
multilineMakes the field a <textarea> that the visitor can resize vertically.
autocompleteTells the browser what the field holds, so that it can fill it in.
input_modeAsks for the on-screen keyboard mode, as inputmode does.
max_lengthStops the visitor from typing more than count characters, as maxlength does.
min_lengthMakes the browser refuse to submit fewer than count characters, as minlength does.
placeholderShows text in the empty field.
descriptionAdds a line under the field that aria-describedby points at.
errorShows message under the field and marks the field invalid while message is not empty.
requiredMarks the field required, which the label shows with an asterisk.
disabledDisables the field while when is true.
InputKindThe type of a single-line TextField.
InputModeThe on-screen keyboard a TextField asks for, as inputmode names it.
NumberField::newA number field labeled label, bound to value.
NumberField::in_formA number field labeled label, bound to field of form.
minSets the lowest value the browser accepts.
maxSets the highest value the browser accepts.
stepSets how far the arrow keys move the value, and which values the browser accepts.
NumericA number type a NumberField can be bound to: i32 or f64.
AutocompleteWhat a TextField holds, as autocomplete names it.