todomvc.rs 3.3 KB

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