Number field
A labeled <input type="number"> bound to an i32 or f64 signal, whose min, max, and step take the signal's own type.
Result
use hypersig::html::div;
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{CalendarDate, Page, Signals, if_else};
use hypersig_ui::{Autocomplete, DateField, InputKind, NumberField, TextField};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Signup {
email: String,
bio: String,
guests: i32,
arrival: Option<CalendarDate>,
}
pub(super) 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,
arrival: None,
};
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."),
)
.child(
DateField::new(
"Arrival",
Signup::arrival(),
)
.min(CalendarDate::new(2026, 1, 1))
.max(CalendarDate::new(2026, 12, 31))
.description("Any day in 2026."),
),
)
}Accessibility
It is an <input type="number"> with the label, aria-describedby, aria-invalid, and live error of a text field. A screen reader announces it as a spin button with its value, and the browser gives it the keys below. The Guests field in the demo above is one.
| Key | What it does |
|---|---|
| Up, Down | Raises or lowers the value by one step, within min and max. |
| Digits | Type the value. A phone shows a keyboard of digits where min is 0 or more. |
`NumberField`
NumberField is a builder like TextField, with a second type parameter T, the number type of its signal. min, max, and step take a T, so a bound is a number of the field's own type:
impl<S, T: Numeric> NumberField<S, T> {
pub fn new(label: impl Into<String>, value: Sig<S, T>) -> Self
pub fn min(self, min: T) -> Self
pub fn max(self, max: T) -> Self
pub fn step(self, step: T) -> Self
pub fn description(self, text: impl Into<String>) -> Self
pub fn error(self, message: impl IntoExpr<S, String>) -> Self
pub fn required(self) -> Self
pub fn disabled(self, when: impl IntoExpr<S, bool>) -> Self
pub fn render(self) -> El<S>
}min(1).max(10) on an i32 field and min(0.0) on an f64 field render as follows, without the ids and the class. A minimum of 0 or more adds inputmode, numeric for i32 and decimal for f64, and an f64 field without a step gets step="any", since the browser's own step of 1 would refuse a value such as 2.5:
<label for="…">Quantity</label>
<input id="…" type="number" inputmode="numeric" min="1" max="10" value="1">
<label for="…">Tip</label>
<input id="…" type="number" inputmode="decimal" min="0" step="any" value="0">`Numeric`
Numeric is the bound on T. It is sealed and implemented for i32 and f64, the two types that the browser's number input and the runtime's binding read:
pub trait Numeric: Bindable + Copy + PartialOrd + Display + Sealed {}
impl Numeric for i32 {}
impl Numeric for f64 {}A signal of another type fails to compile with E0277 and the message a number field cannot be bound to a String signal, with the note bind an i32 or f64 signal; text goes in a TextField:
use hypersig::Signals;
use hypersig_ui::NumberField;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Cart {
quantity: String,
}
// A text signal bound to a number field.
let quantity = NumberField::new("Quantity", Cart::quantity());What the types leave unchecked
The types cannot see the values. min panics when it is above a maximum set before, max when it is below a minimum set before, either one on an f64 NaN, and step when it is not above zero. The panic comes where the field is built, so a test that renders the page finds it:
use hypersig::Signals;
use hypersig_ui::NumberField;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Order {
quantity: i32,
}
// Panics: "a number field's maximum is not below its minimum, but 1 is below 10".
let quantity = NumberField::new("Quantity", Order::quantity()).min(10).max(1);- The arrow keys stop at
minandmax, but a typed value outside them still goes to the signal. A plain<form>refuses to submit it, and a form fromSig::formdoes not, since it hasnovalidate. Check the range again in an error message or a form rule, and on the server. - A minimum below 0 adds no
inputmode, so a phone shows its full number keyboard with the minus sign. - A field the visitor empties leaves the signal at its last number, and a decimal such as 2.5 typed into an
i32field sets the signal to its integer part, 2.
Example: a quantity and a tip
use hypersig::html::{div, p, span};
use hypersig::{Page, Signals};
use hypersig_ui::NumberField;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Order {
quantity: i32,
tip: f64,
}
let quantity = Order::quantity();
let page = Page::new("Order", Order { quantity: 1, tip: 0.0 }).child(
div()
// `min(1)` is 0 or more, so a phone shows a keyboard of digits.
.child(
NumberField::new("Quantity", quantity)
.min(1)
.max(10)
.description("Up to 10 per order.")
.required(),
)
// An `f64` field without a step takes any decimal.
.child(NumberField::new("Tip", Order::tip()).min(0.0))
.child(p().text("Items: ").child(span().bind_text(quantity.to_text()))),
);Next
Slider sets an i32 by dragging. Fields bound to a form has NumberField::in_form, which takes its message from a form's rules.