todomvc.rs 5.8 KB

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