todomvc.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, |c| c);
  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: FC<()> = |cx, props| {
  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 { strong {"{items_left}"} span {"{item_text} left"} }
  60. ul { class: "filters"
  61. li { class: "All", a { href: "", onclick: move |_| filter.set(FilterState::All), "All" }}
  62. li { class: "Active", a { href: "active", onclick: move |_| filter.set(FilterState::Active), "Active" }}
  63. li { class: "Completed", a { href: "completed", onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
  64. }
  65. }
  66. ))}
  67. }
  68. footer { class: "info"
  69. p {"Double-click to edit a todo"}
  70. p { "Created by ", a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }}
  71. p { "Part of ", a { "TodoMVC", href: "http://todomvc.com" }}
  72. }
  73. })
  74. };
  75. #[derive(PartialEq, Props)]
  76. pub struct TodoEntryProps {
  77. todo: Rc<TodoItem>,
  78. }
  79. pub fn TodoEntry<'a>(cx: Context<'a>, props: &TodoEntryProps) -> DomTree<'a> {
  80. let is_editing = use_state(cx, || false);
  81. let contents = use_state(cx, || String::from(""));
  82. let todo = &props.todo;
  83. rsx!(cx, li {
  84. "{todo.id}"
  85. input {
  86. class: "toggle"
  87. r#type: "checkbox"
  88. "{todo.checked}"
  89. }
  90. {is_editing.then(|| rsx!{
  91. input {
  92. value: "{contents}"
  93. oninput: move |evt| contents.set(evt.value.clone())
  94. }
  95. })}
  96. })
  97. }