mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-26 22:20:19 +00:00
Add more features to todomvc (#928)
- toggle-all button - double click modify todo item - filter state show mouse pointer - individual todo item remove button - correct active item count
This commit is contained in:
parent
adade2d5c5
commit
3aa7349db7
1 changed files with 86 additions and 39 deletions
|
@ -7,7 +7,7 @@ fn main() {
|
||||||
dioxus_desktop::launch(app);
|
dioxus_desktop::launch(app);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||||
pub enum FilterState {
|
pub enum FilterState {
|
||||||
All,
|
All,
|
||||||
Active,
|
Active,
|
||||||
|
@ -39,17 +39,25 @@ pub fn app(cx: Scope<()>) -> Element {
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
filtered_todos.sort_unstable();
|
filtered_todos.sort_unstable();
|
||||||
|
|
||||||
let show_clear_completed = todos.values().any(|todo| todo.checked);
|
let active_todo_count = todos.values().filter(|item| !item.checked).count();
|
||||||
let items_left = filtered_todos.len();
|
let active_todo_text = match active_todo_count {
|
||||||
let item_text = match items_left {
|
|
||||||
1 => "item",
|
1 => "item",
|
||||||
_ => "items",
|
_ => "items",
|
||||||
};
|
};
|
||||||
|
|
||||||
cx.render(rsx!{
|
let show_clear_completed = todos.values().any(|todo| todo.checked);
|
||||||
|
|
||||||
|
let selected = |state| {
|
||||||
|
if *filter == state {
|
||||||
|
"selected"
|
||||||
|
} else {
|
||||||
|
"false"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
cx.render(rsx! {
|
||||||
section { class: "todoapp",
|
section { class: "todoapp",
|
||||||
style { include_str!("./assets/todomvc.css") }
|
style { include_str!("./assets/todomvc.css") }
|
||||||
div {
|
|
||||||
header { class: "header",
|
header { class: "header",
|
||||||
h1 {"todos"}
|
h1 {"todos"}
|
||||||
input {
|
input {
|
||||||
|
@ -76,19 +84,54 @@ pub fn app(cx: Scope<()>) -> Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
section {
|
||||||
|
class: "main",
|
||||||
|
if !todos.is_empty() {
|
||||||
|
rsx! {
|
||||||
|
input {
|
||||||
|
id: "toggle-all",
|
||||||
|
class: "toggle-all",
|
||||||
|
r#type: "checkbox",
|
||||||
|
onchange: move |_| {
|
||||||
|
let check = active_todo_count != 0;
|
||||||
|
for (_, item) in todos.make_mut().iter_mut() {
|
||||||
|
item.checked = check;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
checked: if active_todo_count == 0 { "true" } else { "false" },
|
||||||
|
}
|
||||||
|
label { r#for: "toggle-all" }
|
||||||
|
}
|
||||||
|
}
|
||||||
ul { class: "todo-list",
|
ul { class: "todo-list",
|
||||||
filtered_todos.iter().map(|id| rsx!(TodoEntry { key: "{id}", id: *id, todos: todos }))
|
filtered_todos.iter().map(|id| rsx!(TodoEntry {
|
||||||
|
key: "{id}",
|
||||||
|
id: *id,
|
||||||
|
todos: todos,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
(!todos.is_empty()).then(|| rsx!(
|
(!todos.is_empty()).then(|| rsx!(
|
||||||
footer { class: "footer",
|
footer { class: "footer",
|
||||||
span { class: "todo-count",
|
span { class: "todo-count",
|
||||||
strong {"{items_left} "}
|
strong {"{active_todo_count} "}
|
||||||
span {"{item_text} left"}
|
span {"{active_todo_text} left"}
|
||||||
}
|
}
|
||||||
ul { class: "filters",
|
ul { class: "filters",
|
||||||
li { class: "All", a { onclick: move |_| filter.set(FilterState::All), "All" }}
|
for (state, state_text, url) in [
|
||||||
li { class: "Active", a { onclick: move |_| filter.set(FilterState::Active), "Active" }}
|
(FilterState::All, "All", "#/"),
|
||||||
li { class: "Completed", a { onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
|
(FilterState::Active, "Active", "#/active"),
|
||||||
|
(FilterState::Completed, "Completed", "#/completed"),
|
||||||
|
] {
|
||||||
|
li {
|
||||||
|
a {
|
||||||
|
href: url,
|
||||||
|
class: selected(state),
|
||||||
|
onclick: move |_| filter.set(state),
|
||||||
|
prevent_default: "onclick",
|
||||||
|
state_text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
show_clear_completed.then(|| rsx!(
|
show_clear_completed.then(|| rsx!(
|
||||||
button {
|
button {
|
||||||
|
@ -102,7 +145,7 @@ pub fn app(cx: Scope<()>) -> Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
footer { class: "info",
|
footer { class: "info",
|
||||||
p {"Double-click to edit a todo"}
|
p { "Double-click to edit a todo" }
|
||||||
p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
|
p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
|
||||||
p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
|
p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
|
||||||
}
|
}
|
||||||
|
@ -136,13 +179,17 @@ pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
|
||||||
cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
|
cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
label {
|
label {
|
||||||
r#for: "cbg-{todo.id}",
|
r#for: "cbg-{todo.id}",
|
||||||
onclick: move |_| is_editing.set(true),
|
ondblclick: move |_| is_editing.set(true),
|
||||||
prevent_default: "onclick",
|
prevent_default: "onclick",
|
||||||
"{todo.contents}"
|
"{todo.contents}"
|
||||||
}
|
}
|
||||||
|
button {
|
||||||
|
class: "destroy",
|
||||||
|
onclick: move |_| { cx.props.todos.make_mut().remove(&todo.id); },
|
||||||
|
prevent_default: "onclick",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
is_editing.then(|| rsx!{
|
is_editing.then(|| rsx!{
|
||||||
input {
|
input {
|
||||||
|
|
Loading…
Reference in a new issue