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
use crate::;
use *;
/// Creates a reactive table with the given columns and rows.
///
/// This is the main hook for setting up a table. It returns a [`TableData`] that can be used
/// with [`TableHeaders`] and [`TableCells`] components to render the table.
///
/// # Parameters
///
/// - `columns`: A tuple of columns implementing [`TableColumn`](crate::TableColumn)
/// - `rows`: A reactive signal containing the row data
///
/// # Example
///
/// ```
/// use dioxus::prelude::*;
/// use dioxus_tabular::*;
///
/// # #[derive(Clone, PartialEq)]
/// # struct User { id: u32, name: String }
/// # impl Row for User {
/// # fn key(&self) -> impl Into<String> { self.id.to_string() }
/// # }
/// # #[derive(Clone, PartialEq)]
/// # struct NameColumn;
/// # #[derive(Clone, PartialEq)]
/// # struct UserName(String);
/// # impl GetRowData<UserName> for User {
/// # fn get(&self) -> UserName { UserName(self.name.clone()) }
/// # }
/// # impl<R: Row + GetRowData<UserName>> TableColumn<R> for NameColumn {
/// # fn column_name(&self) -> String { "name".into() }
/// # fn render_header(&self, _: ColumnContext, _: Vec<Attribute>) -> Element { rsx! { th {} } }
/// # fn render_cell(&self, _: ColumnContext, row: &R, _: Vec<Attribute>) -> Element {
/// # rsx! { td { "{row.get().0}" } }
/// # }
/// # }
/// fn app() -> Element {
/// let users = use_signal(|| vec![
/// User { id: 1, name: "Alice".to_string() },
/// User { id: 2, name: "Bob".to_string() },
/// ]);
///
/// // Create table with single column
/// let data = use_tabular((NameColumn,), users.into());
///
/// rsx! {
/// table {
/// thead { tr { TableHeaders { data } } }
/// tbody {
/// for row in data.rows() {
/// tr { key: "{row.key()}", TableCells { row } }
/// }
/// }
/// }
/// }
/// }
/// ```
///
/// # Multiple Columns
///
/// ```
/// # use dioxus::prelude::*;
/// # use dioxus_tabular::*;
/// # #[derive(Clone, PartialEq)]
/// # struct User { id: u32, name: String }
/// # impl Row for User {
/// # fn key(&self) -> impl Into<String> { self.id.to_string() }
/// # }
/// # #[derive(Clone, PartialEq)]
/// # struct NameColumn;
/// # #[derive(Clone, PartialEq)]
/// # struct IdColumn;
/// # #[derive(Clone, PartialEq)]
/// # struct UserName(String);
/// # #[derive(Clone, PartialEq)]
/// # struct UserId(u32);
/// # impl GetRowData<UserName> for User {
/// # fn get(&self) -> UserName { UserName(self.name.clone()) }
/// # }
/// # impl GetRowData<UserId> for User {
/// # fn get(&self) -> UserId { UserId(self.id) }
/// # }
/// # impl<R: Row + GetRowData<UserName>> TableColumn<R> for NameColumn {
/// # fn column_name(&self) -> String { "name".into() }
/// # fn render_header(&self, _: ColumnContext, _: Vec<Attribute>) -> Element { rsx! { th {} } }
/// # fn render_cell(&self, _: ColumnContext, row: &R, _: Vec<Attribute>) -> Element {
/// # rsx! { td { "{row.get().0}" } }
/// # }
/// # }
/// # impl<R: Row + GetRowData<UserId>> TableColumn<R> for IdColumn {
/// # fn column_name(&self) -> String { "id".into() }
/// # fn render_header(&self, _: ColumnContext, _: Vec<Attribute>) -> Element { rsx! { th {} } }
/// # fn render_cell(&self, _: ColumnContext, row: &R, _: Vec<Attribute>) -> Element {
/// # rsx! { td { "{row.get().0}" } }
/// # }
/// # }
/// # fn app() -> Element {
/// # let users = use_signal(|| vec![
/// # User { id: 1, name: "Alice".to_string() },
/// # ]);
/// // Use tuple for multiple columns (supports up to 12 columns)
/// let data = use_tabular((IdColumn, NameColumn), users.into());
/// # rsx! { table {} }
/// # }
/// ```
/// Renders table headers for all visible columns.
///
/// This component iterates through the columns and renders each header.
/// It automatically handles column reordering and visibility.
///
/// # Props
///
/// - `data`: The table data from [`use_tabular`]
/// - Additional HTML attributes can be spread onto each `<th>` element
///
/// # Example
///
/// ```
/// # use dioxus::prelude::*;
/// # use dioxus_tabular::*;
/// # #[derive(Clone, PartialEq)]
/// # struct User { id: u32 }
/// # impl Row for User {
/// # fn key(&self) -> impl Into<String> { self.id.to_string() }
/// # }
/// # #[derive(Clone, PartialEq)]
/// # struct Col;
/// # impl TableColumn<User> for Col {
/// # fn column_name(&self) -> String { "col".into() }
/// # fn render_header(&self, _: ColumnContext, _: Vec<Attribute>) -> Element { rsx! { th {} } }
/// # fn render_cell(&self, _: ColumnContext, _: &User, _: Vec<Attribute>) -> Element { rsx! { td {} } }
/// # }
/// # fn app() -> Element {
/// # let users = use_signal(|| vec![User { id: 1 }]);
/// # let data = use_tabular((Col,), users.into());
/// rsx! {
/// table {
/// thead {
/// tr {
/// // Renders all column headers
/// TableHeaders { data }
/// }
/// }
/// }
/// }
/// # }
/// ```
///
/// # With Custom Attributes
///
/// ```
/// # use dioxus::prelude::*;
/// # use dioxus_tabular::*;
/// # #[derive(Clone, PartialEq)]
/// # struct User { id: u32 }
/// # impl Row for User {
/// # fn key(&self) -> impl Into<String> { self.id.to_string() }
/// # }
/// # #[derive(Clone, PartialEq)]
/// # struct Col;
/// # impl TableColumn<User> for Col {
/// # fn column_name(&self) -> String { "col".into() }
/// # fn render_header(&self, _: ColumnContext, _: Vec<Attribute>) -> Element { rsx! { th {} } }
/// # fn render_cell(&self, _: ColumnContext, _: &User, _: Vec<Attribute>) -> Element { rsx! { td {} } }
/// # }
/// # fn app() -> Element {
/// # let users = use_signal(|| vec![User { id: 1 }]);
/// # let data = use_tabular((Col,), users.into());
/// rsx! {
/// tr {
/// TableHeaders {
/// data,
/// class: "header-cell",
/// style: "font-weight: bold;"
/// }
/// }
/// }
/// # }
/// ```
/// Renders table cells for a single row across all visible columns.
///
/// This component iterates through the columns and renders each cell for the given row.
/// It automatically handles column reordering and visibility.
///
/// # Props
///
/// - `row`: A row from iterating over `data.rows()`
/// - Additional HTML attributes can be spread onto each `<td>` element
///
/// # Example
///
/// ```
/// # use dioxus::prelude::*;
/// # use dioxus_tabular::*;
/// # #[derive(Clone, PartialEq)]
/// # struct User { id: u32 }
/// # impl Row for User {
/// # fn key(&self) -> impl Into<String> { self.id.to_string() }
/// # }
/// # #[derive(Clone, PartialEq)]
/// # struct Col;
/// # impl TableColumn<User> for Col {
/// # fn column_name(&self) -> String { "col".into() }
/// # fn render_header(&self, _: ColumnContext, _: Vec<Attribute>) -> Element { rsx! { th {} } }
/// # fn render_cell(&self, _: ColumnContext, _: &User, _: Vec<Attribute>) -> Element { rsx! { td {} } }
/// # }
/// # fn app() -> Element {
/// # let users = use_signal(|| vec![User { id: 1 }]);
/// # let data = use_tabular((Col,), users.into());
/// rsx! {
/// table {
/// tbody {
/// for row in data.rows() {
/// tr {
/// key: "{row.key()}",
/// // Renders all cells for this row
/// TableCells { row }
/// }
/// }
/// }
/// }
/// }
/// # }
/// ```
///
/// # With Custom Attributes
///
/// ```
/// # use dioxus::prelude::*;
/// # use dioxus_tabular::*;
/// # #[derive(Clone, PartialEq)]
/// # struct User { id: u32 }
/// # impl Row for User {
/// # fn key(&self) -> impl Into<String> { self.id.to_string() }
/// # }
/// # #[derive(Clone, PartialEq)]
/// # struct Col;
/// # impl TableColumn<User> for Col {
/// # fn column_name(&self) -> String { "col".into() }
/// # fn render_header(&self, _: ColumnContext, _: Vec<Attribute>) -> Element { rsx! { th {} } }
/// # fn render_cell(&self, _: ColumnContext, _: &User, _: Vec<Attribute>) -> Element { rsx! { td {} } }
/// # }
/// # fn app() -> Element {
/// # let users = use_signal(|| vec![User { id: 1 }]);
/// # let data = use_tabular((Col,), users.into());
/// rsx! {
/// for row in data.rows() {
/// tr {
/// key: "{row.key()}",
/// TableCells {
/// row,
/// class: "data-cell",
/// style: "padding: 8px;"
/// }
/// }
/// }
/// }
/// # }
/// ```