/
counter.rs
46 lines (42 loc) · 1.16 KB
/
counter.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
use std::rc::Rc;
use virtual_dom_rs::VirtualNode;
pub struct Counter {}
pub struct CounterProps {
pub count: i32,
pub increment: Box<Fn()>,
pub decrement: Box<Fn()>,
pub auto: Box<Fn()>,
}
impl Counter {
pub fn new() -> Counter {
Counter {}
}
pub fn render(&self, props: Rc<CounterProps>) -> VirtualNode {
// we clone ref to our props if we have more than event handler
// otherwise we move it twice
let p2 = props.clone();
let p3 = props.clone();
html! {
<div>
{format!("{}",props.count)}
<div class="button",
!onclick=move |_ev| {
(props.increment)()
},
>
{ "+" }
</div>
<div class="button",
!onclick=move |_ev| { (p2.decrement)() },
>
{ "-" }
</div>
<div class="button",
!onclick=move |_ev| { (p3.auto)() },
>
{ "auto" }
</div>
</div>
}
}
}