dioxus/examples/todomvc.rs

260 lines
8.9 KiB
Rust
Raw Normal View History

2024-02-14 20:33:07 +00:00
//! The typical TodoMVC app, implemented in Dioxus.
2021-07-16 20:11:25 +00:00
use dioxus::prelude::*;
2024-01-15 19:54:17 +00:00
use std::collections::HashMap;
const STYLE: &str = asset!("./examples/assets/todomvc.css");
2021-10-11 02:27:08 +00:00
fn main() {
dioxus::launch(app);
2021-07-16 20:11:25 +00:00
}
#[derive(PartialEq, Eq, Clone, Copy)]
2024-01-20 08:11:55 +00:00
enum FilterState {
2021-07-16 20:11:25 +00:00
All,
Active,
Completed,
}
2024-01-20 08:11:55 +00:00
struct TodoItem {
checked: bool,
contents: String,
2021-07-16 20:11:25 +00:00
}
2024-01-20 08:11:55 +00:00
fn app() -> Element {
2024-02-14 20:33:07 +00:00
// We store the todos in a HashMap in a Signal.
// Each key is the id of the todo, and the value is the todo itself.
2024-01-31 02:29:49 +00:00
let mut todos = use_signal(HashMap::<u32, TodoItem>::new);
2024-02-14 20:33:07 +00:00
2024-01-31 01:59:57 +00:00
let filter = use_signal(|| FilterState::All);
2021-07-16 20:11:25 +00:00
2024-02-14 20:33:07 +00:00
// We use a simple memoized signal to calculate the number of active todos.
// Whenever the todos change, the active_todo_count will be recalculated.
2024-01-15 19:54:17 +00:00
let active_todo_count =
2024-01-21 07:32:12 +00:00
use_memo(move || todos.read().values().filter(|item| !item.checked).count());
2021-07-16 20:11:25 +00:00
2024-02-14 20:33:07 +00:00
// We use a memoized signal to filter the todos based on the current filter state.
// Whenever the todos or filter change, the filtered_todos will be recalculated.
// Note that we're only storing the IDs of the todos, not the todos themselves.
2024-01-21 07:32:12 +00:00
let filtered_todos = use_memo(move || {
let mut filtered_todos = todos
.read()
2024-01-15 19:54:17 +00:00
.iter()
.filter(|(_, item)| match filter() {
2024-01-15 19:54:17 +00:00
FilterState::All => true,
FilterState::Active => !item.checked,
FilterState::Completed => item.checked,
})
.map(|f| *f.0)
.collect::<Vec<_>>();
filtered_todos.sort_unstable();
2021-07-16 20:11:25 +00:00
2024-01-15 19:54:17 +00:00
filtered_todos
});
2024-02-14 20:33:07 +00:00
// Toggle all the todos to the opposite of the current state.
// If all todos are checked, uncheck them all. If any are unchecked, check them all.
2024-01-31 01:59:57 +00:00
let toggle_all = move |_| {
2024-01-20 08:11:55 +00:00
let check = active_todo_count() != 0;
for (_, item) in todos.write().iter_mut() {
item.checked = check;
}
};
2024-01-16 19:18:46 +00:00
rsx! {
document::Link { rel: "stylesheet", href: STYLE }
section { class: "todoapp",
2024-01-15 19:54:17 +00:00
TodoHeader { todos }
section { class: "main",
if !todos.read().is_empty() {
input {
id: "toggle-all",
class: "toggle-all",
r#type: "checkbox",
2024-01-20 08:11:55 +00:00
onchange: toggle_all,
2024-04-17 22:08:38 +00:00
checked: active_todo_count() == 0
2021-09-24 06:10:54 +00:00
}
label { r#for: "toggle-all" }
2021-09-24 06:10:54 +00:00
}
2024-02-14 20:33:07 +00:00
// Render the todos using the filtered_todos signal
// We pass the ID into the TodoEntry component so it can access the todo from the todos signal.
// Since we store the todos in a signal too, we also need to send down the todo list
ul { class: "todo-list",
2024-01-20 08:11:55 +00:00
for id in filtered_todos() {
2024-01-15 19:54:17 +00:00
TodoEntry { key: "{id}", id, todos }
}
}
2024-02-14 20:33:07 +00:00
// We only show the footer if there are todos.
if !todos.read().is_empty() {
2024-01-15 19:54:17 +00:00
ListFooter { active_todo_count, todos, filter }
}
}
2021-07-16 20:11:25 +00:00
}
2024-02-14 20:33:07 +00:00
// A simple info footer
footer { class: "info",
p { "Double-click to edit a todo" }
2024-04-17 22:08:38 +00:00
p {
"Created by "
a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }
}
p {
"Part of "
a { href: "http://todomvc.com", "TodoMVC" }
}
2024-02-14 20:33:07 +00:00
}
2024-01-14 05:12:21 +00:00
}
2023-10-19 14:26:17 +00:00
}
2024-01-15 19:54:17 +00:00
#[component]
2024-01-20 08:11:55 +00:00
fn TodoHeader(mut todos: Signal<HashMap<u32, TodoItem>>) -> Element {
let mut draft = use_signal(|| "".to_string());
2024-01-15 19:54:17 +00:00
let mut todo_id = use_signal(|| 0);
let onkeydown = move |evt: KeyboardEvent| {
if evt.key() == Key::Enter && !draft.read().is_empty() {
let id = todo_id();
2024-01-15 19:54:17 +00:00
let todo = TodoItem {
checked: false,
contents: draft.to_string(),
};
todos.write().insert(id, todo);
todo_id += 1;
draft.set("".to_string());
}
};
2023-10-19 14:26:17 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
2023-10-19 14:26:17 +00:00
header { class: "header",
h1 { "todos" }
input {
class: "new-todo",
placeholder: "What needs to be done?",
value: "{draft}",
autofocus: "true",
oninput: move |evt| draft.set(evt.value()),
2024-04-17 22:08:38 +00:00
onkeydown
2023-10-19 14:26:17 +00:00
}
}
2024-01-14 05:12:21 +00:00
}
}
2021-07-16 20:11:25 +00:00
2024-02-14 20:33:07 +00:00
/// A single todo entry
/// This takes the ID of the todo and the todos signal as props
/// We can use these together to memoize the todo contents and checked state
2024-01-15 19:54:17 +00:00
#[component]
2024-01-20 08:11:55 +00:00
fn TodoEntry(mut todos: Signal<HashMap<u32, TodoItem>>, id: u32) -> Element {
let mut is_editing = use_signal(|| false);
2024-02-14 20:33:07 +00:00
// To avoid re-rendering this component when the todo list changes, we isolate our reads to memos
// This way, the component will only re-render when the contents of the todo change, or when the editing state changes.
// This does involve taking a local clone of the todo contents, but it allows us to prevent this component from re-rendering
2024-01-21 07:32:12 +00:00
let checked = use_memo(move || todos.read().get(&id).unwrap().checked);
let contents = use_memo(move || todos.read().get(&id).unwrap().contents.clone());
2021-07-16 20:11:25 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
2024-02-14 20:33:07 +00:00
li {
// Dioxus lets you use if statements in rsx to conditionally render attributes
// These will get merged into a single class attribute
class: if checked() { "completed" },
class: if is_editing() { "editing" },
// Some basic controls for the todo
2022-03-05 20:25:09 +00:00
div { class: "view",
input {
class: "toggle",
r#type: "checkbox",
2024-01-15 19:54:17 +00:00
id: "cbg-{id}",
checked: "{checked}",
2024-04-17 22:08:38 +00:00
oninput: move |evt| todos.write().get_mut(&id).unwrap().checked = evt.checked()
2022-03-05 20:25:09 +00:00
}
label {
2024-01-15 19:54:17 +00:00
r#for: "cbg-{id}",
ondoubleclick: move |_| is_editing.set(true),
onclick: |evt| evt.prevent_default(),
2024-01-15 19:54:17 +00:00
"{contents}"
}
button {
class: "destroy",
onclick: move |evt| {
evt.prevent_default();
2024-04-17 22:08:38 +00:00
todos.write().remove(&id);
},
}
2021-07-16 20:11:25 +00:00
}
2024-02-14 20:33:07 +00:00
// Only render the actual input if we're editing
if is_editing() {
2022-03-05 20:25:09 +00:00
input {
class: "edit",
2024-01-15 19:54:17 +00:00
value: "{contents}",
oninput: move |evt| todos.write().get_mut(&id).unwrap().contents = evt.value(),
2022-03-05 20:25:09 +00:00
autofocus: "true",
onfocusout: move |_| is_editing.set(false),
onkeydown: move |evt| {
match evt.key() {
Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
2022-03-05 20:25:09 +00:00
_ => {}
}
2024-01-15 19:54:17 +00:00
}
2022-03-05 20:25:09 +00:00
}
}
}
2024-01-14 05:12:21 +00:00
}
2021-07-16 20:11:25 +00:00
}
2023-10-19 14:26:17 +00:00
2024-01-15 19:54:17 +00:00
#[component]
2024-01-20 08:11:55 +00:00
fn ListFooter(
mut todos: Signal<HashMap<u32, TodoItem>>,
2024-01-15 19:54:17 +00:00
active_todo_count: ReadOnlySignal<usize>,
mut filter: Signal<FilterState>,
2024-01-15 19:54:17 +00:00
) -> Element {
2024-02-14 20:33:07 +00:00
// We use a memoized signal to calculate whether we should show the "Clear completed" button.
// This will recompute whenever the todos change, and if the value is true, the button will be shown.
2024-01-21 07:32:12 +00:00
let show_clear_completed = use_memo(move || todos.read().values().any(|todo| todo.checked));
2023-10-19 14:26:17 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
footer { class: "footer",
span { class: "todo-count",
strong { "{active_todo_count} " }
span {
match active_todo_count() {
1 => "item",
_ => "items",
2024-01-15 19:54:17 +00:00
}
" left"
2024-01-15 19:54:17 +00:00
}
}
ul { class: "filters",
for (state , state_text , url) in [
(FilterState::All, "All", "#/"),
(FilterState::Active, "Active", "#/active"),
(FilterState::Completed, "Completed", "#/completed"),
] {
li {
a {
href: url,
class: if filter() == state { "selected" },
onclick: move |evt| {
evt.prevent_default();
filter.set(state)
},
{state_text}
2023-10-19 14:26:17 +00:00
}
}
}
}
if show_clear_completed() {
button {
class: "clear-completed",
onclick: move |_| todos.write().retain(|_, todo| !todo.checked),
"Clear completed"
}
2023-10-19 14:26:17 +00:00
}
}
}
2023-10-19 14:26:17 +00:00
}