-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
main.rs
77 lines (69 loc) · 2.16 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use wasm_bindgen::prelude::*;
use web_sys::HtmlTextAreaElement;
use yew::prelude::*;
mod bindings;
pub enum Msg {
Payload(String),
AsyncPayload,
}
pub struct App {
payload: String,
// Pointless field just to have something that's been manipulated
debugged_payload: String,
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
Self {
payload: String::default(),
debugged_payload: format!("{:?}", ""),
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Payload(payload) => {
if payload != self.payload {
self.debugged_payload = format!("{:?}", payload);
self.payload = payload;
true
} else {
false
}
}
Msg::AsyncPayload => {
let callback = ctx.link().callback(Msg::Payload);
bindings::get_payload_later(Closure::once_into_js(move |payload: String| {
callback.emit(payload)
}));
false
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<>
<textarea
class="code-block"
oninput={ctx.link().callback(|e: InputEvent| {
let input: HtmlTextAreaElement = e.target_unchecked_into();
Msg::Payload(input.value())
})}
value={self.payload.clone()}
/>
<button onclick={ctx.link().callback(|_| Msg::Payload(bindings::get_payload()))}>
{ "Get the payload!" }
</button>
<button onclick={ctx.link().callback(|_| Msg::AsyncPayload)} >
{ "Get the payload later!" }
</button>
<p class="code-block">
{ &self.debugged_payload }
</p>
</>
}
}
}
fn main() {
yew::Renderer::<App>::new().render();
}