1
0

todomvc.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // 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. cx.render(rsx! {
  40. section { class: "todoapp",
  41. style { include_str!("./assets/todomvc.css") }
  42. TodoHeader {
  43. todos: todos,
  44. }
  45. section {
  46. class: "main",
  47. if !todos.is_empty() {
  48. rsx! {
  49. input {
  50. id: "toggle-all",
  51. class: "toggle-all",
  52. r#type: "checkbox",
  53. onchange: move |_| {
  54. let check = active_todo_count != 0;
  55. for (_, item) in todos.make_mut().iter_mut() {
  56. item.checked = check;
  57. }
  58. },
  59. checked: if active_todo_count == 0 { "true" } else { "false" },
  60. }
  61. label { r#for: "toggle-all" }
  62. }
  63. }
  64. ul { class: "todo-list",
  65. filtered_todos.iter().map(|id| rsx!(TodoEntry {
  66. key: "{id}",
  67. id: *id,
  68. todos: todos,
  69. }))
  70. }
  71. (!todos.is_empty()).then(|| rsx!(
  72. ListFooter {
  73. active_todo_count: active_todo_count,
  74. active_todo_text: active_todo_text,
  75. show_clear_completed: show_clear_completed,
  76. todos: todos,
  77. filter: filter,
  78. }
  79. ))
  80. }
  81. }
  82. PageFooter {}
  83. })
  84. }
  85. #[derive(Props)]
  86. pub struct TodoHeaderProps<'a> {
  87. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  88. }
  89. pub fn TodoHeader<'a>(cx: Scope<'a, TodoHeaderProps<'a>>) -> Element {
  90. let draft = use_state(cx, || "".to_string());
  91. let todo_id = use_state(cx, || 0);
  92. cx.render(rsx! {
  93. header { class: "header",
  94. h1 {"todos"}
  95. input {
  96. class: "new-todo",
  97. placeholder: "What needs to be done?",
  98. value: "{draft}",
  99. autofocus: "true",
  100. oninput: move |evt| {
  101. draft.set(evt.value.clone());
  102. },
  103. onkeydown: move |evt| {
  104. if evt.key() == Key::Enter && !draft.is_empty() {
  105. cx.props.todos.make_mut().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<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
  127. let is_editing = use_state(cx, || 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. cx.render(rsx!{
  133. li {
  134. class: "{completed} {editing}",
  135. div { class: "view",
  136. input {
  137. class: "toggle",
  138. r#type: "checkbox",
  139. id: "cbg-{todo.id}",
  140. checked: "{todo.checked}",
  141. oninput: move |evt| {
  142. cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
  143. }
  144. }
  145. label {
  146. r#for: "cbg-{todo.id}",
  147. ondblclick: move |_| is_editing.set(true),
  148. prevent_default: "onclick",
  149. "{todo.contents}"
  150. }
  151. button {
  152. class: "destroy",
  153. onclick: move |_| { cx.props.todos.make_mut().remove(&todo.id); },
  154. prevent_default: "onclick",
  155. }
  156. }
  157. is_editing.then(|| rsx!{
  158. input {
  159. class: "edit",
  160. value: "{todo.contents}",
  161. oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
  162. autofocus: "true",
  163. onfocusout: move |_| is_editing.set(false),
  164. onkeydown: move |evt| {
  165. match evt.key() {
  166. Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
  167. _ => {}
  168. }
  169. },
  170. }
  171. })
  172. }
  173. })
  174. }
  175. #[derive(Props)]
  176. pub struct ListFooterProps<'a> {
  177. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  178. active_todo_count: usize,
  179. active_todo_text: &'a str,
  180. show_clear_completed: bool,
  181. filter: &'a UseState<FilterState>,
  182. }
  183. pub fn ListFooter<'a>(cx: Scope<'a, ListFooterProps<'a>>) -> Element {
  184. let active_todo_count = cx.props.active_todo_count;
  185. let active_todo_text = cx.props.active_todo_text;
  186. let selected = |state| {
  187. if *cx.props.filter == state {
  188. "selected"
  189. } else {
  190. "false"
  191. }
  192. };
  193. cx.render(rsx! {
  194. footer { class: "footer",
  195. span { class: "todo-count",
  196. strong {"{active_todo_count} "}
  197. span {"{active_todo_text} left"}
  198. }
  199. ul { class: "filters",
  200. for (state, state_text, url) in [
  201. (FilterState::All, "All", "#/"),
  202. (FilterState::Active, "Active", "#/active"),
  203. (FilterState::Completed, "Completed", "#/completed"),
  204. ] {
  205. li {
  206. a {
  207. href: url,
  208. class: selected(state),
  209. onclick: move |_| cx.props.filter.set(state),
  210. prevent_default: "onclick",
  211. state_text
  212. }
  213. }
  214. }
  215. }
  216. if cx.props.show_clear_completed {
  217. cx.render(rsx! {
  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. }
  228. pub fn PageFooter(cx: Scope) -> Element {
  229. cx.render(rsx! {
  230. footer { class: "info",
  231. p { "Double-click to edit a todo" }
  232. p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
  233. p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
  234. }
  235. })
  236. }