todomvc.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #![allow(non_upper_case_globals, non_snake_case)]
  2. use dioxus::prelude::*;
  3. use im_rc::HashMap;
  4. use std::rc::Rc;
  5. fn main() {
  6. dioxus::desktop::launch(App);
  7. }
  8. #[derive(PartialEq)]
  9. pub enum FilterState {
  10. All,
  11. Active,
  12. Completed,
  13. }
  14. #[derive(Debug, PartialEq)]
  15. pub struct TodoItem {
  16. pub id: u32,
  17. pub checked: bool,
  18. pub contents: String,
  19. }
  20. const STYLE: &str = include_str!("./assets/todomvc.css");
  21. const App: Component = |cx| {
  22. let draft = use_state(&cx, || "".to_string());
  23. let todos = use_state(&cx, || HashMap::<u32, Rc<TodoItem>>::new());
  24. let filter = use_state(&cx, || FilterState::All);
  25. let todolist = todos
  26. .iter()
  27. .filter(|(_id, item)| match *filter {
  28. FilterState::All => true,
  29. FilterState::Active => !item.checked,
  30. FilterState::Completed => item.checked,
  31. })
  32. .map(|(id, todo)| {
  33. rsx!(TodoEntry {
  34. key: "{id}",
  35. todo: todo.clone()
  36. })
  37. })
  38. .collect::<Vec<_>>();
  39. let items_left = todolist.len();
  40. let item_text = match items_left {
  41. 1 => "item",
  42. _ => "items",
  43. };
  44. rsx!(cx, div { id: "app",
  45. style {"{STYLE}"}
  46. div {
  47. header { class: "header",
  48. h1 {"todos"}
  49. input {
  50. class: "new-todo",
  51. placeholder: "What needs to be done?",
  52. value: "{draft}",
  53. oninput: move |evt| draft.set(evt.value.clone()),
  54. }
  55. }
  56. todolist,
  57. (!todos.is_empty()).then(|| rsx!(
  58. footer {
  59. span {
  60. strong {"{items_left}"}
  61. span {"{item_text} left"}
  62. }
  63. ul { class: "filters",
  64. li { class: "All", a { href: "", onclick: move |_| filter.set(FilterState::All), "All" }}
  65. li { class: "Active", a { href: "active", onclick: move |_| filter.set(FilterState::Active), "Active" }}
  66. li { class: "Completed", a { href: "completed", onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
  67. }
  68. }
  69. ))
  70. }
  71. footer { class: "info",
  72. p {"Double-click to edit a todo"}
  73. p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
  74. p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
  75. }
  76. })
  77. };
  78. #[derive(PartialEq, Props)]
  79. pub struct TodoEntryProps {
  80. todo: Rc<TodoItem>,
  81. }
  82. pub fn TodoEntry(cx: Scope<TodoEntryProps>) -> Element {
  83. let is_editing = use_state(&cx, || false);
  84. let contents = use_state(&cx, || String::from(""));
  85. let todo = &cx.props.todo;
  86. rsx!(cx, li {
  87. "{todo.id}"
  88. input {
  89. class: "toggle",
  90. r#type: "checkbox",
  91. "{todo.checked}"
  92. }
  93. {is_editing.then(|| rsx!{
  94. input {
  95. value: "{contents}",
  96. oninput: move |evt| contents.set(evt.value.clone())
  97. }
  98. })}
  99. })
  100. }