todomvc_simple.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, props: &()) -> DomTree {
  24. ctx.render(rsx! {
  25. div {
  26. id: "app"
  27. style { "{APP_STYLE}" }
  28. // list
  29. TodoList {}
  30. // footer
  31. footer {
  32. class: "info"
  33. p {"Double-click to edit a todo"}
  34. p {
  35. "Created by "
  36. a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }
  37. }
  38. p {
  39. "Part of "
  40. a { "TodoMVC", href: "http://todomvc.com" }
  41. }
  42. }
  43. }
  44. })
  45. }
  46. pub fn TodoList(ctx: Context, props: &()) -> DomTree {
  47. let (draft, set_draft) = use_state(&ctx, || "".to_string());
  48. let (todos, set_todos) = use_state(&ctx, || HashMap::<uuid::Uuid, Rc<TodoItem>>::new());
  49. let (filter, set_filter) = use_state(&ctx, || FilterState::All);
  50. ctx.render(rsx! {
  51. div {
  52. header {
  53. class: "header"
  54. h1 {"todos"}
  55. input {
  56. class: "new-todo"
  57. placeholder: "What needs to be done?"
  58. value: "{draft}"
  59. oninput: move |evt| set_draft(evt.value)
  60. }
  61. }
  62. { // list
  63. todos
  64. .iter()
  65. .filter(|(id, item)| match filter {
  66. FilterState::All => true,
  67. FilterState::Active => !item.checked,
  68. FilterState::Completed => item.checked,
  69. })
  70. .map(|(id, item)| {
  71. rsx!(TodoEntry {
  72. key: "{order}",
  73. item: item.clone()
  74. })
  75. })
  76. }
  77. // filter toggle (show only if the list isn't empty)
  78. {(!todos.is_empty()).then(||
  79. rsx!( FilterToggles {})
  80. )}
  81. }
  82. })
  83. }
  84. #[derive(PartialEq, Props)]
  85. pub struct TodoEntryProps {
  86. item: Rc<TodoItem>,
  87. }
  88. pub fn TodoEntry(ctx: Context, props: &TodoEntryProps) -> DomTree {
  89. let (is_editing, set_is_editing) = use_state(&ctx, || false);
  90. let todo = &props.item;
  91. ctx.render(rsx! (
  92. li {
  93. "{todo.id}"
  94. input {
  95. class: "toggle"
  96. type: "checkbox"
  97. "{todo.checked}"
  98. }
  99. {is_editing.then(|| rsx!(
  100. input {
  101. value: "{contents}"
  102. }
  103. ))}
  104. }
  105. ))
  106. }
  107. pub fn FilterToggles(ctx: Context, props: &()) -> DomTree {
  108. let toggles = [
  109. ("All", "", FilterState::All),
  110. ("Active", "active", FilterState::Active),
  111. ("Completed", "completed", FilterState::Completed),
  112. ]
  113. .iter()
  114. .map(|(name, path, filter)| {
  115. rsx!(
  116. li {
  117. class: "{name}"
  118. a {
  119. href: "{path}"
  120. // onclick: move |_| reducer.set_filter(&filter)
  121. "{name}"
  122. }
  123. }
  124. )
  125. });
  126. // todo
  127. let item_text = "";
  128. let items_left = "";
  129. ctx.render(rsx! {
  130. footer {
  131. span {
  132. strong {"{items_left}"}
  133. span {"{item_text} left"}
  134. }
  135. ul {
  136. class: "filters"
  137. {toggles}
  138. }
  139. }
  140. })
  141. }