todomvc.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use dioxus::prelude::*;
  2. use im_rc::HashMap;
  3. use std::rc::Rc;
  4. fn main() {
  5. #[cfg(feature = "desktop")]
  6. // #[cfg(not(target_arch = "wasm32"))]
  7. dioxus::desktop::launch(App, |c| c);
  8. #[cfg(feature = "desktop")]
  9. dioxus::web::launch(App, |c| c);
  10. }
  11. #[derive(PartialEq)]
  12. pub enum FilterState {
  13. All,
  14. Active,
  15. Completed,
  16. }
  17. #[derive(Debug, PartialEq)]
  18. pub struct TodoItem {
  19. pub id: u32,
  20. pub checked: bool,
  21. pub contents: String,
  22. }
  23. const STYLE: &str = include_str!("./_examples/todomvc/style.css");
  24. const App: FC<()> = |cx| {
  25. let draft = use_state(cx, || "".to_string());
  26. let todos = use_state(cx, || HashMap::<u32, Rc<TodoItem>>::new());
  27. let filter = use_state(cx, || FilterState::All);
  28. let todolist = todos
  29. .iter()
  30. .filter(|(id, item)| match *filter {
  31. FilterState::All => true,
  32. FilterState::Active => !item.checked,
  33. FilterState::Completed => item.checked,
  34. })
  35. .map(|(id, todo)| {
  36. rsx!(TodoEntry {
  37. key: "{id}",
  38. todo: todo.clone()
  39. })
  40. })
  41. .collect::<Vec<_>>();
  42. let items_left = todolist.len();
  43. let item_text = match items_left {
  44. 1 => "item",
  45. _ => "items",
  46. };
  47. cx.render(rsx! {
  48. div { id: "app"
  49. style {"{STYLE}"}
  50. div {
  51. header { class: "header"
  52. h1 {"todos"}
  53. input {
  54. class: "new-todo"
  55. placeholder: "What needs to be done?"
  56. value: "{draft}"
  57. oninput: move |evt| draft.set(evt.value())
  58. }
  59. }
  60. {todolist}
  61. {(!todos.is_empty()).then(|| rsx!(
  62. footer {
  63. span { strong {"{items_left}"} span {"{item_text} left"} }
  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 { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }}
  75. p { "Part of ", a { "TodoMVC", href: "http://todomvc.com" }}
  76. }
  77. }
  78. })
  79. };
  80. #[derive(PartialEq, Props)]
  81. pub struct TodoEntryProps {
  82. todo: std::rc::Rc<TodoItem>,
  83. }
  84. pub fn TodoEntry(cx: Context<TodoEntryProps>) -> DomTree {
  85. let TodoEntryProps { todo } = *cx;
  86. let is_editing = use_state(cx, || false);
  87. let contents = "";
  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. }
  100. })}
  101. }
  102. ))
  103. }