1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use std::{
    convert::Infallible,
    fmt::Display,
    str::FromStr,
};
use rooting::{
    El,
    el,
};
use wasm_bindgen::JsCast;
use web_sys::HtmlInputElement;
use crate::{
    css::{
        ATTR_LABEL,
        CSS_CLASS_ERROR,
        CSS_CLASS_SMALL_INPUT,
    },
    FormWith,
    FormElements,
    FormState,
};

/// A minimal string wrapper that creates a password form input.
pub struct Password(pub String);

impl FromStr for Password {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        return Ok(Password(s.to_string()));
    }
}

/// A minimal string wrapper that creates a textarea form input.
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BigString(pub String);

impl FromStr for BigString {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        return Ok(BigString(s.to_string()));
    }
}

/// A helper form type for rust types that implement `FromStr`.
pub struct FromStrFormState {
    el: El,
    error_el: El,
}

impl FromStrFormState {
    pub fn new<
        E: Display,
        T: FromStr<Err = E>,
    >(label: &str, type_: &str, initial_value: &str) -> Box<dyn FormState<T>> {
        let error_el = el("span").classes(&[CSS_CLASS_ERROR]);
        return Box::new(FromStrFormState {
            el: el("input")
                .classes(&[CSS_CLASS_SMALL_INPUT])
                .attr(ATTR_LABEL, label)
                .attr("type", type_)
                .attr("value", initial_value)
                .on("change", {
                    let error_el = error_el.clone();
                    move |ev| {
                        let text = ev.target().unwrap().dyn_ref::<HtmlInputElement>().unwrap().value();
                        if text.len() >= 1 {
                            match T::from_str(&text) {
                                Err(e) => {
                                    error_el.ref_text(&e.to_string());
                                    return;
                                },
                                _ => { },
                            }
                        }
                        error_el.ref_text("");
                    }
                }),
            error_el: error_el,
        });
    }
}

impl<E: Display, T: FromStr<Err = E>> FormState<T> for FromStrFormState {
    fn elements(&self) -> FormElements {
        return FormElements {
            error: Some(self.error_el.clone()),
            elements: vec![self.el.clone()],
        };
    }

    fn parse(&self) -> Result<T, ()> {
        match T::from_str(&self.el.raw().dyn_ref::<HtmlInputElement>().unwrap().value()) {
            Ok(v) => {
                self.error_el.ref_text("");
                return Ok(v);
            },
            Err(e) => {
                self.error_el.ref_text(&e.to_string());
                return Err(());
            },
        }
    }
}

impl<C> FormWith<C> for String {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, String>(
            field,
            "text",
            from.as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for Password {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Password>(
            field,
            "password",
            from.as_ref().map(|x| x.0.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for BigString {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, BigString>(
            field,
            "text",
            from.as_ref().map(|x| x.0.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for u8 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for u16 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for u32 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for u64 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for i8 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for i16 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for i32 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for i64 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for f32 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}

impl<C> FormWith<C> for f64 {
    fn new_form(_context: &C, field: &str, from: Option<&Self>) -> Box<dyn FormState<Self>> {
        return FromStrFormState::new::<_, Self>(
            field,
            "text",
            from.map(|x| x.to_string()).as_ref().map(|x| x.as_str()).unwrap_or(""),
        );
    }
}