windjammer_ui/components/generated/
table.rs

1#![allow(clippy::all)]
2#![allow(noop_method_call)]
3use super::traits::Renderable;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
6pub struct TableColumn {
7    pub header: String,
8    pub width: String,
9}
10
11impl TableColumn {
12    #[inline]
13    pub fn new(header: String) -> TableColumn {
14        TableColumn {
15            header,
16            width: "auto".to_string(),
17        }
18    }
19    #[inline]
20    pub fn width(mut self, width: String) -> TableColumn {
21        self.width = width;
22        self
23    }
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Default)]
27pub struct TableRow {
28    pub cells: Vec<String>,
29}
30
31impl TableRow {
32    #[inline]
33    pub fn new() -> TableRow {
34        TableRow { cells: Vec::new() }
35    }
36    #[inline]
37    pub fn cell(mut self, content: String) -> TableRow {
38        self.cells.push(content);
39        self
40    }
41}
42
43#[derive(Debug, Clone, Default)]
44pub struct Table {
45    pub columns: Vec<TableColumn>,
46    pub rows: Vec<TableRow>,
47    pub striped: bool,
48    pub bordered: bool,
49    pub hoverable: bool,
50}
51
52impl Table {
53    #[inline]
54    pub fn new() -> Table {
55        Table {
56            columns: Vec::new(),
57            rows: Vec::new(),
58            striped: true,
59            bordered: true,
60            hoverable: true,
61        }
62    }
63    #[inline]
64    pub fn column(mut self, col: TableColumn) -> Table {
65        self.columns.push(col);
66        self
67    }
68    #[inline]
69    pub fn row(mut self, row: TableRow) -> Table {
70        self.rows.push(row);
71        self
72    }
73    #[inline]
74    pub fn striped(mut self, striped: bool) -> Table {
75        self.striped = striped;
76        self
77    }
78    #[inline]
79    pub fn bordered(mut self, bordered: bool) -> Table {
80        self.bordered = bordered;
81        self
82    }
83    #[inline]
84    pub fn hoverable(mut self, hoverable: bool) -> Table {
85        self.hoverable = hoverable;
86        self
87    }
88}
89
90impl Renderable for Table {
91    #[inline]
92    fn render(self) -> String {
93        let mut html = String::new();
94        let border_style = {
95            if self.bordered {
96                "border: 1px solid #e2e8f0; border-collapse: collapse;".to_string()
97            } else {
98                "border-collapse: collapse;".to_string()
99            }
100        };
101        html.push_str("<table style='width: 100%; ");
102        html.push_str(&border_style);
103        html.push_str("'>");
104        html.push_str("<thead style='background: #f7fafc; border-bottom: 2px solid #e2e8f0;'>");
105        html.push_str("<tr>");
106        for col in &self.columns {
107            html.push_str("<th style='padding: 12px; text-align: left; font-weight: 600; color: #2d3748; width: ");
108            html.push_str(&col.width);
109            if self.bordered {
110                html.push_str("; border: 1px solid #e2e8f0;")
111            }
112            html.push_str("'>");
113            html.push_str(&col.header.clone());
114            html.push_str("</th>");
115        }
116        html.push_str("</tr>");
117        html.push_str("</thead>");
118        html.push_str("<tbody>");
119        for (row_index, row) in self.rows.iter().enumerate() {
120            let bg_color = {
121                if self.striped && row_index % 2 == 1 {
122                    "background: #f7fafc;".to_string()
123                } else {
124                    "background: white;".to_string()
125                }
126            };
127            let hover_style = {
128                if self.hoverable {
129                    " onmouseover='this.style.background=\"#edf2f7\"' onmouseout='this.style.background=\"".to_string()
130                } else {
131                    "".to_string()
132                }
133            };
134            html.push_str("<tr style='");
135            html.push_str(&bg_color);
136            html.push('\'');
137            if self.hoverable {
138                html.push_str(&hover_style);
139                if self.striped && row_index % 2 == 1 {
140                    html.push_str("#f7fafc")
141                } else {
142                    html.push_str("white")
143                }
144                html.push_str("\"'")
145            }
146            html.push('>');
147            for cell in &row.cells {
148                html.push_str("<td style='padding: 12px; color: #4a5568;");
149                if self.bordered {
150                    html.push_str(" border: 1px solid #e2e8f0;")
151                }
152                html.push_str("'>");
153                html.push_str(&cell);
154                html.push_str("</td>");
155            }
156            html.push_str("</tr>");
157        }
158        html.push_str("</tbody>");
159        html.push_str("</table>");
160        html
161    }
162}