Skip to content

Commit

Permalink
Fix no_children flag in yew-struct-component
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Nov 8, 2024
1 parent bf861eb commit 37b4a08
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/yew-struct-component-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -259,7 +265,7 @@ pub fn derive_struct_component(input: proc_macro::TokenStream) -> proc_macro::To
),)*
]));

tag.add_child(children);
#children

tag.into()
}
Expand Down
57 changes: 54 additions & 3 deletions packages/yew-struct-component/tests/struct_component.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<String>,
#[prop_or_default]
pub class: Option<String>,
#[prop_or_default]
pub style: Option<String>,

#[prop_or_default]
pub node_ref: NodeRef,
#[prop_or_default]
pub attributes: Option<Attributes>,
#[prop_or_default]
pub as_child: Option<Callback<BoxChildProps, Html>>,
#[prop_or_default]
Expand All @@ -47,6 +51,7 @@ pub struct BoxChildProps {
#[struct_component(dynamic_tag = true)]
pub r#as: BoxAs,
pub node_ref: NodeRef,
pub attributes: Option<Attributes>,
pub id: Option<String>,
pub class: Option<String>,
pub style: Option<String>,
Expand All @@ -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(),
Expand All @@ -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<String>,
#[prop_or_default]
pub class: Option<String>,
#[prop_or_default]
pub style: Option<String>,

#[prop_or_default]
pub node_ref: NodeRef,
#[prop_or_default]
pub attributes: Option<Attributes>,
#[prop_or_default]
pub as_child: Option<Callback<ImageChildProps, Html>>,
}

#[derive(Clone, PartialEq, StructComponent)]
#[struct_component(tag = "img", no_children = true)]
pub struct ImageChildProps {
pub node_ref: NodeRef,
pub attributes: Option<Attributes>,
pub id: Option<String>,
pub class: Option<String>,
pub style: Option<String>,
}

#[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()
}
}

0 comments on commit 37b4a08

Please sign in to comment.