dioxus_tabular/row.rs
1/// Defines the unique key and identity of each table row.
2///
3/// This trait must be implemented for any type you want to use as a row in a table.
4/// The key is used by Dioxus for efficient rendering and should uniquely identify each row.
5///
6/// # Example
7///
8/// ```
9/// use dioxus_tabular::Row;
10///
11/// #[derive(Clone, PartialEq)]
12/// struct User {
13/// id: u32,
14/// name: String,
15/// }
16///
17/// impl Row for User {
18/// fn key(&self) -> impl Into<String> {
19/// self.id.to_string()
20/// }
21/// }
22/// ```
23pub trait Row: PartialEq + Clone + 'static {
24 /// Returns a unique key for this row.
25 ///
26 /// The key must be unique within the table and stable across re-renders.
27 fn key(&self) -> impl Into<String>;
28}
29
30/// Provides typed access to row data.
31///
32/// This trait allows columns to extract specific data from rows in a type-safe way.
33/// Implement this trait for each piece of data you want to access in your columns.
34///
35/// # Type Parameter
36///
37/// - `T`: The type of data to extract from the row
38///
39/// # Example
40///
41/// ```
42/// use dioxus_tabular::GetRowData;
43///
44/// #[derive(Clone, PartialEq)]
45/// struct User {
46/// id: u32,
47/// name: String,
48/// email: String,
49/// }
50///
51/// // Define accessor types
52/// #[derive(Clone, PartialEq)]
53/// struct UserName(String);
54///
55/// #[derive(Clone, PartialEq)]
56/// struct UserEmail(String);
57///
58/// // Implement GetRowData for each accessor
59/// impl GetRowData<UserName> for User {
60/// fn get(&self) -> UserName {
61/// UserName(self.name.clone())
62/// }
63/// }
64///
65/// impl GetRowData<UserEmail> for User {
66/// fn get(&self) -> UserEmail {
67/// UserEmail(self.email.clone())
68/// }
69/// }
70/// ```
71pub trait GetRowData<T> {
72 /// Extracts data of type `T` from this row.
73 fn get(&self) -> T;
74}