todomvc.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_elements::input_data::keyboard_types::Key;
  4. fn main() {
  5. dioxus_desktop::launch(app);
  6. }
  7. #[derive(PartialEq, Eq, Clone, Copy)]
  8. pub enum FilterState {
  9. All,
  10. Active,
  11. Completed,
  12. }
  13. #[derive(Debug, PartialEq, Eq, Clone)]
  14. pub struct TodoItem {
  15. pub id: u32,
  16. pub checked: bool,
  17. pub contents: String,
  18. }
  19. pub fn app(cx: Scope<()>) -> Element {
  20. let todos = use_state(cx, im_rc::HashMap::<u32, TodoItem>::default);
  21. let filter = use_state(cx, || FilterState::All);
  22. let draft = use_state(cx, || "".to_string());
  23. let todo_id = use_state(cx, || 0);
  24. // Filter the todos based on the filter state
  25. let mut filtered_todos = todos
  26. .iter()
  27. .filter(|(_, item)| match **filter {
  28. FilterState::All => true,
  29. FilterState::Active => !item.checked,
  30. FilterState::Completed => item.checked,
  31. })
  32. .map(|f| *f.0)
  33. .collect::<Vec<_>>();
  34. filtered_todos.sort_unstable();
  35. let active_todo_count = todos.values().filter(|item| !item.checked).count();
  36. let active_todo_text = match active_todo_count {
  37. 1 => "item",
  38. _ => "items",
  39. };
  40. let show_clear_completed = todos.values().any(|todo| todo.checked);
  41. let selected = |state| {
  42. if *filter == state {
  43. "selected"
  44. } else {
  45. "false"
  46. }
  47. };
  48. cx.render(rsx! {
  49. section { class: "todoapp",
  50. style { include_str!("./assets/todomvc.css") }
  51. header { class: "header",
  52. h1 {"todos"}
  53. input {
  54. class: "new-todo",
  55. placeholder: "What needs to be done?",
  56. value: "{draft}",
  57. autofocus: "true",
  58. oninput: move |evt| {
  59. draft.set(evt.value.clone());
  60. },
  61. onkeydown: move |evt| {
  62. if evt.key() == Key::Enter && !draft.is_empty() {
  63. todos.make_mut().insert(
  64. **todo_id,
  65. TodoItem {
  66. id: **todo_id,
  67. checked: false,
  68. contents: draft.to_string(),
  69. },
  70. );
  71. *todo_id.make_mut() += 1;
  72. draft.set("".to_string());
  73. }
  74. }
  75. }
  76. }
  77. section {
  78. class: "main",
  79. if !todos.is_empty() {
  80. rsx! {
  81. input {
  82. id: "toggle-all",
  83. class: "toggle-all",
  84. r#type: "checkbox",
  85. onchange: move |_| {
  86. let check = active_todo_count != 0;
  87. for (_, item) in todos.make_mut().iter_mut() {
  88. item.checked = check;
  89. }
  90. },
  91. checked: if active_todo_count == 0 { "true" } else { "false" },
  92. }
  93. label { r#for: "toggle-all" }
  94. }
  95. }
  96. ul { class: "todo-list",
  97. filtered_todos.iter().map(|id| rsx!(TodoEntry {
  98. key: "{id}",
  99. id: *id,
  100. todos: todos,
  101. }))
  102. }
  103. (!todos.is_empty()).then(|| rsx!(
  104. footer { class: "footer",
  105. span { class: "todo-count",
  106. strong {"{active_todo_count} "}
  107. span {"{active_todo_text} left"}
  108. }
  109. ul { class: "filters",
  110. for (state, state_text, url) in [
  111. (FilterState::All, "All", "#/"),
  112. (FilterState::Active, "Active", "#/active"),
  113. (FilterState::Completed, "Completed", "#/completed"),
  114. ] {
  115. li {
  116. a {
  117. href: url,
  118. class: selected(state),
  119. onclick: move |_| filter.set(state),
  120. prevent_default: "onclick",
  121. state_text
  122. }
  123. }
  124. }
  125. }
  126. show_clear_completed.then(|| rsx!(
  127. button {
  128. class: "clear-completed",
  129. onclick: move |_| todos.make_mut().retain(|_, todo| !todo.checked),
  130. "Clear completed"
  131. }
  132. ))
  133. }
  134. ))
  135. }
  136. }
  137. footer { class: "info",
  138. p { "Double-click to edit a todo" }
  139. p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
  140. p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
  141. }
  142. })
  143. }
  144. #[derive(Props)]
  145. pub struct TodoEntryProps<'a> {
  146. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  147. id: u32,
  148. }
  149. pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
  150. let is_editing = use_state(cx, || false);
  151. let todos = cx.props.todos.get();
  152. let todo = &todos[&cx.props.id];
  153. let completed = if todo.checked { "completed" } else { "" };
  154. let editing = if **is_editing { "editing" } else { "" };
  155. cx.render(rsx!{
  156. li {
  157. class: "{completed} {editing}",
  158. div { class: "view",
  159. input {
  160. class: "toggle",
  161. r#type: "checkbox",
  162. id: "cbg-{todo.id}",
  163. checked: "{todo.checked}",
  164. oninput: move |evt| {
  165. cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
  166. }
  167. }
  168. label {
  169. r#for: "cbg-{todo.id}",
  170. ondblclick: move |_| is_editing.set(true),
  171. prevent_default: "onclick",
  172. "{todo.contents}"
  173. }
  174. button {
  175. class: "destroy",
  176. onclick: move |_| { cx.props.todos.make_mut().remove(&todo.id); },
  177. prevent_default: "onclick",
  178. }
  179. }
  180. is_editing.then(|| rsx!{
  181. input {
  182. class: "edit",
  183. value: "{todo.contents}",
  184. oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
  185. autofocus: "true",
  186. onfocusout: move |_| is_editing.set(false),
  187. onkeydown: move |evt| {
  188. match evt.key() {
  189. Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
  190. _ => {}
  191. }
  192. },
  193. }
  194. })
  195. }
  196. })
  197. }