todomvc.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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| draft.set(evt.value.clone()),
  53. onkeydown: move |evt| {
  54. if evt.key() == Key::Enter && !draft.is_empty() {
  55. todos.make_mut().insert(
  56. **todo_id,
  57. TodoItem {
  58. id: **todo_id,
  59. checked: false,
  60. contents: draft.to_string(),
  61. },
  62. );
  63. *todo_id.make_mut() += 1;
  64. draft.set("".to_string());
  65. }
  66. }
  67. }
  68. }
  69. ul { class: "todo-list",
  70. filtered_todos.iter().map(|id| rsx!(TodoEntry { key: "{id}", id: *id, todos: todos }))
  71. }
  72. (!todos.is_empty()).then(|| rsx!(
  73. footer { class: "footer",
  74. span { class: "todo-count",
  75. strong {"{items_left} "}
  76. span {"{item_text} left"}
  77. }
  78. ul { class: "filters",
  79. li { class: "All", a { onclick: move |_| filter.set(FilterState::All), "All" }}
  80. li { class: "Active", a { onclick: move |_| filter.set(FilterState::Active), "Active" }}
  81. li { class: "Completed", a { onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
  82. }
  83. show_clear_completed.then(|| rsx!(
  84. button {
  85. class: "clear-completed",
  86. onclick: move |_| todos.make_mut().retain(|_, todo| !todo.checked),
  87. "Clear completed"
  88. }
  89. ))
  90. }
  91. ))
  92. }
  93. }
  94. footer { class: "info",
  95. p {"Double-click to edit a todo"}
  96. p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
  97. p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
  98. }
  99. })
  100. }
  101. #[derive(Props)]
  102. pub struct TodoEntryProps<'a> {
  103. todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
  104. id: u32,
  105. }
  106. pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
  107. let is_editing = use_state(&cx, || false);
  108. let todos = cx.props.todos.get();
  109. let todo = &todos[&cx.props.id];
  110. let completed = if todo.checked { "completed" } else { "" };
  111. let editing = if **is_editing { "editing" } else { "" };
  112. cx.render(rsx!{
  113. li {
  114. class: "{completed} {editing}",
  115. div { class: "view",
  116. input {
  117. class: "toggle",
  118. r#type: "checkbox",
  119. id: "cbg-{todo.id}",
  120. checked: "{todo.checked}",
  121. oninput: move |evt| {
  122. cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
  123. }
  124. }
  125. label {
  126. r#for: "cbg-{todo.id}",
  127. onclick: move |_| is_editing.set(true),
  128. prevent_default: "onclick",
  129. "{todo.contents}"
  130. }
  131. }
  132. is_editing.then(|| rsx!{
  133. input {
  134. class: "edit",
  135. value: "{todo.contents}",
  136. oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
  137. autofocus: "true",
  138. onfocusout: move |_| is_editing.set(false),
  139. onkeydown: move |evt| {
  140. match evt.key() {
  141. Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
  142. _ => {}
  143. }
  144. },
  145. }
  146. })
  147. }
  148. })
  149. }