Skip to main content

select_field_in_form

Function select_field_in_form 

Source
pub fn select_field_in_form<S, T: Validate, U: Serialize + PartialEq, L: Into<String>>(
    title: impl Into<String>,
    form: Sig<S, Form<T>>,
    field: Sig<T, U>,
    options: impl IntoIterator<Item = (U, L)>,
) -> El<S>
Expand description

A select labeled title, offering options, bound to field of form.

It is select_field with the form’s error for field under it, tied to the <select> by aria-describedby and aria-invalid, and it marks the field touched when the visitor leaves the select.

use hypersig::{Form, Rules, Sig, Signals, Validate};
use hypersig_ui::select_field_in_form;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Default)]
enum Plan {
    #[default]
    Unset,
    Pro,
}

#[derive(Signals, Serialize, Deserialize, Clone)]
struct Signup {
    plan: Plan,
}

impl Validate for Signup {
    fn rules<S>(form: Sig<S, Self>, rules: &mut Rules<S, Self>) {
        let chosen = !form.at(Signup::plan()).equals(&Plan::Unset);
        rules.check(Signup::plan(), chosen, "Choose a plan.");
    }
}

#[derive(Signals, Serialize, Deserialize)]
struct Join {
    signup: Form<Signup>,
}

let plan = select_field_in_form(
    "Plan",
    Join::signup(),
    Signup::plan(),
    [(Plan::Unset, "Choose…"), (Plan::Pro, "Pro")],
);
let html = plan.render();
assert!(html.contains(r#"aria-describedby="ui-signup-value-plan-error""#));
assert!(html.contains("signup.touched.plan"));