windjammer_ui/components/generated/
table.rs

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