todomvc.rs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. const _STYLE: &str = mg!(file("./examples/assets/todomvc.css"));
  8. #[derive(PartialEq, Eq, Clone, Copy)]
  9. pub enum FilterState {
  10. All,
  11. Active,
  12. Completed,
  13. }
  14. #[derive(Debug, PartialEq, Eq, Clone)]
  15. pub struct TodoItem {
  16. pub id: u32,
  17. pub checked: bool,
  18. pub contents: String,
  19. }
  20. pub fn app(cx: Scope<()>) -> Element {
  21. let todos = use_state(cx, im_rc::HashMap::<u32, TodoItem>::default);
  22. let filter = use_state(cx, || FilterState::All);
  23. // Filter the todos based on the filter state
  24. let mut filtered_todos = todos
  25. .iter()
  26. .filter(|(_, item)| match **filter {
  27. FilterState::All => true,
  28. FilterState::Active => !item.checked,
  29. FilterState::Completed => item.checked,
  30. })
  31. .map(|f| *f.0)
  32. .collect::<Vec<_>>();
  33. filtered_todos.sort_unstable();
  34. let active_todo_count = todos.values().filter(|item| !item.checked).count();
  35. let active_todo_text = match active_todo_count {
  36. 1 => "item",
  37. _ => "items",
  38. };
  39. let show_clear_completed = todos.values().any(|todo| todo.checked);
  40. cx.render(rsx! {
  41. section { class: "todoapp",
  42. TodoHeader { todos: todos }
  43. section { class: "main",
  44. if !todos.is_empty() {
  45. rsx! {
  46. input {
  47. id: "toggle-all",
  48. class: "toggle-all",
  49. r#type: "checkbox",
  50. onchange: move |_| {
  51. let check = active_todo_count != 0;
  52. for (_, item) in todos.make_mut().iter_mut() {
  53. item.checked = check;
  54. }
  55. },
  56. checked: if active_todo_count == 0 { "true" } else { "false" },
  57. }
  58. label { r#for: "toggle-all" }
  59. }
  60. }
  61. ul { class: "todo-list",
  62. filtered_todos.iter().map(|id| rsx!(TodoEntry {
  63. key: "{id}",
  64. id: *id,
  65. todos: todos,
  66. }))
  67. }
  68. (!todos.is_empty()).then(|| rsx!(
  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<'a>(cx: Scope<'a, TodoHeaderProps<'a>>) -> Element {
  87. let draft = use_state(cx, || "".to_string());
  88. let todo_id = use_state(cx, || 0);
  89. cx.render(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<'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 { 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. is_editing.then(|| rsx!{
  159. input {
  160. class: "edit",
  161. value: "{todo.contents}",
  162. oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
  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<'a>(cx: Scope<'a, ListFooterProps<'a>>) -> 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. cx.render(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. cx.render(rsx! {
  219. button {
  220. class: "clear-completed",
  221. onclick: move |_| cx.props.todos.make_mut().retain(|_, todo| !todo.checked),
  222. "Clear completed"
  223. }
  224. })
  225. }
  226. }
  227. })
  228. }
  229. pub fn PageFooter(cx: Scope) -> Element {
  230. cx.render(rsx! {
  231. footer { class: "info",
  232. p { "Double-click to edit a todo" }
  233. p {
  234. "Created by "
  235. a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }
  236. }
  237. p {
  238. "Part of "
  239. a { href: "http://todomvc.com", "TodoMVC" }
  240. }
  241. }
  242. })
  243. }