todomvc_simple.rs 4.1 KB

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