windjammer_ui/components/generated/
select.rs

1#![allow(clippy::all)]
2#![allow(noop_method_call)]
3#[derive(Debug, Clone)]
4pub struct Select {
5    pub options: Vec<SelectOption>,
6    pub selected: String,
7    pub placeholder: String,
8    pub disabled: bool,
9    pub size: SelectSize,
10    pub class: String,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
14pub struct SelectOption {
15    pub value: String,
16    pub label: String,
17}
18
19#[derive(Clone, Debug, PartialEq, Copy)]
20pub enum SelectSize {
21    Small,
22    Medium,
23    Large,
24}
25
26impl Select {
27    #[inline]
28    pub fn new() -> Select {
29        Select {
30            options: Vec::new(),
31            selected: String::new(),
32            placeholder: "Select an option".to_string(),
33            disabled: false,
34            size: SelectSize::Medium,
35            class: String::new(),
36        }
37    }
38    #[inline]
39    pub fn option(mut self, value: String, label: String) -> Select {
40        self.options.push(SelectOption { value, label });
41        self
42    }
43    #[inline]
44    pub fn selected(mut self, selected: String) -> Select {
45        self.selected = selected;
46        self
47    }
48    #[inline]
49    pub fn placeholder(mut self, placeholder: String) -> Select {
50        self.placeholder = placeholder;
51        self
52    }
53    #[inline]
54    pub fn disabled(mut self, disabled: bool) -> Select {
55        self.disabled = disabled;
56        self
57    }
58    #[inline]
59    pub fn size(mut self, size: SelectSize) -> Select {
60        self.size = size;
61        self
62    }
63    #[inline]
64    pub fn class(mut self, class: String) -> Select {
65        self.class = class;
66        self
67    }
68    #[inline]
69    pub fn render(&self) -> String {
70        let size_style = match self.size {
71            SelectSize::Small => "padding: 4px 8px; font-size: 12px;".to_string(),
72            SelectSize::Medium => "padding: 8px 12px; font-size: 14px;".to_string(),
73            SelectSize::Large => "padding: 12px 16px; font-size: 16px;".to_string(),
74        };
75        let disabled_attr = {
76            if self.disabled {
77                " disabled".to_string()
78            } else {
79                "".to_string()
80            }
81        };
82        let mut html = String::new();
83        html.push_str("<select class=\"wj-select ");
84        html.push_str(&self.class.as_str());
85        html.push_str("\" style=\"");
86        html.push_str(&size_style);
87        html.push_str(
88            " border: 1px solid #d1d5db; border-radius: 6px; background: white; cursor: pointer;\"",
89        );
90        html.push_str(&disabled_attr);
91        html.push('>');
92        if !self.placeholder.is_empty() {
93            html.push_str("<option value=\"\" disabled");
94            if self.selected.is_empty() {
95                html.push_str(" selected")
96            }
97            html.push_str(">");
98            html.push_str(&self.placeholder.as_str());
99            html.push_str("</option>")
100        }
101        for opt in &self.options {
102            html.push_str("<option value=\"");
103            html.push_str(&opt.value.clone().as_str());
104            html.push('"');
105            if opt.value.clone() == self.selected {
106                html.push_str(" selected")
107            }
108            html.push('>');
109            html.push_str(&opt.label.clone().as_str());
110            html.push_str("</option>");
111        }
112        html.push_str("</select>");
113        html
114    }
115}