Skip to main content

switch_in_form

Function switch_in_form 

Source
pub fn switch_in_form<S, T: Validate>(
    text: impl Into<String>,
    form: Sig<S, Form<T>>,
    field: Sig<T, bool>,
) -> El<S>
Expand description

An on and off switch labeled text, bound to field of form.

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

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

#[derive(Signals, Serialize, Deserialize, Clone)]
struct Settings {
    backups: bool,
}

impl Validate for Settings {
    fn rules<S>(form: Sig<S, Self>, rules: &mut Rules<S, Self>) {
        rules.check(Settings::backups(), form.at(Settings::backups()), "Keep backups on.");
    }
}

#[derive(Signals, Serialize, Deserialize)]
struct Page {
    settings: Form<Settings>,
}

let backups = switch_in_form("Backups", Page::settings(), Settings::backups());
assert!(backups.render().contains(r#"role="switch""#));