dioxus/examples/todomvc.rs

163 lines
5.7 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;
2021-10-11 02:27:08 +00:00
fn main() {
dioxus_desktop::launch(app);
2021-07-16 20:11:25 +00:00
}
#[derive(PartialEq, Eq)]
2021-07-16 20:11:25 +00:00
pub enum FilterState {
All,
Active,
Completed,
}
#[derive(Debug, PartialEq, Eq, Clone)]
2021-07-16 20:11:25 +00:00
pub struct TodoItem {
pub id: u32,
pub checked: bool,
pub contents: String,
}
pub fn app(cx: Scope<()>) -> Element {
2022-03-01 07:50:03 +00:00
let todos = use_state(&cx, im_rc::HashMap::<u32, TodoItem>::default);
let filter = use_state(&cx, || FilterState::All);
let draft = use_state(&cx, || "".to_string());
let todo_id = use_state(&cx, || 0);
2021-07-16 20:11:25 +00:00
// Filter the todos based on the filter state
let mut filtered_todos = todos
2021-07-16 20:11:25 +00:00
.iter()
2022-03-01 07:50:03 +00:00
.filter(|(_, item)| match **filter {
2021-07-16 20:11:25 +00:00
FilterState::All => true,
FilterState::Active => !item.checked,
FilterState::Completed => item.checked,
})
.map(|f| *f.0)
2021-07-16 20:11:25 +00:00
.collect::<Vec<_>>();
filtered_todos.sort_unstable();
2021-07-16 20:11:25 +00:00
let show_clear_completed = todos.values().any(|todo| todo.checked);
let items_left = filtered_todos.len();
2021-07-16 20:11:25 +00:00
let item_text = match items_left {
1 => "item",
_ => "items",
};
cx.render(rsx!{
section { class: "todoapp",
style { include_str!("./assets/todomvc.css") }
div {
header { class: "header",
h1 {"todos"}
input {
class: "new-todo",
placeholder: "What needs to be done?",
value: "{draft}",
autofocus: "true",
2022-03-01 07:50:03 +00:00
oninput: move |evt| draft.set(evt.value.clone()),
onkeydown: move |evt| {
if evt.key() == Key::Enter && !draft.is_empty() {
2022-03-01 07:50:03 +00:00
todos.make_mut().insert(
**todo_id,
2022-01-16 20:56:48 +00:00
TodoItem {
2022-03-01 07:50:03 +00:00
id: **todo_id,
2022-01-16 20:56:48 +00:00
checked: false,
2022-03-01 07:50:03 +00:00
contents: draft.to_string(),
2022-01-16 20:56:48 +00:00
},
);
2022-03-01 07:50:03 +00:00
*todo_id.make_mut() += 1;
draft.set("".to_string());
}
}
2021-09-24 06:10:54 +00:00
}
}
ul { class: "todo-list",
filtered_todos.iter().map(|id| rsx!(TodoEntry { key: "{id}", id: *id, todos: todos }))
}
(!todos.is_empty()).then(|| rsx!(
footer { class: "footer",
2022-01-03 06:20:05 +00:00
span { class: "todo-count",
strong {"{items_left} "}
span {"{item_text} left"}
}
ul { class: "filters",
2022-03-01 07:50:03 +00:00
li { class: "All", a { onclick: move |_| filter.set(FilterState::All), "All" }}
li { class: "Active", a { onclick: move |_| filter.set(FilterState::Active), "Active" }}
li { class: "Completed", a { onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
}
show_clear_completed.then(|| rsx!(
2022-01-03 06:20:05 +00:00
button {
class: "clear-completed",
2022-03-01 07:50:03 +00:00
onclick: move |_| todos.make_mut().retain(|_, todo| !todo.checked),
"Clear completed"
}
))
}
))
}
2021-07-16 20:11:25 +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" }}
}
})
}
2021-07-16 20:11:25 +00:00
#[derive(Props)]
pub struct TodoEntryProps<'a> {
2022-03-01 07:50:03 +00:00
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
id: u32,
2021-07-16 20:11:25 +00:00
}
pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
2022-03-01 07:50:03 +00:00
let is_editing = use_state(&cx, || false);
2022-01-26 02:41:40 +00:00
2022-03-01 07:50:03 +00:00
let todos = cx.props.todos.get();
2022-01-26 02:41:40 +00:00
let todo = &todos[&cx.props.id];
let completed = if todo.checked { "completed" } else { "" };
2022-03-01 07:50:03 +00:00
let editing = if **is_editing { "editing" } else { "" };
2021-07-16 20:11:25 +00:00
2022-03-05 20:25:09 +00:00
cx.render(rsx!{
li {
class: "{completed} {editing}",
div { class: "view",
input {
class: "toggle",
r#type: "checkbox",
id: "cbg-{todo.id}",
checked: "{todo.checked}",
oninput: move |evt| {
cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
}
}
label {
r#for: "cbg-{todo.id}",
onclick: move |_| is_editing.set(true),
prevent_default: "onclick",
"{todo.contents}"
}
2021-07-16 20:11:25 +00:00
}
2022-03-05 20:25:09 +00:00
is_editing.then(|| rsx!{
input {
class: "edit",
value: "{todo.contents}",
oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
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
_ => {}
}
},
}
})
}
2021-09-24 06:10:54 +00:00
})
2021-07-16 20:11:25 +00:00
}