Skip to main content

NumberField

Struct NumberField 

Source
pub struct NumberField<S, T> { /* private fields */ }
Expand description

A number field bound to an i32 or f64 signal, with its label and, where given, a description and an error message tied to it.

It is an <input type="number"> with the ids, aria-describedby, and aria-invalid of a TextField. min, max, and step take the field’s own type, so a bound cannot be text. A field whose min is 0 or more asks for a keyboard of digits, which on a phone has no minus sign. An f64 field without a step takes any decimal.

use hypersig::Signals;
use hypersig_ui::NumberField;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Cart {
    quantity: i32,
}

let quantity = NumberField::new("Quantity", Cart::quantity()).min(1).max(10);
let html = quantity.render().render();
assert!(html.contains(r#"type="number""#));
assert!(html.contains(r#"min="1""#));
assert!(html.contains(r#"inputmode="numeric""#));
assert!(html.contains(r#"for="ui-quantity-input""#));

Implementations§

Source§

impl<S, T: Numeric> NumberField<S, T>

Source

pub fn new(label: impl Into<String>, value: Sig<S, T>) -> Self

A number field labeled label, bound to value.

Source

pub fn in_form<V: Validate>( label: impl Into<String>, form: Sig<S, Form<V>>, field: Sig<V, T>, ) -> Self

A number field labeled label, bound to field of form.

It shows error of the form for field and marks the field touched when the visitor leaves it, as TextField::in_form does.

Source

pub fn min(self, min: T) -> Self

Sets the lowest value the browser accepts.

§Panics

Panics if min is above a maximum set before, or is not a number.

Source

pub fn max(self, max: T) -> Self

Sets the highest value the browser accepts.

§Panics

Panics if max is below a minimum set before, or is not a number.

Source

pub fn step(self, step: T) -> Self

Sets how far the arrow keys move the value, and which values the browser accepts: the minimum plus a whole number of steps.

§Panics

Panics if step is not above zero.

Source

pub fn description(self, text: impl Into<String>) -> Self

Adds a line under the field that aria-describedby points at.

Source

pub fn error(self, message: impl IntoExpr<S, String>) -> Self

Shows message under the field and marks the field invalid while message is not empty.

Source

pub fn required(self) -> Self

Marks the field required, which the label shows with an asterisk that a screen reader skips.

Source

pub fn disabled(self, when: impl IntoExpr<S, bool>) -> Self

Disables the field while when is true.

Source

pub fn render(self) -> El<S>

The label, the field, the description, and the error, in a <div>.

Trait Implementations§

Source§

impl<S: Debug, T: Debug> Debug for NumberField<S, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S, T: Numeric> From<NumberField<S, T>> for El<S>

Source§

fn from(field: NumberField<S, T>) -> Self

Converts to this type from the input type.
Source§

impl<S, T: Numeric> From<NumberField<S, T>> for Node<S>

Source§

fn from(field: NumberField<S, T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<S, T> Freeze for NumberField<S, T>
where T: Freeze,

§

impl<S, T> RefUnwindSafe for NumberField<S, T>
where T: RefUnwindSafe,

§

impl<S, T> Send for NumberField<S, T>
where T: Send,

§

impl<S, T> Sync for NumberField<S, T>
where T: Sync,

§

impl<S, T> Unpin for NumberField<S, T>
where T: Unpin,

§

impl<S, T> UnsafeUnpin for NumberField<S, T>
where T: UnsafeUnpin,

§

impl<S, T> UnwindSafe for NumberField<S, T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.