table_rs/yew/
types.rs

1use std::collections::HashMap;
2use yew::prelude::*;
3
4/// Represents a column in the table with customization options.
5#[derive(Properties, PartialEq, Clone, Default)]
6pub struct Column {
7    /// Unique identifier for the column.
8    #[prop_or("")]
9    pub id: &'static str,
10
11    /// Header text displayed at the top of the column.
12    #[prop_or("")]
13    pub header: &'static str,
14
15    /// Callback triggered when accessing column data; can be used for custom rendering.
16    #[prop_or(Callback::noop())]
17    pub accessor: Callback<()>,
18
19    /// Determines if the column is sortable.
20    #[prop_or(false)]
21    pub sortable: bool,
22
23    /// Minimum width of the column in pixels.
24    #[prop_or(100)]
25    pub min_width: u32,
26
27    /// Optional inline style string for the column header.
28    #[prop_or(Some("padding: 8px; font-weight: 600; text-align: left;"))]
29    pub style: Option<&'static str>,
30
31    /// Optional class name(s) for the column header.
32    #[prop_or(Some("table-header-cell"))]
33    pub class: Option<&'static str>,
34}
35
36/// Sort direction for a column: ascending or descending.
37#[derive(Clone, PartialEq, Default)]
38pub enum SortOrder {
39    /// Ascending order (default).
40    #[default]
41    Asc,
42
43    /// Descending order.
44    Desc,
45}
46
47/// Class names used to style various parts of the table.
48#[derive(Properties, PartialEq, Clone)]
49pub struct TableClasses {
50    /// Class name for the container wrapping the table.
51    #[prop_or("table-container")]
52    pub container: &'static str,
53
54    /// Class name for the table element.
55    #[prop_or("table")]
56    pub table: &'static str,
57
58    /// Class name for the `<thead>` element.
59    #[prop_or("thead")]
60    pub thead: &'static str,
61
62    /// Class name for the `<tbody>` element.
63    #[prop_or("tbody")]
64    pub tbody: &'static str,
65
66    /// Class name for pagination controls wrapper.
67    #[prop_or("pagination-controls")]
68    pub pagination: &'static str,
69
70    /// Class name for the search input.
71    #[prop_or("search-input")]
72    pub search_input: &'static str,
73
74    /// Class name for header cells (`<th>`).
75    #[prop_or("th")]
76    pub header_cell: &'static str,
77
78    /// Class name for body cells (`<td>`).
79    #[prop_or("td")]
80    pub body_cell: &'static str,
81
82    /// Class name for table rows (`<tr>`).
83    #[prop_or("tr")]
84    pub row: &'static str,
85
86    /// Class name for the loading row.
87    #[prop_or("loading-row")]
88    pub loading_row: &'static str,
89
90    /// Class name for the empty row.
91    #[prop_or("empty-row")]
92    pub empty_row: &'static str,
93
94    /// Class name for pagination buttons.
95    #[prop_or("pagination-button")]
96    pub pagination_button: &'static str,
97}
98
99impl Default for TableClasses {
100    fn default() -> Self {
101        Self {
102            container: "table-container",
103            table: "table",
104            thead: "thead",
105            tbody: "tbody",
106            pagination: "pagination-controls",
107            search_input: "search-input",
108            header_cell: "th",
109            body_cell: "td",
110            row: "tr",
111            loading_row: "loading-row",
112            empty_row: "empty-row",
113            pagination_button: "pagination-button",
114        }
115    }
116}
117
118/// Texts used in various parts of the table UI.
119#[derive(Properties, PartialEq, Clone)]
120pub struct TableTexts {
121    /// Text shown while data is loading.
122    #[prop_or("Loading...")]
123    pub loading: &'static str,
124
125    /// Text shown when no rows are found.
126    #[prop_or("No results found")]
127    pub empty: &'static str,
128
129    /// Placeholder text for the search input.
130    #[prop_or("Search...")]
131    pub search_placeholder: &'static str,
132
133    /// Label for the "Previous" pagination button.
134    #[prop_or("Previous")]
135    pub previous_button: &'static str,
136
137    /// Label for the "Next" pagination button.
138    #[prop_or("Next")]
139    pub next_button: &'static str,
140
141    /// Format string for the page indicator, e.g., "Page 1 of 5".
142    #[prop_or("Page {current} of {total}")]
143    pub page_indicator: &'static str,
144}
145
146impl Default for TableTexts {
147    fn default() -> Self {
148        Self {
149            loading: "Loading...",
150            empty: "No results found",
151            search_placeholder: "Search...",
152            previous_button: "Previous",
153            next_button: "Next",
154            page_indicator: "Page {current} of {total}",
155        }
156    }
157}
158
159/// Props for the main table component.
160#[derive(Properties, PartialEq, Clone)]
161pub struct TableProps {
162    /// Vector of row data as key-value pairs.
163    #[prop_or_default]
164    pub data: Vec<HashMap<&'static str, String>>,
165
166    /// List of column definitions.
167    #[prop_or_default]
168    pub columns: Vec<Column>,
169
170    /// Number of rows per page.
171    #[prop_or(10)]
172    pub page_size: usize,
173
174    /// Whether the table is currently in a loading state.
175    #[prop_or(false)]
176    pub loading: bool,
177
178    /// Class names used to style the table.
179    #[prop_or_default]
180    pub classes: TableClasses,
181
182    /// Optional inline styles mapped by component part.
183    #[prop_or_default]
184    pub styles: HashMap<&'static str, &'static str>,
185
186    /// Whether to enable pagination.
187    #[prop_or(false)]
188    pub paginate: bool,
189
190    /// Whether to enable search functionality.
191    #[prop_or(false)]
192    pub search: bool,
193
194    /// Text labels for the table UI.
195    #[prop_or_default]
196    pub texts: TableTexts,
197}
198
199/// Props for the table header including sorting logic.
200#[derive(Properties, PartialEq, Clone)]
201pub struct HeaderProps {
202    /// Column definitions for the header.
203    #[prop_or_default]
204    pub columns: Vec<Column>,
205
206    /// Current column used for sorting.
207    pub sort_column: UseStateHandle<Option<&'static str>>,
208
209    /// Current sort order (ascending/descending).
210    pub sort_order: UseStateHandle<SortOrder>,
211
212    /// Callback triggered when a column header is clicked for sorting.
213    #[prop_or(Callback::noop())]
214    pub on_sort_column: Callback<&'static str>,
215
216    /// CSS classes used to style the header.
217    #[prop_or_default]
218    pub classes: TableClasses,
219}
220
221/// Alias for `HeaderProps` to be used explicitly in header components.
222#[derive(Properties, PartialEq, Clone)]
223pub struct TableHeaderProps {
224    /// Column definitions for the header.
225    #[prop_or_default]
226    pub columns: Vec<Column>,
227
228    /// Current column used for sorting.
229    pub sort_column: UseStateHandle<Option<&'static str>>,
230
231    /// Current sort order (ascending/descending).
232    pub sort_order: UseStateHandle<SortOrder>,
233
234    /// Callback triggered when a column is sorted.
235    #[prop_or(Callback::noop())]
236    pub on_sort_column: Callback<&'static str>,
237
238    /// CSS classes used to style the table header.
239    #[prop_or_default]
240    pub classes: TableClasses,
241}
242
243/// Props for the pagination controls component.
244#[derive(Properties, PartialEq, Clone)]
245pub struct PaginationControlsProps {
246    /// Current page index.
247    pub page: UseStateHandle<usize>,
248
249    /// Total number of pages.
250    #[prop_or(1)]
251    pub total_pages: usize,
252
253    /// Class names used to style pagination elements.
254    #[prop_or_default]
255    pub classes: TableClasses,
256
257    /// Texts used in pagination controls.
258    #[prop_or_default]
259    pub texts: TableTexts,
260}
261
262/// Props for rendering the body of the table.
263#[derive(Properties, PartialEq, Clone)]
264pub struct TableBodyProps {
265    /// Column definitions.
266    #[prop_or_default]
267    pub columns: Vec<Column>,
268
269    /// List of row data to render.
270    #[prop_or_default]
271    pub rows: Vec<HashMap<&'static str, String>>,
272
273    /// Indicates if the body is in a loading state.
274    #[prop_or(false)]
275    pub loading: bool,
276
277    /// Class names used to style the table body.
278    #[prop_or_default]
279    pub classes: TableClasses,
280
281    /// Text labels used in the body (e.g., loading, empty).
282    #[prop_or_default]
283    pub texts: TableTexts,
284}