todomvc_simple.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. use std::{collections::HashMap, rc::Rc};
  2. use dioxus_core as dioxus;
  3. use dioxus_core::prelude::*;
  4. use dioxus_web::WebsysRenderer;
  5. static APP_STYLE: &'static str = include_str!("./todomvc/style.css");
  6. fn main() {
  7. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
  8. }
  9. #[derive(PartialEq)]
  10. pub enum FilterState {
  11. All,
  12. Active,
  13. Completed,
  14. }
  15. #[derive(Debug, PartialEq, Clone)]
  16. pub struct TodoItem {
  17. pub id: uuid::Uuid,
  18. pub checked: bool,
  19. pub contents: String,
  20. }
  21. // =======================
  22. // Components
  23. // =======================
  24. pub fn App(cx: Context<()>) -> VNode {
  25. cx.render(rsx! {
  26. div {
  27. id: "app"
  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(cx: Context<()>) -> VNode {
  47. let (draft, set_draft) = use_state_classic(&cx, || "".to_string());
  48. let (todos, set_todos) = use_state_classic(&cx, || HashMap::<uuid::Uuid, Rc<TodoItem>>::new());
  49. let (filter, set_filter) = use_state_classic(&cx, || FilterState::All);
  50. cx.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. pub fn TodoEntry(cx: Context<TodoEntryProps>) -> VNode {
  91. let (is_editing, set_is_editing) = use_state_classic(&cx, || false);
  92. let contents = "";
  93. let todo = TodoItem {
  94. checked: false,
  95. contents: "asd".to_string(),
  96. id: uuid::Uuid::new_v4(),
  97. };
  98. cx.render(rsx! (
  99. li {
  100. "{todo.id}"
  101. input {
  102. class: "toggle"
  103. type: "checkbox"
  104. "{todo.checked}"
  105. }
  106. {is_editing.then(|| rsx!{
  107. input {
  108. value: "{contents}"
  109. }
  110. })}
  111. }
  112. ))
  113. }
  114. pub fn FilterToggles(cx: Context<()>) -> VNode {
  115. let toggles = [
  116. ("All", "", FilterState::All),
  117. ("Active", "active", FilterState::Active),
  118. ("Completed", "completed", FilterState::Completed),
  119. ]
  120. .iter()
  121. .map(|(name, path, filter)| {
  122. rsx!(
  123. li {
  124. class: "{name}"
  125. a {
  126. href: "{path}"
  127. // onclick: move |_| reducer.set_filter(&filter)
  128. "{name}"
  129. }
  130. }
  131. )
  132. });
  133. // todo
  134. let item_text = "";
  135. let items_left = "";
  136. cx.render(rsx! {
  137. footer {
  138. span {
  139. strong {"{items_left}"}
  140. span {"{item_text} left"}
  141. }
  142. ul {
  143. class: "filters"
  144. {toggles}
  145. }
  146. }
  147. })
  148. }