dioxus/packages/web/examples/list.rs

174 lines
5.5 KiB
Rust
Raw Normal View History

2021-03-26 19:50:28 +00:00
use dioxus_core as dioxus;
2021-05-15 16:03:08 +00:00
use dioxus_core::prelude::*;
2021-03-26 19:50:28 +00:00
use dioxus_web::WebsysRenderer;
2021-05-15 16:03:08 +00:00
use std::collections::BTreeMap;
2021-03-26 19:50:28 +00:00
fn main() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
console_error_panic_hook::set_once();
2021-05-15 16:03:08 +00:00
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
}
static APP_STYLE: &'static str = include_str!("./todomvc/style.css");
#[derive(PartialEq, Clone, Copy)]
pub enum FilterState {
All,
Active,
Completed,
2021-03-26 19:50:28 +00:00
}
2021-05-15 16:03:08 +00:00
#[derive(Debug, PartialEq, Clone)]
pub struct TodoItem {
pub id: uuid::Uuid,
pub checked: bool,
pub contents: String,
2021-03-26 19:50:28 +00:00
}
static App: FC<()> = |ctx| {
2021-05-15 16:03:08 +00:00
let (draft, set_draft) = use_state(&ctx, || "".to_string());
let (filter, set_filter) = use_state(&ctx, || FilterState::All);
let todos = use_state_new(&ctx, || BTreeMap::<uuid::Uuid, TodoItem>::new());
2021-03-26 19:50:28 +00:00
ctx.render(rsx!(
div {
2021-05-15 16:03:08 +00:00
id: "app"
div {
header {
class: "header"
h1 {"todos"}
button {
"press me"
onclick: move |evt| {
let contents = draft.clone();
todos.modify(|f| {
let id = uuid::Uuid::new_v4();
f.insert(id.clone(), TodoItem {
id,
checked: false,
contents
});
})
}
}
input {
class: "new-todo"
placeholder: "What needs to be done?"
oninput: move |evt| set_draft(evt.value)
}
}
{ // list
todos
.iter()
.filter(|(id, item)| match filter {
FilterState::All => true,
FilterState::Active => !item.checked,
FilterState::Completed => item.checked,
})
.map(|(id, todo)| {
rsx!{
li {
2021-06-15 14:02:46 +00:00
key: "{id}"
2021-05-15 16:03:08 +00:00
"{todo.contents}"
input {
class: "toggle"
type: "checkbox"
"{todo.checked}"
}
}
}
})
}
// filter toggle (show only if the list isn't empty)
{(!todos.is_empty()).then(||
rsx!{
footer {
span {
strong {"10"}
span {"0 items left"}
}
ul {
class: "filters"
{[
("All", "", FilterState::All),
("Active", "active", FilterState::Active),
("Completed", "completed", FilterState::Completed),
]
.iter()
.map(|(name, path, filter)| {
rsx!(
li {
class: "{name}"
a {
href: "{path}"
onclick: move |_| set_filter(filter.clone())
"{name}"
}
}
)
})
}}
}
}
)}
2021-03-26 19:50:28 +00:00
}
2021-05-15 16:03:08 +00:00
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-03-26 19:50:28 +00:00
}
}
))
};
2021-03-29 16:31:47 +00:00
pub fn FilterToggles(ctx: Context<()>) -> VNode {
2021-05-15 16:03:08 +00:00
// let reducer = recoil::use_callback(&ctx, || ());
// let items_left = recoil::use_atom_family(&ctx, &TODOS, uuid::Uuid::new_v4());
2021-03-29 16:31:47 +00:00
2021-05-15 16:03:08 +00:00
let toggles = [
("All", "", FilterState::All),
("Active", "active", FilterState::Active),
("Completed", "completed", FilterState::Completed),
]
.iter()
.map(|(name, path, _filter)| {
rsx!(
li {
class: "{name}"
a {
href: "{path}"
// onclick: move |_| reducer.set_filter(&filter)
"{name}"
}
}
)
});
// todo
let item_text = "";
let items_left = "";
2021-03-29 16:31:47 +00:00
ctx.render(rsx! {
2021-05-15 16:03:08 +00:00
footer {
span {
strong {"{items_left}"}
span {"{item_text} left"}
}
ul {
class: "filters"
{toggles}
2021-03-29 16:31:47 +00:00
}
}
})
}