dioxus/examples/todomvc.rs

230 lines
6.9 KiB
Rust
Raw Normal View History

#![allow(non_snake_case)]
2021-07-16 20:11:25 +00:00
use dioxus::prelude::*;
use dioxus_elements::input_data::keyboard_types::Key;
2024-01-15 19:54:17 +00:00
use std::collections::HashMap;
2021-10-11 02:27:08 +00:00
fn main() {
2024-01-20 08:11:55 +00:00
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
#[derive(Debug, PartialEq, Eq)]
struct TodoItem {
id: u32,
checked: bool,
contents: String,
2021-07-16 20:11:25 +00:00
}
2024-01-20 08:11:55 +00:00
const STYLE: &str = include_str!("./assets/todomvc.css");
fn app() -> Element {
let mut todos = use_signal(|| HashMap::<u32, TodoItem>::new());
let mut filter = use_signal(|| FilterState::All);
2021-07-16 20:11:25 +00:00
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-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-01-20 08:11:55 +00:00
let mut toggle_all = move |_| {
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! {
section { class: "todoapp",
2024-01-20 08:11:55 +00:00
style { {STYLE} }
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,
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
}
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 }
}
}
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
}
2023-10-19 14:26:17 +00:00
PageFooter {}
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 {
id,
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",
2024-01-15 19:54:17 +00:00
oninput: move |evt| draft.set(evt.value().clone()),
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-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-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-01-16 18:47:23 +00:00
li { class: if checked() { "completed" }, class: if is_editing() { "editing" },
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}",
oninput: move |evt| todos.write().get_mut(&id).unwrap().checked = evt.value().parse().unwrap(),
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),
2022-03-05 20:25:09 +00:00
prevent_default: "onclick",
2024-01-15 19:54:17 +00:00
"{contents}"
}
button {
class: "destroy",
2024-01-15 19:54:17 +00:00
onclick: move |_| { todos.write().remove(&id); },
prevent_default: "onclick"
}
2021-07-16 20:11:25 +00:00
}
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-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! {
2023-10-19 14:26:17 +00:00
footer { class: "footer",
span { class: "todo-count",
strong { "{active_todo_count} " }
2024-01-15 19:54:17 +00:00
span {
match active_todo_count() {
2024-01-15 19:54:17 +00:00
1 => "item",
_ => "items",
}
" left"
}
2023-10-19 14:26:17 +00:00
}
ul { class: "filters",
for (state , state_text , url) in [
(FilterState::All, "All", "#/"),
(FilterState::Active, "Active", "#/active"),
(FilterState::Completed, "Completed", "#/completed"),
] {
2023-10-19 14:26:17 +00:00
li {
a {
href: url,
class: if filter() == state { "selected" },
2024-01-15 19:54:17 +00:00
onclick: move |_| filter.set(state),
2023-10-19 14:26:17 +00:00
prevent_default: "onclick",
{state_text}
2023-10-19 14:26:17 +00:00
}
}
}
}
if show_clear_completed() {
button {
class: "clear-completed",
2024-01-15 19:54:17 +00:00
onclick: move |_| todos.write().retain(|_, todo| !todo.checked),
"Clear completed"
}
2023-10-19 14:26:17 +00:00
}
}
2024-01-14 05:12:21 +00:00
}
2023-10-19 14:26:17 +00:00
}
2024-01-20 08:11:55 +00:00
fn PageFooter() -> Element {
2024-01-16 19:18:46 +00:00
rsx! {
2023-10-19 14:26:17 +00:00
footer { class: "info",
p { "Double-click to edit a todo" }
p {
"Created by "
a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }
}
p {
"Part of "
a { href: "http://todomvc.com", "TodoMVC" }
}
2023-10-19 14:26:17 +00:00
}
2024-01-14 05:12:21 +00:00
}
2023-10-19 14:26:17 +00:00
}