feat: variable bindings on components (#1140)

This commit is contained in:
yuuma03 2023-06-03 16:22:44 -03:00 committed by GitHub
parent f6d856ee11
commit 51a6147609
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1410,7 +1410,10 @@ pub(crate) fn slot_to_tokens(
let props = attrs
.clone()
.filter(|attr| !attr.key.to_string().starts_with("clone:"))
.filter(|attr| {
!attr.key.to_string().starts_with("bind:")
&& !attr.key.to_string().starts_with("clone:")
})
.map(|attr| {
let name = &attr.key;
@ -1426,6 +1429,16 @@ pub(crate) fn slot_to_tokens(
}
});
let items_to_bind = attrs
.clone()
.filter_map(|attr| {
attr.key
.to_string()
.strip_prefix("bind:")
.map(|ident| format_ident!("{ident}", span = attr.key.span()))
})
.collect::<Vec<_>>();
let items_to_clone = attrs
.clone()
.filter_map(|attr| {
@ -1461,16 +1474,29 @@ pub(crate) fn slot_to_tokens(
);
if let Some(children) = children {
let bindables =
items_to_bind.iter().map(|ident| quote! { #ident, });
let clonables = items_to_clone
.iter()
.map(|ident| quote! { let #ident = #ident.clone(); });
quote! {
.children({
#(#clonables)*
if bindables.len() > 0 {
quote! {
.children({
#(#clonables)*
Box::new(move |#cx| #children #view_marker)
})
move |#cx, #(#bindables)*| #children #view_marker
})
}
} else {
quote! {
.children({
#(#clonables)*
Box::new(move |#cx| #children #view_marker)
})
}
}
} else {
quote! {}
@ -1526,7 +1552,8 @@ pub(crate) fn component_to_tokens(
let props = attrs
.clone()
.filter(|attr| {
!attr.key.to_string().starts_with("clone:")
!attr.key.to_string().starts_with("bind:")
&& !attr.key.to_string().starts_with("clone:")
&& !attr.key.to_string().starts_with("on:")
})
.map(|attr| {
@ -1544,6 +1571,16 @@ pub(crate) fn component_to_tokens(
}
});
let items_to_bind = attrs
.clone()
.filter_map(|attr| {
attr.key
.to_string()
.strip_prefix("bind:")
.map(|ident| format_ident!("{ident}", span = attr.key.span()))
})
.collect::<Vec<_>>();
let items_to_clone = attrs
.clone()
.filter_map(|attr| {
@ -1590,16 +1627,29 @@ pub(crate) fn component_to_tokens(
);
if let Some(children) = children {
let bindables =
items_to_bind.iter().map(|ident| quote! { #ident, });
let clonables = items_to_clone
.iter()
.map(|ident| quote! { let #ident = #ident.clone(); });
quote! {
.children({
#(#clonables)*
if bindables.len() > 0 {
quote! {
.children({
#(#clonables)*
Box::new(move |#cx| #children #view_marker)
})
move |#cx, #(#bindables)*| #children #view_marker
})
}
} else {
quote! {
.children({
#(#clonables)*
Box::new(move |#cx| #children #view_marker)
})
}
}
} else {
quote! {}