table_rs/dioxus/
types.rs

1use dioxus::prelude::*;
2use std::collections::HashMap;
3
4/// Represents a column definition for the table.
5#[derive(PartialEq, Props, Clone, Default)]
6pub struct Column {
7    /// Unique identifier for the column.
8    pub id: &'static str,
9
10    /// Header text displayed in the column.
11    pub header: &'static str,
12
13    /// Whether this column is sortable.
14    #[props(default)]
15    pub sortable: bool,
16
17    /// Provide custom element generator (defaults to plain String -> String).
18    #[props(default)]
19    pub cell: Option<Callback<String, Element>>,
20
21    /// Minimum width of the column (default is 100).
22    #[props(default = 100)]
23    pub min_width: u32,
24
25    /// Optional inline styles for the column header.
26    #[props(default)]
27    pub style: Option<&'static str>,
28
29    /// Optional CSS classes for the column header.
30    #[props(default)]
31    pub class: Option<&'static str>,
32}
33
34/// Text labels for table UI elements.
35#[derive(PartialEq, Props, Clone)]
36pub struct TableTexts {
37    /// Text shown when data is loading.
38    #[props(default = "Loading...")]
39    pub loading: &'static str,
40
41    /// Text shown when no data is available.
42    #[props(default = "No results found")]
43    pub empty: &'static str,
44
45    /// Placeholder text for the search input.
46    #[props(default = "Search...")]
47    pub search_placeholder: &'static str,
48
49    /// Label for the previous page button.
50    #[props(default = "Previous")]
51    pub previous_button: &'static str,
52
53    /// Label for the next page button.
54    #[props(default = "Next")]
55    pub next_button: &'static str,
56
57    /// Page indicator text with placeholders `{current}` and `{total}`.
58    #[props(default = "Page {current} of {total}")]
59    pub page_indicator: &'static str,
60}
61
62impl Default for TableTexts {
63    fn default() -> Self {
64        Self {
65            loading: "Loading...",
66            empty: "No results found",
67            search_placeholder: "Search...",
68            previous_button: "Previous",
69            next_button: "Next",
70            page_indicator: "Page {current} of {total}",
71        }
72    }
73}
74
75/// Defines the styling classes for each part of the table.
76#[derive(Clone, PartialEq)]
77pub struct TableClasses {
78    /// Wrapper around the entire table.
79    pub container: &'static str,
80
81    /// Class for the `<table>` element.
82    pub table: &'static str,
83
84    /// Class for the `<thead>` element.
85    pub thead: &'static str,
86
87    /// Class for the `<tbody>` element.
88    pub tbody: &'static str,
89
90    /// Wrapper for pagination controls.
91    pub pagination: &'static str,
92
93    /// Class for the search input field.
94    pub search_input: &'static str,
95
96    /// Class for header cells (`<th>`).
97    pub header_cell: &'static str,
98
99    /// Class for body cells (`<td>`).
100    pub body_cell: &'static str,
101
102    /// Class for each table row.
103    pub row: &'static str,
104
105    /// Class for the row shown while loading.
106    pub loading_row: &'static str,
107
108    /// Class for the row shown when no data is found.
109    pub empty_row: &'static str,
110
111    /// Class for pagination buttons.
112    pub pagination_button: &'static str,
113}
114
115impl Default for TableClasses {
116    fn default() -> Self {
117        Self {
118            container: "table-container",
119            table: "table",
120            thead: "thead",
121            tbody: "tbody",
122            pagination: "pagination-controls",
123            search_input: "search-input",
124            header_cell: "th",
125            body_cell: "td",
126            row: "tr",
127            loading_row: "loading-row",
128            empty_row: "empty-row",
129            pagination_button: "pagination-button",
130        }
131    }
132}
133
134/// Main props for the table component.
135#[derive(PartialEq, Props, Clone)]
136pub struct TableProps {
137    /// Data rows, where each row is a key-value map.
138    #[props(default)]
139    pub data: Vec<HashMap<&'static str, String>>,
140
141    /// Definitions of columns to display.
142    #[props(default)]
143    pub columns: Vec<Column>,
144
145    /// Number of rows per page (default is 10).
146    #[props(default = 10)]
147    pub page_size: usize,
148
149    /// Indicates whether the table is loading.
150    #[props(default)]
151    pub loading: bool,
152
153    /// Enables pagination controls.
154    #[props(default = false)]
155    pub paginate: bool,
156
157    /// Enables the search input field.
158    #[props(default = false)]
159    pub search: bool,
160
161    /// Texts for various table UI messages.
162    #[props(default)]
163    pub texts: TableTexts,
164
165    /// CSS classes for styling different parts of the table.
166    #[props(default)]
167    pub classes: TableClasses,
168}
169
170/// Sort direction (ascending or descending).
171#[derive(PartialEq, Clone, Copy, Default)]
172pub enum SortOrder {
173    /// Ascending (default).
174    #[default]
175    Asc,
176    /// Descending.
177    Desc,
178}