Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strum #3

Merged
merged 4 commits into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/todomvc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/todomvc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ version = "0.1.0"
authors = ["Denis Kolodin <deniskolodin@gmail.com>"]

[dependencies]
strum = "0.8.0"
strum_macros = "0.8.0"
yew = { path = "../.." }
70 changes: 32 additions & 38 deletions examples/todomvc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#![recursion_limit="128"]

extern crate strum;
#[macro_use]
extern crate strum_macros;
#[macro_use]
extern crate yew;

use strum::IntoEnumIterator;
use yew::html::*;

#[derive(Clone, PartialEq)]
#[derive(EnumIter, ToString, Clone, PartialEq)]
enum Filter {
All,
Active,
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 {
Expand All @@ -22,17 +36,6 @@ impl Filter {
}
}

impl ToString for Filter {
fn to_string(&self) -> String {
let name = match *self {
Filter::All => "All",
Filter::Active => "Active",
Filter::Completed => "Completed",
};
name.to_string()
}
}

struct Model {
entries: Vec<Entry>,
filter: Filter,
Expand Down Expand Up @@ -160,29 +163,17 @@ fn view(model: &Model) -> Html<Msg> {
</header>
<section class="main",>
<input class="toggle-all", type="checkbox", checked=model.is_all_completed(), onclick=|_| Msg::ToggleAll, />
{ view_entries(&model) }
<ul class="todo-list",>
{ for model.entries.iter().filter(|e| model.filter.fit(e)).enumerate().map(view_entry) }
</ul>
</section>
<footer class="footer",>
<span class="todo-count",>
<strong>{ model.total() }</strong>
{ " item(s) left" }
</span>
<ul class="filters",>
<li>
<a href="#/", onclick=|_| Msg::SetFilter(Filter::All),>
{ Filter::All }
</a>
</li>
<li>
<a href="#/active", onclick=|_| Msg::SetFilter(Filter::Active),>
{ Filter::Active }
</a>
</li>
<li>
<a href="#/completed", onclick=|_| Msg::SetFilter(Filter::Completed),>
{ Filter::Completed }
</a>
</li>
{ for Filter::iter().map(|flt| view_filter(&model, flt)) }
</ul>
<button class="clear-completed", onclick=|_| Msg::ClearCompleted,>
{ format!("Clear completed ({})", model.total_completed()) }
Expand All @@ -198,25 +189,28 @@ fn view(model: &Model) -> Html<Msg> {
}
}

fn view_filter(_model: &Model, filter: Filter) -> Html<Msg> {
let flt = filter.clone();
html! {
<li>
<a href=&flt, onclick=move |_| Msg::SetFilter(flt.clone()),>
{ filter }
</a>
</li>
}
}

fn view_input(model: &Model) -> Html<Msg> {
html! {
// You can use standard Rust comments. One line:
// <li></li>
<input class="new-todo",
placeholder="What needs to be done?",
value=&model.value,
oninput=|e: InputData| Msg::Update(e.value),
onkeypress=|e: KeyData| {
if e.key == "Enter" { Msg::Add } else { Msg::Nope }
}, />
}
}

fn view_entries(model: &Model) -> Html<Msg> {
html! {
<ul class="todo-list",>
{ for model.entries.iter().filter(|e| model.filter.fit(e)).enumerate().map(view_entry) }
// You can use standard Rust comments. One line:
// <li></li>
</ul>
/* Or multiline:
<ul>
<li></li>
Expand Down
23 changes: 23 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

6 changes: 6 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*) }
Expand Down