todomvc-native.rs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //! The typical TodoMVC app, implemented in Dioxus.
  2. use dioxus::prelude::*;
  3. use std::collections::HashMap;
  4. const STYLE: Asset = asset!("/examples/assets/todomvc-native.css");
  5. fn main() {
  6. dioxus::launch(app);
  7. }
  8. #[derive(PartialEq, Eq, Clone, Copy)]
  9. enum FilterState {
  10. All,
  11. Active,
  12. Completed,
  13. }
  14. #[derive(Debug, PartialEq, Eq)]
  15. struct TodoItem {
  16. id: u32,
  17. checked: bool,
  18. contents: String,
  19. }
  20. fn app() -> Element {
  21. // We store the todos in a HashMap in a Signal.
  22. // Each key is the id of the todo, and the value is the todo itself.
  23. let mut todos = use_signal(HashMap::<u32, TodoItem>::new);
  24. let filter = use_signal(|| FilterState::All);
  25. // We use a simple memoized signal to calculate the number of active todos.
  26. // Whenever the todos change, the active_todo_count will be recalculated.
  27. let active_todo_count =
  28. use_memo(move || todos.read().values().filter(|item| !item.checked).count());
  29. // We use a memoized signal to filter the todos based on the current filter state.
  30. // Whenever the todos or filter change, the filtered_todos will be recalculated.
  31. // Note that we're only storing the IDs of the todos, not the todos themselves.
  32. let filtered_todos = use_memo(move || {
  33. let mut filtered_todos = todos
  34. .read()
  35. .iter()
  36. .filter(|(_, item)| match filter() {
  37. FilterState::All => true,
  38. FilterState::Active => !item.checked,
  39. FilterState::Completed => item.checked,
  40. })
  41. .map(|f| *f.0)
  42. .collect::<Vec<_>>();
  43. filtered_todos.sort_unstable();
  44. filtered_todos
  45. });
  46. // Toggle all the todos to the opposite of the current state.
  47. // If all todos are checked, uncheck them all. If any are unchecked, check them all.
  48. let toggle_all = move |_| {
  49. let check = active_todo_count() != 0;
  50. for (_, item) in todos.write().iter_mut() {
  51. item.checked = check;
  52. }
  53. };
  54. rsx! {
  55. document::Link { rel: "stylesheet", href: STYLE }
  56. body {
  57. section { class: "todoapp",
  58. TodoHeader { todos }
  59. section { class: "main",
  60. if !todos.read().is_empty() {
  61. input {
  62. id: "toggle-all",
  63. class: "toggle-all",
  64. r#type: "checkbox",
  65. onchange: toggle_all,
  66. checked: active_todo_count() == 0
  67. }
  68. label { r#for: "toggle-all" }
  69. }
  70. // Render the todos using the filtered_todos signal
  71. // We pass the ID into the TodoEntry component so it can access the todo from the todos signal.
  72. // Since we store the todos in a signal too, we also need to send down the todo list
  73. ul { class: "todo-list",
  74. for id in filtered_todos() {
  75. TodoEntry { key: "{id}", id, todos }
  76. }
  77. }
  78. // We only show the footer if there are todos.
  79. if !todos.read().is_empty() {
  80. ListFooter { active_todo_count, todos, filter }
  81. }
  82. }
  83. }
  84. // A simple info footer
  85. footer { class: "info",
  86. p { "Double-click to edit a todo" }
  87. p {
  88. "Created by "
  89. a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }
  90. }
  91. p {
  92. "Part of "
  93. a { href: "http://todomvc.com", "TodoMVC" }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. #[component]
  100. fn TodoHeader(mut todos: Signal<HashMap<u32, TodoItem>>) -> Element {
  101. let mut draft = use_signal(|| "".to_string());
  102. let mut todo_id = use_signal(|| 0);
  103. let onkeydown = move |evt: KeyboardEvent| {
  104. if evt.key() == Key::Enter && !draft.read().is_empty() {
  105. let id = todo_id();
  106. let todo = TodoItem {
  107. id,
  108. checked: false,
  109. contents: draft.to_string(),
  110. };
  111. todos.write().insert(id, todo);
  112. todo_id += 1;
  113. draft.set("".to_string());
  114. evt.prevent_default();
  115. }
  116. };
  117. rsx! {
  118. header { class: "header",
  119. h1 { "todos" }
  120. input {
  121. class: "new-todo",
  122. r#type: "text",
  123. placeholder: "What needs to be done?",
  124. value: "{draft}",
  125. autofocus: "true",
  126. oninput: move |evt| draft.set(evt.value()),
  127. onkeydown
  128. }
  129. }
  130. }
  131. }
  132. /// A single todo entry
  133. /// This takes the ID of the todo and the todos signal as props
  134. /// We can use these together to memoize the todo contents and checked state
  135. #[component]
  136. fn TodoEntry(mut todos: Signal<HashMap<u32, TodoItem>>, id: u32) -> Element {
  137. let mut is_editing = use_signal(|| false);
  138. // To avoid re-rendering this component when the todo list changes, we isolate our reads to memos
  139. // This way, the component will only re-render when the contents of the todo change, or when the editing state changes.
  140. // This does involve taking a local clone of the todo contents, but it allows us to prevent this component from re-rendering
  141. let checked = use_memo(move || todos.read().get(&id).unwrap().checked);
  142. let contents = use_memo(move || todos.read().get(&id).unwrap().contents.clone());
  143. rsx! {
  144. li {
  145. // Dioxus lets you use if statements in rsx to conditionally render attributes
  146. // These will get merged into a single class attribute
  147. class: if checked() { "completed" },
  148. class: if is_editing() { "editing" },
  149. // Some basic controls for the todo
  150. div { class: "view",
  151. input {
  152. class: "toggle",
  153. r#type: "checkbox",
  154. id: "cbg-{id}",
  155. checked: "{checked}",
  156. oninput: move |evt| todos.write().get_mut(&id).unwrap().checked = evt.checked()
  157. }
  158. label {
  159. r#for: "cbg-{id}",
  160. onclick: move |evt| {
  161. is_editing.set(true);
  162. evt.prevent_default()
  163. },
  164. "{contents}"
  165. }
  166. button {
  167. class: "destroy",
  168. onclick: move |evt| {
  169. todos.write().remove(&id);
  170. evt.prevent_default();
  171. },
  172. }
  173. }
  174. // Only render the actual input if we're editing
  175. if is_editing() {
  176. input {
  177. class: "edit",
  178. r#type: "text",
  179. value: "{contents}",
  180. oninput: move |evt| todos.write().get_mut(&id).unwrap().contents = evt.value(),
  181. autofocus: "true",
  182. onfocusout: move |_| is_editing.set(false),
  183. onkeydown: move |evt| {
  184. if matches!(evt.key(), Key::Enter | Key::Escape | Key::Tab) {
  185. evt.prevent_default();
  186. is_editing.set(false);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. #[component]
  195. fn ListFooter(
  196. mut todos: Signal<HashMap<u32, TodoItem>>,
  197. active_todo_count: ReadOnlySignal<usize>,
  198. mut filter: Signal<FilterState>,
  199. ) -> Element {
  200. // We use a memoized signal to calculate whether we should show the "Clear completed" button.
  201. // This will recompute whenever the todos change, and if the value is true, the button will be shown.
  202. let show_clear_completed = use_memo(move || todos.read().values().any(|todo| todo.checked));
  203. rsx! {
  204. footer { class: "footer",
  205. span { class: "todo-count",
  206. strong { "{active_todo_count} " }
  207. span {
  208. match active_todo_count() {
  209. 1 => "item",
  210. _ => "items",
  211. },
  212. " left"
  213. }
  214. }
  215. ul { class: "filters",
  216. for (state , state_text , url) in [
  217. (FilterState::All, "All", "#/"),
  218. (FilterState::Active, "Active", "#/active"),
  219. (FilterState::Completed, "Completed", "#/completed"),
  220. ] {
  221. li {
  222. a {
  223. href: url,
  224. class: if filter() == state { "selected" },
  225. onclick: move |evt| {
  226. filter.set(state);
  227. evt.prevent_default();
  228. },
  229. {state_text}
  230. }
  231. }
  232. }
  233. }
  234. if show_clear_completed() {
  235. button {
  236. class: "clear-completed",
  237. onclick: move |_| todos.write().retain(|_, todo| !todo.checked),
  238. "Clear completed"
  239. }
  240. }
  241. }
  242. }
  243. }