todomvc.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 mut 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" {
  53. if !draft.is_empty() {
  54. todos.modify().insert(
  55. *todo_id,
  56. TodoItem {
  57. id: *todo_id,
  58. checked: false,
  59. contents: draft.get().clone(),
  60. },
  61. );
  62. todo_id += 1;
  63. draft.set("".to_string());
  64. }
  65. }
  66. }
  67. }
  68. }
  69. ul { class: "todo-list",
  70. filtered_todos.iter().map(|id| rsx!(todo_entry( 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.modify().retain(|_, todo| todo.checked == false),
  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: UseState<'a, im_rc::HashMap<u32, TodoItem>>,
  104. id: u32,
  105. }
  106. pub fn todo_entry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
  107. let todo = &cx.props.todos[&cx.props.id];
  108. let is_editing = use_state(&cx, || false);
  109. let completed = if todo.checked { "completed" } else { "" };
  110. rsx!(cx, li { class: "{completed}",
  111. div { class: "view",
  112. input { class: "toggle", r#type: "checkbox", id: "cbg-{todo.id}", checked: "{todo.checked}",
  113. onchange: move |evt| {
  114. cx.props.todos.modify()[&cx.props.id].checked = evt.value.parse().unwrap();
  115. }
  116. }
  117. label { r#for: "cbg-{todo.id}", pointer_events: "none", "{todo.contents}" }
  118. is_editing.then(|| rsx!{
  119. input {
  120. value: "{todo.contents}",
  121. oninput: move |evt| cx.props.todos.modify()[&cx.props.id].contents = evt.value.clone(),
  122. }
  123. })
  124. }
  125. })
  126. }