todomvc_simple.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. TodoEntry!();
  72. todo!()
  73. // rsx!(TodoEntry {
  74. // key: "{order}",
  75. // item: item.clone()
  76. // })
  77. })
  78. }
  79. // filter toggle (show only if the list isn't empty)
  80. {(!todos.is_empty()).then(||
  81. rsx!( FilterToggles {})
  82. )}
  83. }
  84. })
  85. }
  86. #[derive(PartialEq, Props)]
  87. pub struct TodoEntryProps {
  88. item: Rc<TodoItem>,
  89. }
  90. mod mac {
  91. #[macro_export]
  92. macro_rules! TodoEntry {
  93. () => {};
  94. }
  95. }
  96. // pub fn TodoEntry(ctx: Context, props: &TodoEntryProps) -> DomTree {
  97. // #[inline_props]
  98. pub fn TodoEntry(
  99. ctx: Context,
  100. baller: &impl Fn() -> (),
  101. caller: &impl Fn() -> (),
  102. todo: &Rc<TodoItem>,
  103. ) -> DomTree {
  104. // pub fn TodoEntry(ctx: Context, todo: &Rc<TodoItem>) -> DomTree {
  105. let (is_editing, set_is_editing) = use_state(&ctx, || false);
  106. // let todo = &props.item;
  107. ctx.render(rsx! (
  108. li {
  109. "{todo.id}"
  110. input {
  111. class: "toggle"
  112. type: "checkbox"
  113. "{todo.checked}"
  114. }
  115. {is_editing.then(|| rsx!{
  116. input {
  117. value: "{contents}"
  118. }
  119. })}
  120. }
  121. ))
  122. }
  123. pub fn FilterToggles(ctx: Context, props: &()) -> DomTree {
  124. let toggles = [
  125. ("All", "", FilterState::All),
  126. ("Active", "active", FilterState::Active),
  127. ("Completed", "completed", FilterState::Completed),
  128. ]
  129. .iter()
  130. .map(|(name, path, filter)| {
  131. rsx!(
  132. li {
  133. class: "{name}"
  134. a {
  135. href: "{path}"
  136. // onclick: move |_| reducer.set_filter(&filter)
  137. "{name}"
  138. }
  139. }
  140. )
  141. });
  142. // todo
  143. let item_text = "";
  144. let items_left = "";
  145. ctx.render(rsx! {
  146. footer {
  147. span {
  148. strong {"{items_left}"}
  149. span {"{item_text} left"}
  150. }
  151. ul {
  152. class: "filters"
  153. {toggles}
  154. }
  155. }
  156. })
  157. }