rooting_forms/
impl_bool.rs

1use rooting::{
2    El,
3    el,
4};
5use wasm_bindgen::JsCast;
6use web_sys::HtmlInputElement;
7use crate::{
8    css::{
9        ATTR_LABEL,
10        CSS_CLASS_SMALL_INPUT,
11    },
12    FormElements,
13    FormState,
14    FormWith,
15};
16
17struct BoolFormState {
18    input: El,
19}
20
21impl FormState<bool> for BoolFormState {
22    fn parse(&self) -> Result<bool, ()> {
23        return Ok(self.input.raw().dyn_ref::<HtmlInputElement>().unwrap().checked());
24    }
25}
26
27impl<C> FormWith<C> for bool {
28    fn new_form_with_(
29        _context: &C,
30        field: &str,
31        from: Option<&Self>,
32        _depth: usize,
33    ) -> (FormElements, Box<dyn FormState<Self>>) {
34        let e = el("input").classes(&[CSS_CLASS_SMALL_INPUT]).attr(ATTR_LABEL, field).attr("type", "checkbox");
35        if from.cloned().unwrap_or(false) {
36            e.ref_attr("checked", "checked");
37        }
38        return (FormElements {
39            error: None,
40            elements: vec![e.clone()],
41        }, Box::new(BoolFormState { input: e }));
42    }
43}