feat: allow mut in component props and suppress "needless lifetime" warning (closes #1458) (#1459)

This commit is contained in:
Greg Johnston 2023-07-29 06:32:06 -04:00 committed by GitHub
parent cc32a3e863
commit d44b90c16d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View file

@ -28,7 +28,7 @@ pub fn TestComponent(
/// # use example::TestComponent;
/// <TestComponent key="hello"/>
#[prop(optional)]
another:usize,
another: usize,
/// rust unclosed
/// ```view
/// use example::TestComponent;
@ -38,3 +38,22 @@ pub fn TestComponent(
_ = (key, another, and_another);
}
#[component]
fn TestMutCallback<'a, F>(
cx: Scope,
mut callback: F,
value: &'a str,
) -> impl IntoView
where
F: FnMut(u32) + 'static,
{
let value = value.to_owned();
view! { cx,
<button on:click=move |_| {
callback(5);
}>
{value}
</button>
<TestComponent key="test"/>
}
}

View file

@ -284,6 +284,8 @@ impl ToTokens for Model {
) #ret #(+ #lifetimes)*
#where_clause
{
// allowed for lifetimes that are needed for props struct
#[allow(clippy::needless_lifetimes)]
#body
#destructure_props
@ -589,12 +591,14 @@ fn prop_builder_fields(vis: &Visibility, props: &[Prop]) -> TokenStream {
quote!()
};
let PatIdent { ident, by_ref, .. } = &name;
quote! {
#docs
#builder_docs
#builder_attrs
#allow_missing_docs
#vis #name: #ty,
#vis #by_ref #ident: #ty,
}
})
.collect()
@ -604,7 +608,12 @@ fn prop_names(props: &[Prop]) -> TokenStream {
props
.iter()
.filter(|Prop { ty, .. }| !is_valid_scope_type(ty))
.map(|Prop { name, .. }| quote! { #name, })
.map(|Prop { name, .. }| {
// fields like mutability are removed because unneeded
// in the contexts in which this is used
let ident = &name.ident;
quote! { #ident, }
})
.collect()
}