2022-07-07 08:50:36 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(App);
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Clone)]
|
|
|
|
struct Comment {
|
|
|
|
content: String,
|
|
|
|
id: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn App(cx: Scope) -> Element {
|
|
|
|
// ANCHOR: render_list
|
2022-09-13 23:22:27 +00:00
|
|
|
let comment_field = use_state(&cx, String::new);
|
2022-07-07 08:50:36 +00:00
|
|
|
let mut next_id = use_state(&cx, || 0);
|
2022-09-13 23:22:27 +00:00
|
|
|
let comments = use_ref(&cx, Vec::<Comment>::new);
|
2022-07-07 08:50:36 +00:00
|
|
|
|
|
|
|
let comments_lock = comments.read();
|
|
|
|
let comments_rendered = comments_lock.iter().map(|comment| {
|
|
|
|
cx.render(rsx!(CommentComponent {
|
|
|
|
key: "{comment.id}",
|
|
|
|
comment: comment.clone(),
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.render(rsx!(
|
|
|
|
form {
|
|
|
|
onsubmit: move |_| {
|
|
|
|
comments.write().push(Comment {
|
|
|
|
content: comment_field.get().clone(),
|
|
|
|
id: *next_id.get(),
|
|
|
|
});
|
|
|
|
next_id += 1;
|
|
|
|
|
|
|
|
comment_field.set(String::new());
|
|
|
|
},
|
|
|
|
input {
|
|
|
|
value: "{comment_field}",
|
|
|
|
oninput: |event| comment_field.set(event.value.clone()),
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
r#type: "submit",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
comments_rendered,
|
|
|
|
))
|
|
|
|
// ANCHOR_END: render_list
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline_props]
|
|
|
|
fn CommentComponent(cx: Scope, comment: Comment) -> Element {
|
|
|
|
cx.render(rsx!(div {
|
|
|
|
"Comment by anon:",
|
|
|
|
p { "{comment.content}" }
|
|
|
|
button { "Reply" },
|
|
|
|
}))
|
|
|
|
}
|