-
-
Notifications
You must be signed in to change notification settings - Fork 599
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
feat: basic TODO app; no bells, no whistles #1167
Open
jdevries3133
wants to merge
1
commit into
leptos-rs:main
Choose a base branch
from
jdevries3133:basic_todo_app
base: main
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
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 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 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,9 @@ | ||
[package] | ||
name = "todo_app_basic" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
leptos = { path = "../../leptos" } |
This file contains 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,9 @@ | ||
[tasks.check-debug] | ||
command = "cargo" | ||
args = ["+nightly", "check-all-features"] | ||
install_crate = "cargo-all-features" | ||
|
||
[tasks.check-release] | ||
command = "cargo" | ||
args = ["+nightly", "check-all-features", "--release"] | ||
install_crate = "cargo-all-features" |
This file contains 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,14 @@ | ||
# Basic TODO App | ||
|
||
This is the client-side only TODO App you learned in Grade school. | ||
|
||
## Running This Example | ||
|
||
Just use trunk from this folder! | ||
|
||
```bash | ||
trunk serve --open | ||
``` | ||
|
||
If you don't have `trunk` installed, | ||
[click here for install instructions.](https://trunkrs.dev/) |
This file contains 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,11 @@ | ||
<html> | ||
<head> | ||
<style> | ||
* { | ||
box-sizing: border-box; | ||
font-family: Arial; | ||
} | ||
</style> | ||
</head> | ||
<body></body> | ||
</html> |
This file contains 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,3 @@ | ||
[toolchain] | ||
channel = "nightly" | ||
targets = ["wasm32-unknown-unknown"] |
This file contains 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,86 @@ | ||
use leptos::*; | ||
|
||
fn main() { | ||
mount_to_body(|cx| view! { cx, <App /> }); | ||
} | ||
|
||
#[derive(Eq, Hash, PartialEq, Clone, Debug)] | ||
struct TodoItem { | ||
id: u32, | ||
name: String, | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
struct TodoGenerator { | ||
next_id: u32, | ||
} | ||
|
||
impl TodoGenerator { | ||
fn new() -> Self { | ||
TodoGenerator { next_id: 1 } | ||
} | ||
fn get_todo(&mut self, name: String) -> TodoItem { | ||
let next_id = self.next_id; | ||
self.next_id += 1; | ||
TodoItem { name, id: next_id } | ||
} | ||
} | ||
|
||
#[component] | ||
fn App(cx: Scope) -> impl IntoView { | ||
let (draft_item_name, set_draft_item_name) = | ||
create_signal(cx, String::new()); | ||
let (todos, set_todos) = create_signal::<Vec<TodoItem>>(cx, vec![]); | ||
let mut todo_generator = TodoGenerator::new(); | ||
|
||
let delete_todo = move |id: u32| { | ||
set_todos.update(move |todos| { | ||
todos.retain(|t| t.id != id); | ||
}); | ||
}; | ||
|
||
view! { | ||
cx, | ||
<form on:submit=move |e: ev::SubmitEvent| { | ||
e.prevent_default(); | ||
set_todos.update(|todos| { | ||
todos.push(todo_generator.get_todo(draft_item_name())); | ||
set_draft_item_name(String::new()) | ||
}); | ||
}> | ||
<label for="todo-name">"Todo Name: "</label> | ||
<input | ||
id="todo-name" | ||
type="text" | ||
prop:value=draft_item_name | ||
on:input=move |e| { | ||
let value = event_target_value(&e); | ||
set_draft_item_name(value); | ||
} | ||
/> | ||
</form> | ||
<For | ||
each=todos | ||
key=move |todo| todo.id | ||
view=move |cx, todo| view! { cx, <TodoItem todo=todo delete_todo=Box::new(delete_todo) /> } | ||
/> | ||
} | ||
} | ||
|
||
#[component] | ||
fn TodoItem( | ||
cx: Scope, | ||
todo: TodoItem, | ||
delete_todo: Box<dyn Fn(u32)>, | ||
) -> impl IntoView { | ||
// Note that we could have structured our todo list as a vector of signals. | ||
// That would be a bit noisier, but would allow us to implement inline | ||
// editing of TODO items inside this component, where we'd recieve a signal | ||
// here instead of a TODO item, such that we could write to that signal | ||
// to propagate changes throughout our app! | ||
view! { | ||
cx, | ||
<p>{todo.name.clone()}</p> | ||
<button on:click=move |_| delete_todo(todo.id)>"Done"</button> | ||
} | ||
} |
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.
I'd split the ID, the Todo and the latest ID state.
e.g.: