Components participate in event handlers

This commit is contained in:
Jonathan Kelley committed 2024-01-11 12:11:27 -08:00
1 parent 8881b8a473
commit 1527b81e02
12 files changed
+86 -54

No files matched your search

+15
View File
@@ -804,6 +804,15 @@ impl<'a, 'b> IntoDynNode<'b> for &'a str {
}
}
impl IntoDynNode<'_> for &String {
fn into_dyn_node(self, cx: &ScopeState) -> DynamicNode {
DynamicNode::Text(VText {
value: cx.bump().alloc_str(&self),
id: Default::default(),
})
}
}
impl IntoDynNode<'_> for String {
fn into_dyn_node(self, cx: &ScopeState) -> DynamicNode {
DynamicNode::Text(VText {
@@ -898,6 +907,12 @@ impl<'a> IntoAttributeValue<'a> for String {
}
}
impl<'a> IntoAttributeValue<'a> for &String {
fn into_value(self, cx: &'a Bump) -> AttributeValue<'a> {
AttributeValue::Text(cx.alloc_str(&self))
}
}
impl<'a> IntoAttributeValue<'a> for f64 {
fn into_value(self, _: &'a Bump) -> AttributeValue<'a> {
AttributeValue::Float(self)
+14 -5
View File
@@ -109,17 +109,25 @@ impl ToTokens for ElementAttrNamed {
};
let attribute = {
let value = &self.attr.value;
let is_shorthand_event = match &attr.value {
ElementAttrValue::Shorthand(s) => s.to_string().starts_with("on"),
_ => false,
};
match &attr.value {
ElementAttrValue::AttrLiteral(_)
| ElementAttrValue::AttrExpr(_)
| ElementAttrValue::Shorthand(_)
| ElementAttrValue::AttrOptionalExpr { .. } => {
| ElementAttrValue::AttrOptionalExpr { .. }
if !is_shorthand_event =>
{
let name = &self.attr.name;
let ns = ns(name);
let volitile = volitile(name);
let attribute = attribute(name);
let value = &self.attr.value;
let value = quote! { #value };
quote! {
__cx.attr(
#attribute,
@@ -131,12 +139,13 @@ impl ToTokens for ElementAttrNamed {
}
ElementAttrValue::EventTokens(tokens) => match &self.attr.name {
ElementAttrName::BuiltIn(name) => {
quote! {
dioxus_elements::events::#name(__cx, #tokens)
}
quote! { dioxus_elements::events::#name(__cx, #tokens) }
}
ElementAttrName::Custom(_) => todo!(),
},
_ => {
quote! { dioxus_elements::events::#value(__cx, #value) }
}
}
};
+3
View File
@@ -241,6 +241,9 @@ impl ContentField {
impl ToTokens for ContentField {
fn to_tokens(&self, tokens: &mut TokenStream2) {
match self {
ContentField::Shorthand(i) if i.to_string().starts_with("on") => {
tokens.append_all(quote! { __cx.event_handler(#i) })
}
ContentField::Shorthand(i) => tokens.append_all(quote! { #i }),
ContentField::ManExpr(e) => e.to_tokens(tokens),
ContentField::Formatted(s) => tokens.append_all(quote! { __cx.raw_text(#s) }),