Skip to content

Commit

Permalink
it works
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Jan 23, 2022
1 parent 03250f2 commit 507c2ee
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
1 change: 1 addition & 0 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use gloo_console as console;
use js_sys::Date;
use yew::{html, Component, Context, Html};
use yew::virtual_dom::typings::*;

// Define the possible messages which can be sent to the component
pub enum Msg {
Expand Down
6 changes: 1 addition & 5 deletions packages/yew-macro/src/html_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,8 @@ impl HtmlTree {

if input.peek(Token![=]) || (input.peek(Token![?]) && input.peek2(Token![=])) {
Some(HtmlType::List)
} else if ident_str.chars().next().unwrap().is_ascii_uppercase()
|| input.peek(Token![::])
{
Some(HtmlType::Component)
} else {
Some(HtmlType::Element)
Some(HtmlType::Component)
}
} else {
None
Expand Down
10 changes: 5 additions & 5 deletions packages/yew-macro/src/typed_vdom/generate_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ impl ToTokens for GenerateElement {

let out = quote! {
#[allow(non_camel_case_types)]
struct #element_name;
pub struct #element_name;

#[derive(::std::default::Default, ::std::clone::Clone, ::std::fmt::Debug, ::yew::html::Properties, ::std::cmp::PartialEq)]
struct #props_ident {
node_ref: ::std::option::Option::<::yew::NodeRef>,
key: ::std::option::Option::<::yew::virtual_dom::Key>,
children: ::yew::Children,
pub struct #props_ident {
pub node_ref: ::std::option::Option::<::yew::NodeRef>,
pub key: ::std::option::Option::<::yew::virtual_dom::Key>,
pub children: ::yew::Children,
#(#props)*
#(#listeners)*
}
Expand Down
28 changes: 13 additions & 15 deletions packages/yew-macro/src/typed_vdom/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream};
use syn::{parse_quote, Token, Type};
use syn::{LitStr, parse_quote, Token, Type};
use syn::ext::IdentExt;
use syn::parse::{Parse, ParseStream};

pub mod generate_element;
mod globals;

#[derive(Clone)]
pub struct AttributePropDefinition {
name: Ident,
ty: Type,
}

impl Parse for AttributePropDefinition {
fn parse(input: ParseStream) -> syn::Result<Self> {
let name = Ident::parse_any(input)?;
Expand All @@ -19,6 +21,7 @@ impl Parse for AttributePropDefinition {
Ok(Self { name, ty })
}
}

impl AttributePropDefinition {
pub fn new(name: Ident, ty: Type) -> Self {
Self { name, ty }
Expand All @@ -30,24 +33,18 @@ impl AttributePropDefinition {
pub #name: ::std::option::Option::<#ty>,
}
}
fn _build_setter(&self) -> TokenStream {
let AttributePropDefinition { ty, .. } = self;
let name = self.name();
quote! {
pub fn #name(&mut self, val: #ty) {
self.#name = ::std::option::Option::Some(val);
}
}
}

fn name(&self) -> Ident {
format_ident!("r#{}", self.name)
}

fn build_if_lets(&self) -> TokenStream {
let ident = self.name();
let name = self.name.to_string().replace('_', "-");
let name = LitStr::new(&name, self.name.span());
quote! {
if let Some(val) = self.#ident {
attrs.insert(::std::stringify!(#name), val);
attrs.insert(#name, val);
}
}
}
Expand All @@ -58,6 +55,7 @@ pub struct ListenerPropDefinition {
event: Ident,
ty: Type,
}

impl ListenerPropDefinition {
fn new(event: Ident) -> Self {
Self {
Expand All @@ -69,7 +67,7 @@ impl ListenerPropDefinition {
Self { event, ty }
}
fn ident(&self) -> Ident {
format_ident!("on_{}", self.event, span = self.event.span())
format_ident!("on{}", self.event, span = self.event.span())
}
fn build_fields(&self) -> TokenStream {
let ident = self.ident();
Expand All @@ -80,14 +78,14 @@ impl ListenerPropDefinition {
}
fn build_if_lets(&self) -> TokenStream {
let ident = self.ident();
let on_event = Ident::new(&format!("on{}", self.event), Span::mixed_site());
quote! {
if let Some(value) = self.#ident {
listeners.push(::yew::html::#on_event::Wrapper::__macro_new(value));
listeners.push(::yew::html::#ident::Wrapper::__macro_new(value));
}
}
}
}

pub(crate) mod kw {
syn::custom_keyword!(props);
}
1 change: 1 addition & 0 deletions packages/yew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pub mod macros {
pub use crate::html;
pub use crate::html_nested;
pub use crate::props;
pub use yew_macro::generate_element;
}

mod app_handle;
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub mod key;
#[doc(hidden)]
pub mod listeners;
mod typings;
pub mod typings;
#[doc(hidden)]
pub mod vcomp;
#[doc(hidden)]
Expand Down
3 changes: 2 additions & 1 deletion packages/yew/src/virtual_dom/typings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![allow(missing_docs)]
//! This module contains the items required for a statically typed VDOM
use std::collections::HashMap;
use std::rc::Rc;

use yew_macro::generate_element;
use crate::macros::generate_element;

use crate::virtual_dom::{AttrValue, Key, Listener, VNode};
use crate::{NodeRef};
Expand Down

0 comments on commit 507c2ee

Please sign in to comment.