list.rs 5.6 KB

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