diff --git a/packages/yew-struct-component-macro/src/lib.rs b/packages/yew-struct-component-macro/src/lib.rs index c9283cf..fe9deaf 100644 --- a/packages/yew-struct-component-macro/src/lib.rs +++ b/packages/yew-struct-component-macro/src/lib.rs @@ -232,6 +232,12 @@ pub fn derive_struct_component(input: proc_macro::TokenStream) -> proc_macro::To } }; + let children = (!args.no_children.unwrap_or(false)).then(|| { + quote! { + tag.add_child(children); + } + }); + quote! { impl #ident { pub fn render(#arguments) -> ::yew::prelude::Html { @@ -259,7 +265,7 @@ pub fn derive_struct_component(input: proc_macro::TokenStream) -> proc_macro::To ),)* ])); - tag.add_child(children); + #children tag.into() } diff --git a/packages/yew-struct-component/tests/struct_component.rs b/packages/yew-struct-component/tests/struct_component.rs index 787b88a..5be3ad8 100644 --- a/packages/yew-struct-component/tests/struct_component.rs +++ b/packages/yew-struct-component/tests/struct_component.rs @@ -1,7 +1,7 @@ use std::fmt::{self, Display}; use yew::prelude::*; -use yew_struct_component::{struct_component, StructComponent}; +use yew_struct_component::{struct_component, Attributes, StructComponent}; #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub enum BoxAs { @@ -28,14 +28,18 @@ pub struct BoxProps { #[prop_or_default] pub r#as: BoxAs, - #[prop_or_default] - pub node_ref: NodeRef, + // Global attributes #[prop_or_default] pub id: Option, #[prop_or_default] pub class: Option, #[prop_or_default] pub style: Option, + + #[prop_or_default] + pub node_ref: NodeRef, + #[prop_or_default] + pub attributes: Option, #[prop_or_default] pub as_child: Option>, #[prop_or_default] @@ -47,6 +51,7 @@ pub struct BoxChildProps { #[struct_component(dynamic_tag = true)] pub r#as: BoxAs, pub node_ref: NodeRef, + pub attributes: Option, pub id: Option, pub class: Option, pub style: Option, @@ -57,6 +62,7 @@ pub fn Box(props: &BoxProps) -> Html { let child_props = BoxChildProps { r#as: props.r#as, node_ref: props.node_ref.clone(), + attributes: props.attributes.clone(), id: props.id.clone(), class: props.class.clone(), style: props.style.clone(), @@ -68,3 +74,48 @@ pub fn Box(props: &BoxProps) -> Html { child_props.render(props.children.clone()) } } + +#[derive(PartialEq, Properties)] +pub struct ImageProps { + // Global attributes + #[prop_or_default] + pub id: Option, + #[prop_or_default] + pub class: Option, + #[prop_or_default] + pub style: Option, + + #[prop_or_default] + pub node_ref: NodeRef, + #[prop_or_default] + pub attributes: Option, + #[prop_or_default] + pub as_child: Option>, +} + +#[derive(Clone, PartialEq, StructComponent)] +#[struct_component(tag = "img", no_children = true)] +pub struct ImageChildProps { + pub node_ref: NodeRef, + pub attributes: Option, + pub id: Option, + pub class: Option, + pub style: Option, +} + +#[function_component] +pub fn Image(props: &ImageProps) -> Html { + let child_props = ImageChildProps { + node_ref: props.node_ref.clone(), + attributes: props.attributes.clone(), + id: props.id.clone(), + class: props.class.clone(), + style: props.style.clone(), + }; + + if let Some(as_child) = &props.as_child { + as_child.emit(child_props) + } else { + child_props.render() + } +}