1
0

todomvc.rs 5.8 KB

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