Skip to content

Commit

Permalink
Remove generics from virtual dom (yewstack#783)
Browse files Browse the repository at this point in the history
* Fix tests

* Remove generics from virtual dom

* Prep for degenerify

* Fix examples

* Remove props cloning

* Fix tests
  • Loading branch information
jstarry authored and llebout committed Jan 20, 2020
1 parent f08fc3f commit 11e3c95
Show file tree
Hide file tree
Showing 67 changed files with 459 additions and 688 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Component for Model {
}
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
let onclick = self.link.callback(|_| Msg::DoIt);
html! {
// Render your model here
Expand Down Expand Up @@ -323,8 +323,8 @@ Use external crates and put values from them into the template:
extern crate chrono;
use chrono::prelude::*;

impl Renderable<Model> for Model {
fn render(&self) -> Html<Self> {
impl Renderable for Model {
fn render(&self) -> Html {
html! {
<p>{ Local::now() }</p>
}
Expand Down
13 changes: 6 additions & 7 deletions crates/macro/src/html_tree/html_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ impl ToTokens for HtmlComponent {
props,
children,
} = self;
let vcomp_scope = Ident::new("__yew_vcomp_scope", Span::call_site());

let validate_props = if let Props::List(ListProps { props, .. }) = props {
let prop_ref = Ident::new("__yew_prop_ref", Span::call_site());
Expand Down Expand Up @@ -139,9 +138,11 @@ impl ToTokens for HtmlComponent {
let init_props = match props {
Props::List(ListProps { props, .. }) => {
let set_props = props.iter().map(|HtmlProp { label, value }| {
quote_spanned! { value.span()=>
.#label(<::yew::virtual_dom::vcomp::VComp<_> as ::yew::virtual_dom::Transformer<_, _, _>>::transform(#vcomp_scope.clone(), #value))
}
quote_spanned! { value.span()=> .#label(
<::yew::virtual_dom::vcomp::VComp as ::yew::virtual_dom::Transformer<_, _>>::transform(
#value
)
)}
});

quote! {
Expand Down Expand Up @@ -181,9 +182,7 @@ impl ToTokens for HtmlComponent {
#validate_props
}

let #vcomp_scope: ::yew::html::ScopeHolder<_> = ::std::default::Default::default();
let __yew_node_ref: ::yew::html::NodeRef = #node_ref;
::yew::virtual_dom::VChild::<#ty, _>::new(#init_props, #vcomp_scope, __yew_node_ref)
::yew::virtual_dom::VChild::<#ty>::new(#init_props, #node_ref)
}});
}
}
Expand Down
26 changes: 6 additions & 20 deletions crates/macro/src/html_tree/html_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,12 @@ impl Parse for HtmlPropSuffix {
if let TokenTree::Punct(punct) = &next {
match punct.as_char() {
'>' => {
let possible_tag_end = input.peek(Token![<])
|| input.peek(syn::token::Brace)
|| input.is_empty();

if angle_count > 1 || possible_tag_end {
angle_count -= 1;
if angle_count == 0 {
gt = Some(syn::token::Gt {
spans: [punct.span()],
});
break;
}
angle_count -= 1;
if angle_count == 0 {
gt = Some(syn::token::Gt {
spans: [punct.span()],
});
break;
}
}
'<' => angle_count += 1,
Expand All @@ -73,14 +67,6 @@ impl Parse for HtmlPropSuffix {
break;
}
}
'-' => {
if input.peek(Token![>]) {
// Handle explicit return types in callbacks (#560)
// We increase angle_count here in order to ignore
// the following >.
angle_count += 1;
}
}
_ => {}
};
}
Expand Down
8 changes: 3 additions & 5 deletions crates/macro/src/html_tree/html_tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl ToTokens for HtmlTag {
} = &attributes;

let vtag = Ident::new("__yew_vtag", tag_name.span());
let vtag_scope = Ident::new("__yew_vtag_scope", Span::call_site());
let attr_pairs = attributes.iter().map(|TagAttribute { label, value }| {
let label_str = label.to_string();
quote_spanned! {value.span() => (#label_str.to_owned(), (#value).to_string()) }
Expand Down Expand Up @@ -155,16 +154,15 @@ impl ToTokens for HtmlTag {

quote_spanned! {name.span()=> {
::yew::html::#name::Wrapper::new(
<::yew::virtual_dom::vtag::VTag<_> as ::yew::virtual_dom::Transformer<_, _, _>>::transform(
#vtag_scope.clone(), #callback
<::yew::virtual_dom::vtag::VTag as ::yew::virtual_dom::Transformer<_, _>>::transform(
#callback
)
)
}}
});

tokens.extend(quote! {{
let #vtag_scope: ::yew::html::ScopeHolder<_> = ::std::default::Default::default();
let mut #vtag = ::yew::virtual_dom::vtag::VTag::new_with_scope(#name, #vtag_scope.clone());
let mut #vtag = ::yew::virtual_dom::vtag::VTag::new(#name);
#(#set_kind)*
#(#set_value)*
#(#add_href)*
Expand Down
2 changes: 1 addition & 1 deletion crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! # unimplemented!()
//! # }
//! #
//! # fn view(&self) -> Html<Self> {
//! # fn view(&self) -> Html {
//! #
//! // ...
//!
Expand Down
2 changes: 1 addition & 1 deletion examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
html! {
<div>
<nav class="menu">
Expand Down
12 changes: 6 additions & 6 deletions examples/crm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
match self.scene {
Scene::ClientsList => html! {
<div class="crm">
Expand Down Expand Up @@ -179,8 +179,8 @@ impl Component for Model {
}
}

impl Renderable<Model> for Client {
fn render(&self) -> Html<Model> {
impl Renderable for Client {
fn render(&self) -> Html {
html! {
<div class="client">
<p>{ format!("First Name: {}", self.first_name) }</p>
Expand All @@ -193,7 +193,7 @@ impl Renderable<Model> for Client {
}

impl Client {
fn view_first_name_input(&self, link: &ComponentLink<Model>) -> Html<Model> {
fn view_first_name_input(&self, link: &ComponentLink<Model>) -> Html {
html! {
<input class="new-client firstname"
placeholder="First name"
Expand All @@ -202,15 +202,15 @@ impl Client {
}
}

fn view_last_name_input(&self, link: &ComponentLink<Model>) -> Html<Model> {
fn view_last_name_input(&self, link: &ComponentLink<Model>) -> Html {
html! {
<input class="new-client lastname"
placeholder="Last name"
value=&self.last_name
oninput=link.callback(|e: InputData| Msg::UpdateLastName(e.value)) />
}
}
fn view_description_textarea(&self, link: &ComponentLink<Model>) -> Html<Model> {
fn view_description_textarea(&self, link: &ComponentLink<Model>) -> Html {
html! {
<textarea class=("new-client", "description")
placeholder="Description"
Expand Down
12 changes: 3 additions & 9 deletions examples/crm/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
/// Source: https://github.com/acmumn/mentoring/blob/master/web-client/src/view/markdown.rs
use pulldown_cmark::{Alignment, Event, Parser, Tag, OPTION_ENABLE_TABLES};
use yew::virtual_dom::{VNode, VTag, VText};
use yew::{html, Component, Html};
use yew::{html, Html};

/// Renders a string of Markdown to HTML with the default options (footnotes
/// disabled, tables enabled).
pub fn render_markdown<COMP>(src: &str) -> Html<COMP>
where
COMP: Component,
{
pub fn render_markdown(src: &str) -> Html {
let mut elems = vec![];
let mut spine = vec![];

Expand Down Expand Up @@ -81,10 +78,7 @@ where
}
}

fn make_tag<COMP>(t: Tag) -> VTag<COMP>
where
COMP: Component,
{
fn make_tag(t: Tag) -> VTag {
match t {
Tag::Paragraph => VTag::new("p"),
Tag::Rule => VTag::new("hr"),
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_components/src/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Component for Barrier {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
let onsignal = &self.link.callback(|_| Msg::ChildClicked);
html! {
<div class="barrier">
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_components/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Component for Button {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
html! {
<button onclick=self.link.callback(|_| Msg::Clicked)>
{ &self.title }
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_components/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Component for Counter {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
let colorize = {
match self.color {
Color::Red => "background: red;",
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Component for Model {
}
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
let counter = |x| {
html! {
<Counter initial=x color=&self.color
Expand All @@ -64,7 +64,7 @@ impl Component for Model {
}

impl Model {
fn view_barrier(&self) -> Html<Self> {
fn view_barrier(&self) -> Html {
if self.with_barrier {
html! {
<Barrier limit=10 onsignal=self.link.callback(|_| Msg::Repaint) />
Expand Down
4 changes: 2 additions & 2 deletions examples/dashboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
html! {
<div>
<nav class="menu">
Expand Down Expand Up @@ -206,7 +206,7 @@ impl Component for Model {
}

impl Model {
fn view_data(&self) -> Html<Self> {
fn view_data(&self) -> Html {
if let Some(value) = self.data {
html! {
<p>{ value }</p>
Expand Down
4 changes: 2 additions & 2 deletions examples/file_upload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
let flag = self.by_chunks;
html! {
<div>
Expand All @@ -91,7 +91,7 @@ impl Component for Model {
}

impl Model {
fn view_file(&self, data: &str) -> Html<Self> {
fn view_file(&self, data: &str) -> Html {
html! {
<li>{ data }</li>
}
Expand Down
6 changes: 3 additions & 3 deletions examples/fragments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
html! {
<>
<nav class="menu">{ self.view_menu() }</nav>
Expand All @@ -54,7 +54,7 @@ impl Component for Model {
}

impl Model {
fn view_cols(&self) -> Html<Self> {
fn view_cols(&self) -> Html {
let render = |idx| {
html! {
<td>{ idx }</td>
Expand All @@ -65,7 +65,7 @@ impl Model {
}
}

fn view_menu(&self) -> Html<Self> {
fn view_menu(&self) -> Html {
html! {
<>
<button onclick=self.link.callback(|_| Msg::More)>{ "More" }</button>
Expand Down
2 changes: 1 addition & 1 deletion examples/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Component for Model {
}
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
match &self.markdown {
FetchState::NotFetching => html! {
<button onclick=self.link.callback(|_| Msg::GetMarkdown)>
Expand Down
4 changes: 2 additions & 2 deletions examples/game_of_life/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
html! {
<div>
<section class="game-container">
Expand Down Expand Up @@ -241,7 +241,7 @@ impl Component for Model {
}

impl Model {
fn view_cellule(&self, (idx, cellule): (usize, &Cellule)) -> Html<Self> {
fn view_cellule(&self, (idx, cellule): (usize, &Cellule)) -> Html {
let cellule_status = {
if cellule.life_state == LifeState::Alive {
"cellule-live"
Expand Down
2 changes: 1 addition & 1 deletion examples/inner_html/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
let js_svg = js! {
var div = document.createElement("div");
div.innerHTML = @{SVG.to_string()};
Expand Down
2 changes: 1 addition & 1 deletion examples/js_callback/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Component for Model {
false
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
html! {
<div>
<textarea oninput=|input| Msg::Payload(input.value)
Expand Down
8 changes: 4 additions & 4 deletions examples/large_table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ impl Component for Model {
true
}

fn view(&self) -> Html<Self> {
fn view(&self) -> Html {
html! {
<table>
{ (0..99).map(|row| self.view_row(row)).collect::<Html<Self>>() }
{ (0..99).map(|row| self.view_row(row)).collect::<Html>() }
</table>
}
}
}

impl Model {
fn view_square(&self, row: u32, column: u32) -> Html<Self> {
fn view_square(&self, row: u32, column: u32) -> Html {
html! {
<td class=square_class((column, row), self.selected)
onclick=self.link.callback(move |_| Msg::Select(column, row))>
</td>
}
}

fn view_row(&self, row: u32) -> Html<Self> {
fn view_row(&self, row: u32) -> Html {
html! {
<tr>
{for (0..99).map(|column| {
Expand Down
Loading

0 comments on commit 11e3c95

Please sign in to comment.