dioxus/examples/todomvc.rs

108 lines
3.1 KiB
Rust
Raw Normal View History

#![allow(non_upper_case_globals, non_snake_case)]
2021-07-16 20:11:25 +00:00
use dioxus::prelude::*;
use im_rc::HashMap;
use std::rc::Rc;
2021-10-11 02:27:08 +00:00
fn main() {
dioxus::desktop::launch(App, |c| c);
2021-07-16 20:11:25 +00:00
}
#[derive(PartialEq)]
pub enum FilterState {
All,
Active,
Completed,
}
#[derive(Debug, PartialEq)]
pub struct TodoItem {
pub id: u32,
pub checked: bool,
pub contents: String,
}
2021-10-16 21:04:28 +00:00
const STYLE: &str = include_str!("./assets/todomvc.css");
2021-10-16 21:37:28 +00:00
const App: FC<()> = |(cx, props)| {
2021-07-16 20:11:25 +00:00
let draft = use_state(cx, || "".to_string());
let todos = use_state(cx, || HashMap::<u32, Rc<TodoItem>>::new());
let filter = use_state(cx, || FilterState::All);
let todolist = todos
.iter()
.filter(|(id, item)| match *filter {
FilterState::All => true,
FilterState::Active => !item.checked,
FilterState::Completed => item.checked,
})
.map(|(id, todo)| {
rsx!(TodoEntry {
key: "{id}",
todo: todo.clone()
})
})
.collect::<Vec<_>>();
let items_left = todolist.len();
let item_text = match items_left {
1 => "item",
_ => "items",
};
2021-09-24 06:10:54 +00:00
rsx!(cx, div { id: "app"
style {"{STYLE}"}
div {
header { class: "header"
h1 {"todos"}
input {
class: "new-todo"
placeholder: "What needs to be done?"
value: "{draft}"
oninput: move |evt| draft.set(evt.value.clone())
2021-07-16 20:11:25 +00:00
}
}
2021-09-24 06:10:54 +00:00
{todolist}
{(!todos.is_empty()).then(|| rsx!(
footer {
span { strong {"{items_left}"} span {"{item_text} left"} }
ul { class: "filters"
li { class: "All", a { href: "", onclick: move |_| filter.set(FilterState::All), "All" }}
li { class: "Active", a { href: "active", onclick: move |_| filter.set(FilterState::Active), "Active" }}
li { class: "Completed", a { href: "completed", onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
}
}
))}
}
footer { class: "info"
p {"Double-click to edit a todo"}
p { "Created by ", a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }}
p { "Part of ", a { "TodoMVC", href: "http://todomvc.com" }}
2021-07-16 20:11:25 +00:00
}
})
};
#[derive(PartialEq, Props)]
pub struct TodoEntryProps {
2021-09-24 06:10:54 +00:00
todo: Rc<TodoItem>,
2021-07-16 20:11:25 +00:00
}
2021-10-16 21:37:28 +00:00
pub fn TodoEntry((cx, props): Component<TodoEntryProps>) -> DomTree {
2021-07-16 20:11:25 +00:00
let is_editing = use_state(cx, || false);
2021-09-24 06:10:54 +00:00
let contents = use_state(cx, || String::from(""));
let todo = &props.todo;
2021-07-16 20:11:25 +00:00
2021-09-24 06:10:54 +00:00
rsx!(cx, li {
"{todo.id}"
input {
class: "toggle"
r#type: "checkbox"
"{todo.checked}"
}
{is_editing.then(|| rsx!{
2021-07-16 20:11:25 +00:00
input {
2021-09-24 06:10:54 +00:00
value: "{contents}"
oninput: move |evt| contents.set(evt.value.clone())
2021-07-16 20:11:25 +00:00
}
2021-09-24 06:10:54 +00:00
})}
})
2021-07-16 20:11:25 +00:00
}