forked from yewstack/yew
/
lib.rs
82 lines (72 loc) · 1.81 KB
/
lib.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
78
79
80
81
82
#![recursion_limit = "128"]
mod barrier;
mod button;
mod counter;
use barrier::Barrier;
use counter::{Color, Counter};
use yew::prelude::*;
pub struct Model {
link: ComponentLink<Self>,
with_barrier: bool,
color: Color,
}
pub enum Msg {
Repaint,
Toggle,
ChildClicked(u32),
}
impl Component for Model {
type Message = Msg;
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
Model {
link,
with_barrier: false,
color: Color::Red,
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Repaint => {
self.color = Color::Blue;
true
}
Msg::Toggle => {
self.with_barrier = !self.with_barrier;
true
}
Msg::ChildClicked(_value) => false,
}
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
let counter = |x| {
html! {
<Counter initial=x color=&self.color
onclick=self.link.callback(Msg::ChildClicked) />
}
};
html! {
<div class="custom-components-example">
<button onclick=self.link.callback(|_| Msg::Toggle)>{ "Toggle" }</button>
{ self.view_barrier() }
{ for (1..1001).map(counter) }
</div>
}
}
}
impl Model {
fn view_barrier(&self) -> Html {
if self.with_barrier {
html! {
<Barrier limit=10 onsignal=self.link.callback(|_| Msg::Repaint) />
}
} else {
html! {
<p>{ "Click \"toggle\"!" }</p>
}
}
}
}