todomvc_simple.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. use std::{collections::HashMap, rc::Rc};
  2. use dioxus_core::prelude::*;
  3. use dioxus_web::WebsysRenderer;
  4. static APP_STYLE: &'static str = include_str!("./todomvc/style.css");
  5. fn main() {
  6. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
  7. }
  8. #[derive(PartialEq)]
  9. pub enum FilterState {
  10. All,
  11. Active,
  12. Completed,
  13. }
  14. #[derive(Debug, PartialEq, Clone)]
  15. pub struct TodoItem {
  16. pub id: uuid::Uuid,
  17. pub checked: bool,
  18. pub contents: String,
  19. }
  20. // =======================
  21. // Components
  22. // =======================
  23. pub fn App(ctx: Context<()>) -> VNode {
  24. ctx.render(rsx! {
  25. div {
  26. id: "app"
  27. // list
  28. TodoList {}
  29. // footer
  30. footer {
  31. class: "info"
  32. p {"Double-click to edit a todo"}
  33. p {
  34. "Created by "
  35. a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }
  36. }
  37. p {
  38. "Part of "
  39. a { "TodoMVC", href: "http://todomvc.com" }
  40. }
  41. }
  42. }
  43. })
  44. }
  45. pub fn TodoList(ctx: Context<()>) -> VNode {
  46. let (draft, set_draft) = use_state(&ctx, || "".to_string());
  47. let (todos, set_todos) = use_state(&ctx, || HashMap::<uuid::Uuid, Rc<TodoItem>>::new());
  48. let (filter, set_filter) = use_state(&ctx, || FilterState::All);
  49. ctx.render(rsx! {
  50. div {
  51. header {
  52. class: "header"
  53. h1 {"todos"}
  54. input {
  55. class: "new-todo"
  56. placeholder: "What needs to be done?"
  57. value: "{draft}"
  58. oninput: move |evt| set_draft(evt.value)
  59. }
  60. }
  61. { // list
  62. todos
  63. .iter()
  64. .filter(|(id, item)| match filter {
  65. FilterState::All => true,
  66. FilterState::Active => !item.checked,
  67. FilterState::Completed => item.checked,
  68. })
  69. .map(|(id, item)| {
  70. TodoEntry!();
  71. todo!()
  72. // rsx!(TodoEntry {
  73. // key: "{order}",
  74. // item: item.clone()
  75. // })
  76. })
  77. }
  78. // filter toggle (show only if the list isn't empty)
  79. {(!todos.is_empty()).then(||
  80. rsx!( FilterToggles {})
  81. )}
  82. }
  83. })
  84. }
  85. #[derive(PartialEq, Props)]
  86. pub struct TodoEntryProps {
  87. item: Rc<TodoItem>,
  88. }
  89. pub fn TodoEntry(ctx: Context, props: &TodoEntryProps) -> VNode {
  90. // #[inline_props]
  91. pub fn TodoEntry(
  92. ctx: Context,
  93. baller: &impl Fn() -> (),
  94. caller: &impl Fn() -> (),
  95. todo: &Rc<TodoItem>,
  96. ) -> VNode {
  97. // pub fn TodoEntry(ctx: Context, todo: &Rc<TodoItem>) -> VNode {
  98. let (is_editing, set_is_editing) = use_state(&ctx, || false);
  99. // let todo = &ctx.item;
  100. ctx.render(rsx! (
  101. li {
  102. "{todo.id}"
  103. input {
  104. class: "toggle"
  105. type: "checkbox"
  106. "{todo.checked}"
  107. }
  108. {is_editing.then(|| rsx!{
  109. input {
  110. value: "{contents}"
  111. }
  112. })}
  113. }
  114. ))
  115. }
  116. pub fn FilterToggles(ctx: Context<()>) -> VNode {
  117. let toggles = [
  118. ("All", "", FilterState::All),
  119. ("Active", "active", FilterState::Active),
  120. ("Completed", "completed", FilterState::Completed),
  121. ]
  122. .iter()
  123. .map(|(name, path, filter)| {
  124. rsx!(
  125. li {
  126. class: "{name}"
  127. a {
  128. href: "{path}"
  129. // onclick: move |_| reducer.set_filter(&filter)
  130. "{name}"
  131. }
  132. }
  133. )
  134. });
  135. // todo
  136. let item_text = "";
  137. let items_left = "";
  138. ctx.render(rsx! {
  139. footer {
  140. span {
  141. strong {"{items_left}"}
  142. span {"{item_text} left"}
  143. }
  144. ul {
  145. class: "filters"
  146. {toggles}
  147. }
  148. }
  149. })
  150. }