mirror of
https://github.com/yewstack/yew.git
synced 2026-02-01 17:26:28 +00:00
Add conversion to Href
This commit is contained in:
parent
89a580b646
commit
d2e770e44d
@ -16,6 +16,16 @@ enum Filter {
|
||||
Completed,
|
||||
}
|
||||
|
||||
impl<'a> Into<Href> for &'a Filter {
|
||||
fn into(self) -> Href {
|
||||
match *self {
|
||||
Filter::All => "#/".into(),
|
||||
Filter::Active => "#/active".into(),
|
||||
Filter::Completed => "#/completed".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Filter {
|
||||
fn fit(&self, entry: &Entry) -> bool {
|
||||
match *self {
|
||||
@ -183,7 +193,7 @@ fn view_filter(_model: &Model, filter: Filter) -> Html<Msg> {
|
||||
let flt = filter.clone();
|
||||
html! {
|
||||
<li>
|
||||
<a href="#/", onclick=move |_| Msg::SetFilter(flt.clone()),>
|
||||
<a href=&flt, onclick=move |_| Msg::SetFilter(flt.clone()),>
|
||||
{ filter }
|
||||
</a>
|
||||
</li>
|
||||
|
||||
23
src/html.rs
23
src/html.rs
@ -165,3 +165,26 @@ impl<T: IKeyboardEvent> From<T> for KeyData {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Href {
|
||||
link: String,
|
||||
}
|
||||
|
||||
impl From<String> for Href {
|
||||
fn from(link: String) -> Self {
|
||||
Href { link }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for Href {
|
||||
fn from(link: &'a str) -> Self {
|
||||
Href { link: link.to_owned() }
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Href {
|
||||
fn to_string(&self) -> String {
|
||||
self.link.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -49,6 +49,12 @@ macro_rules! html_impl {
|
||||
$crate::macros::attach_listener(&mut $stack, Box::new(listener));
|
||||
html_impl! { $stack ($($tail)*) }
|
||||
};
|
||||
// Attributes:
|
||||
($stack:ident (href = $href:expr, $($tail:tt)*)) => {
|
||||
let href: $crate::html::Href = $href.into();
|
||||
$crate::macros::add_attribute(&mut $stack, "href", href);
|
||||
html_impl! { $stack ($($tail)*) }
|
||||
};
|
||||
($stack:ident ($attr:ident = $val:expr, $($tail:tt)*)) => {
|
||||
$crate::macros::add_attribute(&mut $stack, stringify!($attr), $val);
|
||||
html_impl! { $stack ($($tail)*) }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user