windjammer_ui/components/generated/
form.rs

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