yew/yew-router/src/components/router_link.rs

88 lines
2.5 KiB
Rust

//! A component wrapping an `<a>` tag that changes the route.
use crate::{
agent::{RouteAgentDispatcher, RouteRequest},
route::Route,
Switch,
};
use yew::prelude::*;
use super::{Msg, Props};
use crate::RouterState;
use yew::virtual_dom::VNode;
/// An anchor tag Component that when clicked, will navigate to the provided route.
///
/// Alias to RouterAnchor.
#[deprecated(note = "Has been renamed to RouterAnchor")]
pub type RouterLink<T> = RouterAnchor<T>;
/// An anchor tag Component that when clicked, will navigate to the provided route.
#[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>
}
}
}