clean up some more examples
This commit is contained in:
3 files changed
+84
-122
No files matched your search
+3
-3
@@ -2,12 +2,12 @@
|
|||||||
//!
|
//!
|
||||||
//! This example shows how we can render the Dioxus Virtualdom using SSR.
|
//! This example shows how we can render the Dioxus Virtualdom using SSR.
|
||||||
|
|
||||||
use dioxus::prelude::*;
|
use dioxus::{core::NoOpMutations, prelude::*};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// We can render VirtualDoms
|
// We can render VirtualDoms
|
||||||
let mut vdom = VirtualDom::new(app);
|
let mut vdom = VirtualDom::new(app);
|
||||||
let _ = vdom.rebuild();
|
let _ = vdom.rebuild(&mut NoOpMutations);
|
||||||
println!("{}", dioxus_ssr::render(&vdom));
|
println!("{}", dioxus_ssr::render(&vdom));
|
||||||
|
|
||||||
// Or we can render rsx! calls themselves
|
// Or we can render rsx! calls themselves
|
||||||
@@ -36,5 +36,5 @@ fn app() -> Element {
|
|||||||
h1 { "Title" }
|
h1 { "Title" }
|
||||||
p { "Body" }
|
p { "Body" }
|
||||||
}
|
}
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
+1
-1
@@ -12,7 +12,7 @@ fn main() {
|
|||||||
fn app() -> Element {
|
fn app() -> Element {
|
||||||
let mut count = use_signal(|| 0);
|
let mut count = use_signal(|| 0);
|
||||||
|
|
||||||
use_future(move |_| async move {
|
use_future(move || async move {
|
||||||
loop {
|
loop {
|
||||||
tokio::time::sleep(Duration::from_millis(1000)).await;
|
tokio::time::sleep(Duration::from_millis(1000)).await;
|
||||||
count += 1;
|
count += 1;
|
||||||
|
|||||||
+80
-118
@@ -1,7 +1,7 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use dioxus_elements::input_data::keyboard_types::Key;
|
use dioxus_elements::input_data::keyboard_types::Key;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dioxus_desktop::launch(app);
|
dioxus_desktop::launch(app);
|
||||||
@@ -21,67 +21,56 @@ pub struct TodoItem {
|
|||||||
pub contents: String,
|
pub contents: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn app(cx: Scope<()>) -> Element {
|
pub fn app(_props: ()) -> Element {
|
||||||
let todos = use_signal(im_rc::HashMap::<u32, TodoItem>::default);
|
let todos = use_signal(|| HashMap::<u32, TodoItem>::new());
|
||||||
let filter = use_signal(|| FilterState::All);
|
let filter = use_signal(|| FilterState::All);
|
||||||
|
|
||||||
// Filter the todos based on the filter state
|
let active_todo_count =
|
||||||
let mut filtered_todos = todos
|
use_selector(move || todos().values().filter(|item| !item.checked).count());
|
||||||
.iter()
|
|
||||||
.filter(|(_, item)| match **filter {
|
|
||||||
FilterState::All => true,
|
|
||||||
FilterState::Active => !item.checked,
|
|
||||||
FilterState::Completed => item.checked,
|
|
||||||
})
|
|
||||||
.map(|f| *f.0)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
filtered_todos.sort_unstable();
|
|
||||||
|
|
||||||
let active_todo_count = todos.values().filter(|item| !item.checked).count();
|
let filtered_todos = use_selector(move || {
|
||||||
let active_todo_text = match active_todo_count {
|
let mut filtered_todos = todos()
|
||||||
1 => "item",
|
.iter()
|
||||||
_ => "items",
|
.filter(|(_, item)| match *filter() {
|
||||||
};
|
FilterState::All => true,
|
||||||
|
FilterState::Active => !item.checked,
|
||||||
|
FilterState::Completed => item.checked,
|
||||||
|
})
|
||||||
|
.map(|f| *f.0)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let show_clear_completed = todos.values().any(|todo| todo.checked);
|
filtered_todos.sort_unstable();
|
||||||
|
|
||||||
|
filtered_todos
|
||||||
|
});
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
section { class: "todoapp",
|
section { class: "todoapp",
|
||||||
style { {include_str!("./assets/todomvc.css")} }
|
style { {include_str!("./assets/todomvc.css")} }
|
||||||
TodoHeader { todos: todos }
|
TodoHeader { todos }
|
||||||
section { class: "main",
|
section { class: "main",
|
||||||
if !todos.is_empty() {
|
if !todos().is_empty() {
|
||||||
input {
|
input {
|
||||||
id: "toggle-all",
|
id: "toggle-all",
|
||||||
class: "toggle-all",
|
class: "toggle-all",
|
||||||
r#type: "checkbox",
|
r#type: "checkbox",
|
||||||
onchange: move |_| {
|
onchange: move |_| {
|
||||||
let check = active_todo_count != 0;
|
let check = *active_todo_count() != 0;
|
||||||
for (_, item) in todos.make_mut().iter_mut() {
|
for (_, item) in todos.write().iter_mut() {
|
||||||
item.checked = check;
|
item.checked = check;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
checked: if active_todo_count == 0 { "true" } else { "false" },
|
checked: *active_todo_count() == 0,
|
||||||
}
|
}
|
||||||
label { r#for: "toggle-all" }
|
label { r#for: "toggle-all" }
|
||||||
}
|
}
|
||||||
ul { class: "todo-list",
|
ul { class: "todo-list",
|
||||||
for id in filtered_todos.iter() {
|
for id in filtered_todos().iter().copied() {
|
||||||
TodoEntry {
|
TodoEntry { key: "{id}", id, todos }
|
||||||
key: "{id}",
|
|
||||||
id: *id,
|
|
||||||
todos: todos,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !todos.is_empty() {
|
if !todos().is_empty() {
|
||||||
ListFooter {
|
ListFooter { active_todo_count, todos, filter }
|
||||||
active_todo_count: active_todo_count,
|
|
||||||
active_todo_text: active_todo_text,
|
|
||||||
show_clear_completed: show_clear_completed,
|
|
||||||
todos: todos,
|
|
||||||
filter: filter,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,14 +78,24 @@ pub fn app(cx: Scope<()>) -> Element {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Props)]
|
#[component]
|
||||||
pub struct TodoHeaderProps<'a> {
|
pub fn TodoHeader(todos: Signal<HashMap<u32, TodoItem>>) -> Element {
|
||||||
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn TodoHeader(props: TodoHeaderProps) -> Element {
|
|
||||||
let draft = use_signal(|| "".to_string());
|
let draft = use_signal(|| "".to_string());
|
||||||
let todo_id = use_signal(|| 0);
|
let mut todo_id = use_signal(|| 0);
|
||||||
|
|
||||||
|
let onkeydown = move |evt: KeyboardEvent| {
|
||||||
|
if evt.key() == Key::Enter && !draft().is_empty() {
|
||||||
|
let id = *todo_id();
|
||||||
|
let todo = TodoItem {
|
||||||
|
id,
|
||||||
|
checked: false,
|
||||||
|
contents: draft.to_string(),
|
||||||
|
};
|
||||||
|
todos.write().insert(id, todo);
|
||||||
|
todo_id += 1;
|
||||||
|
draft.set("".to_string());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
header { class: "header",
|
header { class: "header",
|
||||||
@@ -106,76 +105,46 @@ pub fn TodoHeader(props: TodoHeaderProps) -> Element {
|
|||||||
placeholder: "What needs to be done?",
|
placeholder: "What needs to be done?",
|
||||||
value: "{draft}",
|
value: "{draft}",
|
||||||
autofocus: "true",
|
autofocus: "true",
|
||||||
oninput: move |evt| {
|
oninput: move |evt| draft.set(evt.value().clone()),
|
||||||
draft.set(evt.value().clone());
|
onkeydown,
|
||||||
},
|
|
||||||
onkeydown: move |evt| {
|
|
||||||
if evt.key() == Key::Enter && !draft.is_empty() {
|
|
||||||
cx.props
|
|
||||||
.todos
|
|
||||||
.make_mut()
|
|
||||||
.insert(
|
|
||||||
**todo_id,
|
|
||||||
TodoItem {
|
|
||||||
id: **todo_id,
|
|
||||||
checked: false,
|
|
||||||
contents: draft.to_string(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
*todo_id.make_mut() += 1;
|
|
||||||
draft.set("".to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Props)]
|
#[component]
|
||||||
pub struct TodoEntryProps<'a> {
|
pub fn TodoEntry(todos: Signal<HashMap<u32, TodoItem>>, id: u32) -> Element {
|
||||||
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
|
||||||
id: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn TodoEntry(props: TodoEntryProps) -> Element {
|
|
||||||
let is_editing = use_signal(|| false);
|
let is_editing = use_signal(|| false);
|
||||||
|
let checked = use_selector(move || todos().get(&id).unwrap().checked);
|
||||||
let todos = cx.props.todos.get();
|
let contents = use_selector(move || todos().get(&id).unwrap().contents.clone());
|
||||||
let todo = &todos[&cx.props.id];
|
|
||||||
let completed = if todo.checked { "completed" } else { "" };
|
|
||||||
let editing = if **is_editing { "editing" } else { "" };
|
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
li { class: "{completed} {editing}",
|
li { class: if *checked() { "completed" }, class: if *is_editing() { "editing"},
|
||||||
div { class: "view",
|
div { class: "view",
|
||||||
input {
|
input {
|
||||||
class: "toggle",
|
class: "toggle",
|
||||||
r#type: "checkbox",
|
r#type: "checkbox",
|
||||||
id: "cbg-{todo.id}",
|
id: "cbg-{id}",
|
||||||
checked: "{todo.checked}",
|
checked: "{checked}",
|
||||||
oninput: move |evt| {
|
oninput: move |evt| todos.write().get_mut(&id).unwrap().checked = evt.value().parse().unwrap(),
|
||||||
cx.props.todos.make_mut()[&cx.props.id].checked = evt.value().parse().unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
label {
|
label {
|
||||||
r#for: "cbg-{todo.id}",
|
r#for: "cbg-{id}",
|
||||||
ondoubleclick: move |_| is_editing.set(true),
|
ondoubleclick: move |_| is_editing.set(true),
|
||||||
prevent_default: "onclick",
|
prevent_default: "onclick",
|
||||||
"{todo.contents}"
|
"{contents}"
|
||||||
}
|
}
|
||||||
button {
|
button {
|
||||||
class: "destroy",
|
class: "destroy",
|
||||||
onclick: move |_| {
|
onclick: move |_| { todos.write().remove(&id); },
|
||||||
cx.props.todos.make_mut().remove(&todo.id);
|
|
||||||
},
|
|
||||||
prevent_default: "onclick"
|
prevent_default: "onclick"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if **is_editing {
|
if *is_editing() {
|
||||||
input {
|
input {
|
||||||
class: "edit",
|
class: "edit",
|
||||||
value: "{todo.contents}",
|
value: "{contents}",
|
||||||
oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value(),
|
oninput: move |evt| todos.write().get_mut(&id).unwrap().contents = evt.value(),
|
||||||
autofocus: "true",
|
autofocus: "true",
|
||||||
onfocusout: move |_| is_editing.set(false),
|
onfocusout: move |_| is_editing.set(false),
|
||||||
onkeydown: move |evt| {
|
onkeydown: move |evt| {
|
||||||
@@ -183,39 +152,32 @@ pub fn TodoEntry(props: TodoEntryProps) -> Element {
|
|||||||
Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
|
Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Props)]
|
#[component]
|
||||||
pub struct ListFooterProps<'a> {
|
pub fn ListFooter(
|
||||||
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
todos: Signal<HashMap<u32, TodoItem>>,
|
||||||
active_todo_count: usize,
|
active_todo_count: ReadOnlySignal<usize>,
|
||||||
active_todo_text: &'a str,
|
filter: Signal<FilterState>,
|
||||||
show_clear_completed: bool,
|
) -> Element {
|
||||||
filter: &'a UseState<FilterState>,
|
let show_clear_completed = use_selector(move || todos().values().any(|todo| todo.checked));
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ListFooter(props: ListFooterProps) -> Element {
|
|
||||||
let active_todo_count = cx.props.active_todo_count;
|
|
||||||
let active_todo_text = cx.props.active_todo_text;
|
|
||||||
|
|
||||||
let selected = |state| {
|
|
||||||
if *cx.props.filter == state {
|
|
||||||
"selected"
|
|
||||||
} else {
|
|
||||||
"false"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
footer { class: "footer",
|
footer { class: "footer",
|
||||||
span { class: "todo-count",
|
span { class: "todo-count",
|
||||||
strong { "{active_todo_count} " }
|
strong { "{active_todo_count} " }
|
||||||
span { "{active_todo_text} left" }
|
span {
|
||||||
|
match *active_todo_count() {
|
||||||
|
1 => "item",
|
||||||
|
_ => "items",
|
||||||
|
}
|
||||||
|
" left"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ul { class: "filters",
|
ul { class: "filters",
|
||||||
for (state , state_text , url) in [
|
for (state , state_text , url) in [
|
||||||
@@ -226,18 +188,18 @@ pub fn ListFooter(props: ListFooterProps) -> Element {
|
|||||||
li {
|
li {
|
||||||
a {
|
a {
|
||||||
href: url,
|
href: url,
|
||||||
class: selected(state),
|
class: if *filter() == state { "selected" },
|
||||||
onclick: move |_| cx.props.filter.set(state),
|
onclick: move |_| filter.set(state),
|
||||||
prevent_default: "onclick",
|
prevent_default: "onclick",
|
||||||
{state_text}
|
{state_text}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cx.props.show_clear_completed {
|
if *show_clear_completed() {
|
||||||
button {
|
button {
|
||||||
class: "clear-completed",
|
class: "clear-completed",
|
||||||
onclick: move |_| cx.props.todos.make_mut().retain(|_, todo| !todo.checked),
|
onclick: move |_| todos.write().retain(|_, todo| !todo.checked),
|
||||||
"Clear completed"
|
"Clear completed"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user