windjammer_ui/components/generated/
form.rs

1#![allow(clippy::all)]
2#![allow(noop_method_call)]
3use super::traits::Renderable;
4
5#[derive(Debug, Clone, PartialEq, Eq, Default)]
6pub struct Form {
7    pub id: String,
8    pub action: String,
9    pub method: String,
10    pub children: Vec<String>,
11    pub on_submit: String,
12}
13
14impl Form {
15    #[inline]
16    pub fn new(id: String) -> Form {
17        Form {
18            id,
19            action: "#".to_string(),
20            method: "POST".to_string(),
21            children: Vec::new(),
22            on_submit: "return false;".to_string(),
23        }
24    }
25    #[inline]
26    pub fn action(mut self, action: String) -> Form {
27        self.action = action;
28        self
29    }
30    #[inline]
31    pub fn method(mut self, method: String) -> Form {
32        self.method = method;
33        self
34    }
35    #[inline]
36    pub fn on_submit(mut self, handler: String) -> Form {
37        self.on_submit = handler;
38        self
39    }
40    #[inline]
41    pub fn child(mut self, child: String) -> Form {
42        self.children.push(child);
43        self
44    }
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
48pub struct FormField {
49    pub label: String,
50    pub input: String,
51    pub error: String,
52    pub required: bool,
53    pub help_text: String,
54}
55
56impl FormField {
57    #[inline]
58    pub fn new(label: String, input: String) -> FormField {
59        FormField {
60            label,
61            input,
62            error: String::new(),
63            required: false,
64            help_text: String::new(),
65        }
66    }
67    #[inline]
68    pub fn required(mut self, required: bool) -> FormField {
69        self.required = required;
70        self
71    }
72    #[inline]
73    pub fn error(mut self, error: String) -> FormField {
74        self.error = error;
75        self
76    }
77    #[inline]
78    pub fn help_text(mut self, text: String) -> FormField {
79        self.help_text = text;
80        self
81    }
82    #[inline]
83    pub fn render(&self) -> String {
84        let mut html = String::new();
85        html.push_str("<div style='margin-bottom: 16px;'>");
86        html.push_str(
87            "<label style='display: block; margin-bottom: 4px; font-weight: 500; color: #333;'>",
88        );
89        html.push_str(&self.label);
90        if self.required {
91            html.push_str(" <span style='color: #e53e3e;'>*</span>")
92        }
93        html.push_str("</label>");
94        html.push_str(&self.input);
95        if self.help_text.len() > (0 as usize) {
96            html.push_str("<div style='margin-top: 4px; font-size: 12px; color: #718096;'>");
97            html.push_str(&self.help_text);
98            html.push_str("</div>")
99        }
100        if self.error.len() > (0 as usize) {
101            html.push_str("<div style='margin-top: 4px; font-size: 12px; color: #e53e3e;'>");
102            html.push_str(&self.error);
103            html.push_str("</div>")
104        }
105        html.push_str("</div>");
106        html
107    }
108}
109
110impl Renderable for Form {
111    #[inline]
112    fn render(self) -> String {
113        let mut html = String::new();
114        html.push_str("<form id='");
115        html.push_str(&self.id);
116        html.push_str("' action='");
117        html.push_str(&self.action);
118        html.push_str("' method='");
119        html.push_str(&self.method);
120        html.push_str("' onsubmit='");
121        html.push_str(&self.on_submit);
122        html.push_str("'>");
123        for child in &self.children {
124            html.push_str(&child);
125        }
126        html.push_str("</form>");
127        html
128    }
129}