todomvc.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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() -> anyhow::Result<()> {
  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!("./_examples/todomvc/style.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. cx.render(rsx! {
  45. div { id: "app"
  46. style {"{STYLE}"}
  47. div {
  48. header { 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())
  55. }
  56. }
  57. {todolist}
  58. {(!todos.is_empty()).then(|| rsx!(
  59. footer {
  60. span { strong {"{items_left}"} span {"{item_text} left"} }
  61. ul { class: "filters"
  62. li { class: "All", a { href: "", onclick: move |_| filter.set(FilterState::All), "All" }}
  63. li { class: "Active", a { href: "active", onclick: move |_| filter.set(FilterState::Active), "Active" }}
  64. li { class: "Completed", a { href: "completed", onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
  65. }
  66. }
  67. ))}
  68. }
  69. footer { class: "info"
  70. p {"Double-click to edit a todo"}
  71. p { "Created by ", a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }}
  72. p { "Part of ", a { "TodoMVC", href: "http://todomvc.com" }}
  73. }
  74. }
  75. })
  76. };
  77. #[derive(PartialEq, Props)]
  78. pub struct TodoEntryProps {
  79. todo: std::rc::Rc<TodoItem>,
  80. }
  81. pub fn TodoEntry<'a>(cx: Context<'a>, TodoEntryProps { todo }: &'a TodoEntryProps) -> DomTree<'a> {
  82. let is_editing = use_state(cx, || false);
  83. let contents = "";
  84. cx.render(rsx! (
  85. li {
  86. "{todo.id}"
  87. input {
  88. class: "toggle"
  89. r#type: "checkbox"
  90. "{todo.checked}"
  91. }
  92. {is_editing.then(|| rsx!{
  93. input {
  94. value: "{contents}"
  95. }
  96. })}
  97. }
  98. ))
  99. }