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