dioxus-element-plug 0.1.4

Element UI components for Dioxus applications with pure Rust styling system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use dioxus::prelude::*;

// Table CSS class constants
pub const TABLE: &str = "el-table";
pub const TABLE_BORDERED: &str = "el-table--border";
pub const TABLE_STRIPED: &str = "el-table--striped";
pub const TABLE_HOVER: &str = "el-table--enable-row-hover";
pub const TABLE_HEADER: &str = "el-table__header";
pub const TABLE_BODY: &str = "el-table__body";
pub const TABLE_ROW: &str = "el-table__row";
pub const TABLE_CELL: &str = "el-table__cell";

/// Table column definition
#[derive(Clone, PartialEq)]
pub struct TableColumn {
    /// Column title
    pub title: String,
    /// Data key for the column
    pub key: String,
    /// Column width
    pub width: Option<String>,
    /// Whether the column is sortable
    pub sortable: bool,
    /// Whether the column is fixed (left/right)
    pub fixed: Option<String>,
}

/// Table row data type - simplified
pub type TableData = Vec<std::collections::HashMap<String, String>>;

/// Table props
#[derive(Props, Clone, PartialEq)]
pub struct TableProps {
    /// Table columns
    pub columns: Vec<TableColumn>,
    /// Table data
    pub data: TableData,
    /// Table height
    #[props(default)]
    pub height: Option<String>,
    /// Table max height
    #[props(default)]
    pub max_height: Option<String>,
    /// Whether to show header
    #[props(default = true)]
    pub show_header: bool,
    /// Whether to show border
    #[props(default = false)]
    pub border: bool,
    /// Whether to show stripe effect
    #[props(default = false)]
    pub stripe: bool,
    /// Whether to highlight current row
    #[props(default = false)]
    pub highlight_current_row: bool,
    /// Whether table is loading
    #[props(default = false)]
    pub loading: bool,
    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,
    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
}

