todomvc.rs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_signal(im_rc::HashMap::<u32, TodoItem>::default);
  21. let filter = use_signal(|| FilterState::All);
  22. // Filter the todos based on the filter state
  23. let mut filtered_todos = todos
  24. .iter()
  25. .filter(|(_, item)| match **filter {
  26. FilterState::All => true,
  27. FilterState::Active => !item.checked,
  28. FilterState::Completed => item.checked,
  29. })
  30. .map(|f| *f.0)
  31. .collect::<Vec<_>>();
  32. filtered_todos.sort_unstable();
  33. let active_todo_count = todos.values().filter(|item| !item.checked).count();
  34. let active_todo_text = match active_todo_count {
  35. 1 => "item",
  36. _ => "items",
  37. };
  38. let show_clear_completed = todos.values().any(|todo| todo.checked);
  39. rsx! {
  40. section { class: "todoapp",
  41. style { {include_str!("./assets/todomvc.css")} }
  42. TodoHeader { todos: todos }
  43. section { class: "main",
  44. if !todos.is_empty() {
  45. input {
  46. id: "toggle-all",
  47. class: "toggle-all",
  48. r#type: "checkbox",
  49. onchange: move |_| {
  50. let check = active_todo_count != 0;
  51. for (_, item) in todos.make_mut().iter_mut() {
  52. item.checked = check;
  53. }
  54. },
  55. checked: if active_todo_count == 0 { "true" } else { "false" },
  56. }
  57. label { r#for: "toggle-all" }
  58. }
  59. ul { class: "todo-list",
  60. for id in filtered_todos.iter() {
  61. TodoEntry {
  62. key: "{id}",
  63. id: *id,
  64. todos: todos,
  65. }
  66. }
  67. }
  68. if !todos.is_empty() {
  69. ListFooter {
  70. active_todo_count: active_todo_count,
  71. active_todo_text: active_todo_text,
  72. show_clear_completed: show_clear_completed,
  73. todos: todos,
  74. filter: filter,
  75. }
  76. }
  77. }
  78. }
  79. PageFooter {}
  80. }
  81. }
  82. #[derive(Props)]
  83. pub struct TodoHeaderProps<'a> {
  84. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  85. }
  86. pub fn TodoHeader(props: TodoHeaderProps) -> Element {
  87. let draft = use_signal(|| "".to_string());
  88. let todo_id = use_signal(|| 0);
  89. rsx! {
  90. header { class: "header",
  91. h1 { "todos" }
  92. input {
  93. class: "new-todo",
  94. placeholder: "What needs to be done?",
  95. value: "{draft}",
  96. autofocus: "true",
  97. oninput: move |evt| {
  98. draft.set(evt.value().clone());
  99. },
  100. onkeydown: move |evt| {
  101. if evt.key() == Key::Enter && !draft.is_empty() {
  102. cx.props
  103. .todos
  104. .make_mut()
  105. .insert(
  106. **todo_id,
  107. TodoItem {
  108. id: **todo_id,
  109. checked: false,
  110. contents: draft.to_string(),
  111. },
  112. );
  113. *todo_id.make_mut() += 1;
  114. draft.set("".to_string());
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. #[derive(Props)]
  122. pub struct TodoEntryProps<'a> {
  123. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  124. id: u32,
  125. }
  126. pub fn TodoEntry(props: TodoEntryProps) -> Element {
  127. let is_editing = use_signal(|| false);
  128. let todos = cx.props.todos.get();
  129. let todo = &todos[&cx.props.id];
  130. let completed = if todo.checked { "completed" } else { "" };
  131. let editing = if **is_editing { "editing" } else { "" };
  132. rsx! {
  133. li { class: "{completed} {editing}",
  134. div { class: "view",
  135. input {
  136. class: "toggle",
  137. r#type: "checkbox",
  138. id: "cbg-{todo.id}",
  139. checked: "{todo.checked}",
  140. oninput: move |evt| {
  141. cx.props.todos.make_mut()[&cx.props.id].checked = evt.value().parse().unwrap();
  142. }
  143. }
  144. label {
  145. r#for: "cbg-{todo.id}",
  146. ondoubleclick: move |_| is_editing.set(true),
  147. prevent_default: "onclick",
  148. "{todo.contents}"
  149. }
  150. button {
  151. class: "destroy",
  152. onclick: move |_| {
  153. cx.props.todos.make_mut().remove(&todo.id);
  154. },
  155. prevent_default: "onclick"
  156. }
  157. }
  158. if **is_editing {
  159. input {
  160. class: "edit",
  161. value: "{todo.contents}",
  162. oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value(),
  163. autofocus: "true",
  164. onfocusout: move |_| is_editing.set(false),
  165. onkeydown: move |evt| {
  166. match evt.key() {
  167. Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
  168. _ => {}
  169. }
  170. },
  171. }
  172. }
  173. }
  174. }
  175. }
  176. #[derive(Props)]
  177. pub struct ListFooterProps<'a> {
  178. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  179. active_todo_count: usize,
  180. active_todo_text: &'a str,
  181. show_clear_completed: bool,
  182. filter: &'a UseState<FilterState>,
  183. }
  184. pub fn ListFooter(props: ListFooterProps) -> Element {
  185. let active_todo_count = cx.props.active_todo_count;
  186. let active_todo_text = cx.props.active_todo_text;
  187. let selected = |state| {
  188. if *cx.props.filter == state {
  189. "selected"
  190. } else {
  191. "false"
  192. }
  193. };
  194. rsx! {
  195. footer { class: "footer",
  196. span { class: "todo-count",
  197. strong { "{active_todo_count} " }
  198. span { "{active_todo_text} left" }
  199. }
  200. ul { class: "filters",
  201. for (state , state_text , url) in [
  202. (FilterState::All, "All", "#/"),
  203. (FilterState::Active, "Active", "#/active"),
  204. (FilterState::Completed, "Completed", "#/completed"),
  205. ] {
  206. li {
  207. a {
  208. href: url,
  209. class: selected(state),
  210. onclick: move |_| cx.props.filter.set(state),
  211. prevent_default: "onclick",
  212. {state_text}
  213. }
  214. }
  215. }
  216. }
  217. if cx.props.show_clear_completed {
  218. button {
  219. class: "clear-completed",
  220. onclick: move |_| cx.props.todos.make_mut().retain(|_, todo| !todo.checked),
  221. "Clear completed"
  222. }
  223. }
  224. }
  225. }
  226. }
  227. pub fn PageFooter() -> Element {
  228. rsx! {
  229. footer { class: "info",
  230. p { "Double-click to edit a todo" }
  231. p {
  232. "Created by "
  233. a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }
  234. }
  235. p {
  236. "Part of "
  237. a { href: "http://todomvc.com", "TodoMVC" }
  238. }
  239. }
  240. }
  241. }