123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- use dioxus_core as dioxus;
- use dioxus_core::prelude::*;
- use dioxus_html as dioxus_elements;
- use dioxus_web::WebsysRenderer;
- use std::collections::BTreeMap;
- fn main() {
- wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
- console_error_panic_hook::set_once();
- 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,
- }
- #[derive(Debug, PartialEq, Clone)]
- pub struct TodoItem {
- pub id: uuid::Uuid,
- pub checked: bool,
- pub contents: String,
- }
- static App: FC<()> = |cx| {
- let (draft, set_draft) = use_state_classic(cx, || "".to_string());
- let (filter, set_filter) = use_state_classic(cx, || FilterState::All);
- let todos = use_state(cx, || BTreeMap::<uuid::Uuid, TodoItem>::new());
- cx.render(rsx!(
- div {
- 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 {
- key: "{id}"
- "{todo.contents}"
- input {
- class: "toggle"
- r#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}"
- }
- }
- )
- })
- }}
- }
- }
- )}
- }
- 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" }
- }
- }
- }
- ))
- };
- pub fn FilterToggles(cx: Context<()>) -> VNode {
- // let reducer = recoil::use_callback(&cx, || ());
- // let items_left = recoil::use_atom_family(&cx, &TODOS, uuid::Uuid::new_v4());
- 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 = "";
- cx.render(rsx! {
- footer {
- span {
- strong {"{items_left}"}
- span {"{item_text} left"}
- }
- ul {
- class: "filters"
- {toggles}
- }
- }
- })
- }
|