/// A table component for displaying structured data
///
/// This component provides a basic table with features like sorting and styling.
///
/// ## Example
///
/// ```rust,ignore
/// use dioxus_element_plug::components::table::{Table, TableColumn};
/// use std::collections::HashMap;
///
/// let columns = vec![
///     TableColumn {
///         title: "Name".to_string(),
///         key: "name".to_string(),
///         width: Some("200px".to_string()),
///         sortable: true,
///         fixed: None,
///     },
///     TableColumn {
///         title: "Age".to_string(),
///         key: "age".to_string(),
///         width: None,
///         sortable: false,
///         fixed: None,
///     },
/// ];
///
/// let mut row1 = HashMap::new();
/// row1.insert("name".to_string(), "John".to_string());
/// row1.insert("age".to_string(), "30".to_string());
///
/// let data = vec![row1];
///
/// rsx! {
///     Table {
///         columns: columns,
///         data: data,
///         stripe: true,
///     }
/// }
/// ```
#[component]
pub fn Table(props: TableProps) -> Element {
    let mut class_names = vec!["el-table".to_string()];
    
    if props.border {
        class_names.push("el-table--border".to_string());
    }
    
    if props.stripe {
        class_names.push("el-table--striped".to_string());
    }
    
    if props.highlight_current_row {
        class_names.push("el-table--highlight-current-row".to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    let class_string = if props.border { "el-table el-table--border" } else { "el-table" };
    let stripe_class = if props.stripe { " el-table--striped" } else { "" };
    let full_class = format!("{}{}", class_string, stripe_class);

    rsx! {
        div {
            class: "el-table__wrapper",
            
            if let Some(height) = &props.height {
                div {
                    style: "max-height: {height}; overflow-y: auto;",
                    table {
                        class: "{full_class}",
                        {build_table_content(&props)}
                    }
                }
            } else {
                table {
                    class: "{full_class}",
                    {build_table_content(&props)}
                }
            }
        }
    }
}

fn build_table_content(props: &TableProps) -> Element {
    rsx! {
        if props.show_header {
            thead {
                class: "el-table__header",
                
                tr {
                    class: "el-table__row",
                    
                    for column in props.columns.iter() {
                        th {
                            class: "el-table__cell",
                            style: column.width.as_ref().map(|w| format!("width: {};", w)).unwrap_or_default(),
                            
                            div {
                                class: "cell",
                                
                                if column.sortable {
                                    span {
                                        "{column.title}"
                                    }
                                    span {
                                        class: "caret-wrapper",
                                        
                                        i {
                                            class: "sort-caret ascending"
                                        }
                                        
                                        i {
                                            class: "sort-caret descending"
                                        }
                                    }
                                } else {
                                    "{column.title}"
                                }
                            }
                        }
                    }
                }
            }
        }
        
        tbody {
            class: "el-table__body",
            
            for (row_index, row_data) in props.data.iter().enumerate() {
                tr {
                    class: if row_index % 2 == 1 && props.stripe {
                        "el-table__row el-table__row--striped"
                    } else {
                        "el-table__row"
                    },
                    
                    for column in props.columns.iter() {
                        td {
                            class: "el-table__cell",
                            
                            div {
                                class: "cell",
                                "{row_data.get(&column.key).unwrap_or(&String::new())}"
                            }
                        }
                    }
                }
            }
        }
        
        if props.loading {
            div {
                class: "el-table__loading",
                
                div {
                    class: "el-loading-spinner",
                    
                    i {
                        class: "el-icon-loading"
                    }
                    
                    span {
                        "Loading..."
                    }
                }
            }
        }
    }
}

#[component]
fn table_content(props: TableProps) -> Element {
    let class_string = if props.border { "el-table el-table--border" } else { "el-table" };
    let stripe_class = if props.stripe { " el-table--striped" } else { "" };
    let full_class = format!("{}{}", class_string, stripe_class);

    rsx! {
        table {
            class: "{full_class}",
            style: props.style.as_ref().cloned().unwrap_or_default(),
            
            if props.show_header {
                thead {
                    class: "el-table__header",
                    
                    tr {
                        class: "el-table__row",
                        
                        for column in props.columns.iter() {
                            th {
                                class: "el-table__cell",
                                style: column.width.as_ref().map(|w| format!("width: {};", w)).unwrap_or_default(),
                                
                                div {
                                    class: "cell",
                                    
                                    if column.sortable {
                                        span {
                                            "{column.title}"
                                        }
                                        span {
                                            class: "caret-wrapper",
                                            
                                            i {
                                                class: "sort-caret ascending"
                                            }
                                            
                                            i {
                                                class: "sort-caret descending"
                                            }
                                        }
                                    } else {
                                        "{column.title}"
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            tbody {
                class: "el-table__body",
                
                for (row_index, row_data) in props.data.iter().enumerate() {
                    tr {
                        class: if row_index % 2 == 1 && props.stripe {
                            "el-table__row el-table__row--striped"
                        } else {
                            "el-table__row"
                        },
                        
                        for column in props.columns.iter() {
                            td {
                                class: "el-table__cell",
                                
                                div {
                                    class: "cell",
                                    "{row_data.get(&column.key).unwrap_or(&String::new())}"
                                }
                            }
                        }
                    }
                }
            }
            
            if props.loading {
                div {
                    class: "el-table__loading",
                    
                    div {
                        class: "el-loading-spinner",
                        
                        i {
                            class: "el-icon-loading"
                        }
                        
                        span {
                            "Loading..."
                        }
                    }
                }
            }
        }
    }
}

/// Data list component for simpler data display
#[derive(Props, Clone, PartialEq)]
pub struct DataListProps {
    /// List items
    pub items: Vec<String>,
    /// Whether to show loading state
    #[props(default = false)]
    pub loading: bool,
    /// Whether to show empty state
    #[props(default = true)]
    pub show_empty: bool,
    /// Empty state message
    #[props(default = "No data".to_string())]
    pub empty_text: String,
    /// List direction (vertical/horizontal)
    #[props(default = "vertical".to_string())]
    pub direction: String,
    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,
    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
    /// Item click handler
    #[props(default)]
    pub on_item_click: Option<EventHandler<usize>>,
}

/// A data list component for displaying item collections
///
/// This component provides a flexible way to display collections of data
/// with custom templates and interaction handlers.
#[component]
pub fn DataList(props: DataListProps) -> Element {
    let mut class_names = vec!["el-data-list".to_string()];
    
    class_names.push(format!("el-data-list--{}", props.direction));
    
    if props.loading {
        class_names.push("el-data-list--loading".to_string());
    }
    
    if props.items.is_empty() {
        class_names.push("el-data-list--empty".to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    if props.items.is_empty() && props.show_empty {
        return rsx! {
            div {
                class: "{class_string} el-data-list--empty-state",
                style: "{style_string}",
                
                div {
                    class: "el-empty",
                    
                    div {
                        class: "el-empty__image",
                        i {
                            class: "el-icon-document"
                        }
                    }
                    
                    div {
                        class: "el-empty__description",
                        p {
                            "{props.empty_text}"
                        }
                    }
                }
            }
        };
    }
    
    rsx! {
        div {
            class: "{class_string}",
            style: "{style_string}",
            
            for (index, item) in props.items.iter().enumerate() {
                div {
                    class: "el-data-list__item",
                    
                    onclick: move |_| {
                        if let Some(handler) = props.on_item_click {
                            handler.call(index);
                        }
                    },
                    
                    "{item}"
                }
            }
            
            if props.loading {
                div {
                    class: "el-data-list__loading",
                    
                    div {
                        class: "el-loading-spinner",
                        
                        i {
                            class: "el-icon-loading"
                        }
                        
                        span {
                            "Loading..."
                        }
                    }
                }
            }
        }
    }
}