-
-
Notifications
You must be signed in to change notification settings - Fork 36
Replace usize column index with a generic #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ToxicMushroom
wants to merge
3
commits into
Synphonyte:master
Choose a base branch
from
ToxicMushroom:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| name = "column_index_type" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| leptos = { version = "0.8", features = ["csr"] } | ||
| leptos-struct-table = { path = "../..", features = ["chrono"] } | ||
| chrono = { version = "0.4" } | ||
| console_error_panic_hook = "0.1" | ||
| console_log = "1" | ||
| log = "0.4" | ||
| derive_more = { version = "0.99" } | ||
|
|
||
| [dev-dependencies] | ||
| wasm-bindgen = "0.2" | ||
| wasm-bindgen-test = "0.3.0" | ||
| web-sys = "0.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ### A table example with a generated enum as column index type. | ||
| Two custom renders are also used to show where the generated enum type needs to fill in and how it can be used. | ||
|
|
||
| To make this example work, you must download / fork the whole repo because this is in the Cargo.toml: `leptos-struct-table = { path = "../.." }`. | ||
|
|
||
| If you don't have it installed already, install [Trunk](https://trunkrs.dev/) | ||
| as well as the wasm32-unknown-unknown target: | ||
|
|
||
| ```bash | ||
| cargo install trunk | ||
| rustup target add wasm32-unknown-unknown | ||
| ``` | ||
|
|
||
| Then, to run this example, execute in a terminal: | ||
|
|
||
| ```bash | ||
| trunk serve --open | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head></head> | ||
| <body></body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| #![deny(missing_docs)] | ||
| //! Column index showcase example. | ||
|
|
||
| use ::chrono::NaiveDate; | ||
| use derive_more::{Deref, DerefMut}; | ||
| use leptos::prelude::*; | ||
| use leptos_struct_table::*; | ||
| use std::sync::Arc; | ||
| use leptos::web_sys; | ||
| use std::marker::PhantomData; | ||
|
|
||
| /// Custom row renderer that adds a link to the end of the row | ||
| #[allow(unused_variables, non_snake_case)] | ||
| pub fn CustomTableRowRenderer( | ||
| // The class attribute for the row element. Generated by the classes provider. | ||
| class: Signal<String>, | ||
| // The row to render. | ||
| row: RwSignal<Book>, | ||
| // The index of the row. Starts at 0 for the first body row. | ||
| index: usize, | ||
| // The selected state of the row. True, when the row is selected. | ||
| selected: Signal<bool>, | ||
| // Event handler callback when this row is selected | ||
| on_select: EventHandler<web_sys::MouseEvent>, | ||
| // Phantom data needs the column index type (a generated enum in this example) | ||
| _phantom: PhantomData<BookColumn> | ||
| ) -> impl IntoView { | ||
|
|
||
| view! { | ||
| <tr class=class on:click=move |mouse_event| on_select.run(mouse_event)> | ||
| {TableRow::render_row(row, index)} | ||
| <td> | ||
| <a href=move || format!("/some-path/{}", row.read().title)>"Some link"</a> | ||
| </td> | ||
| </tr> | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #[component] | ||
| #[allow(unused_variables)] | ||
| pub fn ColorCellRenderer( | ||
| /// Base cell rendere classes | ||
| #[prop(into)] class: String, | ||
| /// Value of this cell | ||
| value: Signal<String>, | ||
| /// Row containing this cell | ||
| row: RwSignal<Book>, | ||
| /// Column Index of the cell. (variant of a generated enum in this example) | ||
| index: BookColumn, | ||
| ) -> impl IntoView { | ||
| match index { | ||
| BookColumn::Title => view! { <span style="color: blue;">{ value }</span> }, | ||
| BookColumn::Author => view! { <span style="color: red;">{ value }</span> }, | ||
| BookColumn::PublishDate => view! { <span style="color: green;">{ value }</span> }, | ||
| BookColumn::Description => view! { <span style="color: yellow;">{ value }</span> } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /// This generates the component BookTable | ||
| #[derive(TableRow, Clone)] | ||
| #[table(sortable, impl_vec_data_provider, column_index_type = "Enum")] | ||
| pub struct Book { | ||
| /// Title of the book. | ||
| pub title: String, | ||
| /// Author of the book. | ||
| #[table(renderer = "ColorCellRenderer")] | ||
| pub author: String, | ||
| /// Date when book has been published. | ||
| pub publish_date: Option<NaiveDate>, | ||
| /// Description of the book. Optional. | ||
| #[table(none_value = "-")] | ||
| pub description: Option<String>, | ||
| } | ||
|
|
||
| /// New-type pattern because otherwise the impl TableRow doesn't work because of orphan rules. | ||
| #[derive(Deref, DerefMut, Clone)] | ||
| pub struct ArcBook(Arc<Book>); | ||
|
|
||
| fn main() { | ||
| _ = console_log::init_with_level(log::Level::Debug); | ||
| console_error_panic_hook::set_once(); | ||
|
|
||
| mount_to_body(|| { | ||
| let rows = vec![ | ||
| Book { | ||
| title: "The Great Gatsby".to_string(), | ||
| author: "F. Scott Fitzgerald".to_string(), | ||
| publish_date: Some(NaiveDate::from_ymd_opt(1925, 4, 10).unwrap()), | ||
| description: Some( | ||
| "A story of wealth, love, and the American Dream in the 1920s.".to_string(), | ||
| ), | ||
| }, | ||
| Book { | ||
| title: "The Grapes of Wrath".to_string(), | ||
| author: "John Steinbeck".to_string(), | ||
| publish_date: Some(NaiveDate::from_ymd_opt(1939, 4, 14).unwrap()), | ||
| description: None, | ||
| }, | ||
| Book { | ||
| title: "Nineteen Eighty-Four".to_string(), | ||
| author: "George Orwell".to_string(), | ||
| publish_date: Some(NaiveDate::from_ymd_opt(1949, 6, 8).unwrap()), | ||
| description: None, | ||
| }, | ||
| Book { | ||
| title: "Ulysses".to_string(), | ||
| author: "James Joyce".to_string(), | ||
| publish_date: Some(NaiveDate::from_ymd_opt(1922, 2, 2).unwrap()), | ||
| description: None, | ||
| }, | ||
| ]; | ||
|
|
||
| view! { | ||
| <table> | ||
| <TableContent rows scroll_container="html" /> | ||
| </table> | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the component isn't generic, this shouldn't be necessary.
I think instead of adding the
PhantomDatato the renderer function's signature we should add it as a field to the generated struct in the macrorenderer_fn!.