todomvc.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. use dioxus::prelude::*;
  2. fn main() {
  3. dioxus::desktop::launch(app);
  4. }
  5. #[derive(PartialEq)]
  6. pub enum FilterState {
  7. All,
  8. Active,
  9. Completed,
  10. }
  11. #[derive(Debug, PartialEq, Clone)]
  12. pub struct TodoItem {
  13. pub id: u32,
  14. pub checked: bool,
  15. pub contents: String,
  16. }
  17. pub fn app(cx: Scope<()>) -> Element {
  18. let todos = use_state(&cx, im_rc::HashMap::<u32, TodoItem>::default);
  19. let filter = use_state(&cx, || FilterState::All);
  20. let draft = use_state(&cx, || "".to_string());
  21. let todo_id = use_state(&cx, || 0);
  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 show_clear_completed = todos.values().any(|todo| todo.checked);
  34. let items_left = filtered_todos.len();
  35. let item_text = match items_left {
  36. 1 => "item",
  37. _ => "items",
  38. };
  39. cx.render(rsx!{
  40. section { class: "todoapp",
  41. style { [include_str!("./assets/todomvc.css")] }
  42. div {
  43. header { class: "header",
  44. h1 {"todos"}
  45. input {
  46. class: "new-todo",
  47. placeholder: "What needs to be done?",
  48. value: "{draft}",
  49. autofocus: "true",
  50. oninput: move |evt| draft.set(evt.value.clone()),
  51. onkeydown: move |evt| {
  52. if evt.key == "Enter" && !draft.is_empty() {
  53. todos.make_mut().insert(
  54. **todo_id,
  55. TodoItem {
  56. id: **todo_id,
  57. checked: false,
  58. contents: draft.to_string(),
  59. },
  60. );
  61. *todo_id.make_mut() += 1;
  62. draft.set("".to_string());
  63. }
  64. }
  65. }
  66. }
  67. ul { class: "todo-list",
  68. filtered_todos.iter().map(|id| rsx!(todo_entry( key: "{id}", id: *id, todos: todos )))
  69. }
  70. (!todos.is_empty()).then(|| rsx!(
  71. footer { class: "footer",
  72. span { class: "todo-count",
  73. strong {"{items_left} "}
  74. span {"{item_text} left"}
  75. }
  76. ul { class: "filters",
  77. li { class: "All", a { onclick: move |_| filter.set(FilterState::All), "All" }}
  78. li { class: "Active", a { onclick: move |_| filter.set(FilterState::Active), "Active" }}
  79. li { class: "Completed", a { onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
  80. }
  81. (show_clear_completed).then(|| rsx!(
  82. button {
  83. class: "clear-completed",
  84. onclick: move |_| todos.make_mut().retain(|_, todo| !todo.checked),
  85. "Clear completed"
  86. }
  87. ))
  88. }
  89. ))
  90. }
  91. }
  92. footer { class: "info",
  93. p {"Double-click to edit a todo"}
  94. p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
  95. p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
  96. }
  97. })
  98. }
  99. #[derive(Props)]
  100. pub struct TodoEntryProps<'a> {
  101. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  102. id: u32,
  103. }
  104. pub fn todo_entry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
  105. let is_editing = use_state(&cx, || false);
  106. let todos = cx.props.todos.get();
  107. let todo = &todos[&cx.props.id];
  108. let completed = if todo.checked { "completed" } else { "" };
  109. let editing = if **is_editing { "editing" } else { "" };
  110. cx.render(rsx!{
  111. li {
  112. class: "{completed} {editing}",
  113. div { class: "view",
  114. input {
  115. class: "toggle",
  116. r#type: "checkbox",
  117. id: "cbg-{todo.id}",
  118. checked: "{todo.checked}",
  119. oninput: move |evt| {
  120. cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
  121. }
  122. }
  123. label {
  124. r#for: "cbg-{todo.id}",
  125. onclick: move |_| is_editing.set(true),
  126. prevent_default: "onclick",
  127. "{todo.contents}"
  128. }
  129. }
  130. is_editing.then(|| rsx!{
  131. input {
  132. class: "edit",
  133. value: "{todo.contents}",
  134. oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
  135. autofocus: "true",
  136. onfocusout: move |_| is_editing.set(false),
  137. onkeydown: move |evt| {
  138. match evt.key.as_str() {
  139. "Enter" | "Escape" | "Tab" => is_editing.set(false),
  140. _ => {}
  141. }
  142. },
  143. }
  144. })
  145. }
  146. })
  147. }