fix spreading attributes

This commit is contained in:
Evan Almloff 2024-01-16 11:04:37 -06:00
parent d44b0b34c8
commit 8d14671520
5 changed files with 8 additions and 55 deletions

View file

@ -9,7 +9,7 @@ fn main() {
fn app() -> Element { fn app() -> Element {
render! { render! {
Component { spreadable_component {
width: "10px", width: "10px",
extra_data: "hello{1}", extra_data: "hello{1}",
extra_data2: "hello{2}", extra_data2: "hello{2}",
@ -19,8 +19,7 @@ fn app() -> Element {
} }
} }
#[component] fn spreadable_component(props: Props) -> Element {
fn Component(props: Props) -> Element {
render! { render! {
audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" } audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
} }

View file

@ -38,8 +38,8 @@ impl AttributeType {
impl ToTokens for AttributeType { impl ToTokens for AttributeType {
fn to_tokens(&self, tokens: &mut TokenStream2) { fn to_tokens(&self, tokens: &mut TokenStream2) {
match self { match self {
AttributeType::Named(n) => tokens.append_all(quote! { #n }), AttributeType::Named(n) => tokens.append_all(quote! { Box::new([#n]) }),
AttributeType::Spread(e) => tokens.append_all(quote! { (&#e).into() }), AttributeType::Spread(e) => tokens.append_all(quote! { #e.into_boxed_slice() }),
} }
} }
} }

View file

@ -243,54 +243,6 @@ Like so:
} }
} }
impl ToTokens for Element {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let name = &self.name;
let children = &self.children;
let key = match &self.key {
Some(ty) => quote! { Some(#ty) },
None => quote! { None },
};
let listeners = self.merged_attributes.iter().filter(|f| {
matches!(
f,
AttributeType::Named(ElementAttrNamed {
attr: ElementAttr {
value: ElementAttrValue::EventTokens { .. },
..
},
..
})
)
});
let attr = self.merged_attributes.iter().filter(|f| {
!matches!(
f,
AttributeType::Named(ElementAttrNamed {
attr: ElementAttr {
value: ElementAttrValue::EventTokens { .. },
..
},
..
})
)
});
tokens.append_all(quote! {
dioxus_core::Element::new(
#name,
vec![ #(#listeners),* ],
vec![ #(#attr),* ],
vec![ #(#children),* ],
#key,
)
});
}
}
#[derive(PartialEq, Eq, Clone, Debug, Hash)] #[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum ElementName { pub enum ElementName {
Ident(Ident), Ident(Ident),

View file

@ -257,7 +257,7 @@ impl<'a> ToTokens for TemplateRenderer<'a> {
#key_tokens, #key_tokens,
TEMPLATE, TEMPLATE,
Box::new([ #( #node_printer),* ]), Box::new([ #( #node_printer),* ]),
Box::new([ #( Box::new([ #( #dyn_attr_printer),* ]) ),* ]), Box::new([ #( #(#dyn_attr_printer),* ),* ]),
) )
}); });
} }

View file

@ -139,7 +139,9 @@ impl Parse for BodyNode {
impl ToTokens for BodyNode { impl ToTokens for BodyNode {
fn to_tokens(&self, tokens: &mut TokenStream2) { fn to_tokens(&self, tokens: &mut TokenStream2) {
match &self { match &self {
BodyNode::Element(el) => el.to_tokens(tokens), BodyNode::Element(_) => {
unimplemented!("Elements are statically created in the template")
}
BodyNode::Component(comp) => comp.to_tokens(tokens), BodyNode::Component(comp) => comp.to_tokens(tokens),
BodyNode::Text(txt) => tokens.append_all(quote! { BodyNode::Text(txt) => tokens.append_all(quote! {
dioxus_core::DynamicNode::Text(dioxus_core::VText::new(#txt)) dioxus_core::DynamicNode::Text(dioxus_core::VText::new(#txt))