|
| 1 | +#![deny(missing_docs)] |
| 2 | +//! Simple showcase example. |
| 3 | +
|
| 4 | +use chrono::NaiveDate; |
| 5 | +use derive_more::{Deref, DerefMut}; |
| 6 | +use leptos::*; |
| 7 | +use leptos_struct_table::*; |
| 8 | +use std::rc::Rc; |
| 9 | + |
| 10 | +/// This generates the component BookTable |
| 11 | +#[derive(TableRow)] |
| 12 | +#[table(sortable, impl_vec_data_provider, row_type = "RcBook")] |
| 13 | +pub struct Book { |
| 14 | + /// Title of the book. |
| 15 | + pub title: String, |
| 16 | + /// Author of the book. |
| 17 | + pub author: String, |
| 18 | + /// Date when book has been published. |
| 19 | + pub publish_date: Option<NaiveDate>, |
| 20 | + /// Description of the book. Optional. |
| 21 | + #[table(none_value = "-")] |
| 22 | + pub description: Option<String>, |
| 23 | +} |
| 24 | + |
| 25 | +/// New-type pattern because otherwise the impl TableRow doesn't work because of orphan rules. |
| 26 | +#[derive(Deref, DerefMut, Clone)] |
| 27 | +pub struct RcBook(Rc<Book>); |
| 28 | + |
| 29 | +fn main() { |
| 30 | + _ = console_log::init_with_level(log::Level::Debug); |
| 31 | + console_error_panic_hook::set_once(); |
| 32 | + |
| 33 | + mount_to_body(|| { |
| 34 | + let rows = vec![ |
| 35 | + RcBook(Rc::new(Book { |
| 36 | + title: "The Great Gatsby".to_string(), |
| 37 | + author: "F. Scott Fitzgerald".to_string(), |
| 38 | + publish_date: Some(NaiveDate::from_ymd_opt(1925, 4, 10).unwrap()), |
| 39 | + description: Some( |
| 40 | + "A story of wealth, love, and the American Dream in the 1920s.".to_string(), |
| 41 | + ), |
| 42 | + })), |
| 43 | + RcBook(Rc::new(Book { |
| 44 | + title: "The Grapes of Wrath".to_string(), |
| 45 | + author: "John Steinbeck".to_string(), |
| 46 | + publish_date: Some(NaiveDate::from_ymd_opt(1939, 4, 14).unwrap()), |
| 47 | + description: None, |
| 48 | + })), |
| 49 | + RcBook(Rc::new(Book { |
| 50 | + title: "Nineteen Eighty-Four".to_string(), |
| 51 | + author: "George Orwell".to_string(), |
| 52 | + publish_date: Some(NaiveDate::from_ymd_opt(1949, 6, 8).unwrap()), |
| 53 | + description: None, |
| 54 | + })), |
| 55 | + RcBook(Rc::new(Book { |
| 56 | + title: "Ulysses".to_string(), |
| 57 | + author: "James Joyce".to_string(), |
| 58 | + publish_date: Some(NaiveDate::from_ymd_opt(1922, 2, 2).unwrap()), |
| 59 | + description: None, |
| 60 | + })), |
| 61 | + ]; |
| 62 | + |
| 63 | + view! { |
| 64 | + <table> |
| 65 | + <TableContent rows/> |
| 66 | + </table> |
| 67 | + } |
| 68 | + }) |
| 69 | +} |
0 commit comments