rooting_forms/
impl_password.rs

1use {
2    rooting::{
3        El,
4        el,
5    },
6    wasm_bindgen::JsCast,
7    web_sys::HtmlInputElement,
8    crate::{
9        css::{
10            ATTR_LABEL,
11            CSS_CLASS_SMALL_INPUT,
12        },
13        FormWith,
14        FormElements,
15        FormState,
16    },
17};
18
19/// A minimal string wrapper that creates a password form input.
20pub struct Password(pub String);
21
22/// A helper form type for rust types that implement `FromStr`.
23pub struct PasswordFormState {
24    pub el: El,
25}
26
27impl FormState<Password> for PasswordFormState {
28    fn parse(&self) -> Result<Password, ()> {
29        return Ok(Password(self.el.raw().dyn_ref::<HtmlInputElement>().unwrap().value()));
30    }
31}
32
33impl<C> FormWith<C> for Password {
34    fn new_form_with_(
35        _context: &C,
36        field: &str,
37        from: Option<&Self>,
38        _depth: usize,
39    ) -> (FormElements, Box<dyn FormState<Self>>) {
40        let input_el =
41            el("input")
42                .classes(&[CSS_CLASS_SMALL_INPUT])
43                .attr(ATTR_LABEL, field)
44                .attr("type", "password")
45                .attr("value", match from {
46                    Some(x) => &x.0,
47                    None => "",
48                });
49        return (FormElements {
50            error: None,
51            elements: vec![input_el.clone()],
52        }, Box::new(PasswordFormState { el: input_el }));
53    }
54}