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
83
84
85
86
87
use crate::{
agent::{RouteAgentDispatcher, RouteRequest},
route::Route,
Switch,
};
use yew::prelude::*;
use super::{Msg, Props};
use crate::RouterState;
use yew::virtual_dom::VNode;
#[deprecated(note = "Has been renamed to RouterAnchor")]
pub type RouterLink<T> = RouterAnchor<T>;
#[derive(Debug)]
pub struct RouterAnchor<SW: Switch + Clone + 'static, STATE: RouterState = ()> {
link: ComponentLink<Self>,
router: RouteAgentDispatcher<STATE>,
props: Props<SW>,
}
impl<SW: Switch + Clone + 'static, STATE: RouterState> Component for RouterAnchor<SW, STATE> {
type Message = Msg;
type Properties = Props<SW>;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
let router = RouteAgentDispatcher::new();
RouterAnchor {
link,
router,
props,
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Clicked => {
let route = Route::from(self.props.route.clone());
self.router.send(RouteRequest::ChangeRoute(route));
false
}
}
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.props = props;
true
}
fn view(&self) -> VNode {
#[cfg(feature = "std_web")]
use stdweb::web::event::IEvent;
let route: Route<STATE> = Route::from(self.props.route.clone());
let target: &str = route.as_str();
#[cfg(feature = "std_web")]
let cb = self.link.callback(|event: ClickEvent| {
event.prevent_default();
Msg::Clicked
});
#[cfg(feature = "web_sys")]
let cb = self.link.callback(|event: MouseEvent| {
event.prevent_default();
Msg::Clicked
});
html! {
<a
class=self.props.classes.clone(),
onclick=cb,
disabled=self.props.disabled,
href=target,
>
{
#[allow(deprecated)]
&self.props.text
}
{self.props.children.iter().collect::<VNode>()}
</a>
}
}
}