feat: Support generic component with inline_props with some limitation

This commit is contained in:
Muhannad Alrusayni 2022-05-06 11:11:34 +03:00
parent a55b56b403
commit 0b5b7f09db
2 changed files with 18 additions and 4 deletions

View file

@ -17,6 +17,7 @@ pub struct InlinePropsBody {
pub inputs: Punctuated<FnArg, Token![,]>, pub inputs: Punctuated<FnArg, Token![,]>,
// pub fields: FieldsNamed, // pub fields: FieldsNamed,
pub output: ReturnType, pub output: ReturnType,
pub where_clause: Option<WhereClause>,
pub block: Box<Block>, pub block: Box<Block>,
} }
@ -28,7 +29,7 @@ impl Parse for InlinePropsBody {
let fn_token = input.parse()?; let fn_token = input.parse()?;
let ident = input.parse()?; let ident = input.parse()?;
let generics = input.parse()?; let generics: Generics = input.parse()?;
let content; let content;
let paren_token = syn::parenthesized!(content in input); let paren_token = syn::parenthesized!(content in input);
@ -47,6 +48,11 @@ impl Parse for InlinePropsBody {
let output = input.parse()?; let output = input.parse()?;
let where_clause = input
.peek(syn::token::Where)
.then(|| input.parse())
.transpose()?;
let block = input.parse()?; let block = input.parse()?;
Ok(Self { Ok(Self {
@ -57,6 +63,7 @@ impl Parse for InlinePropsBody {
paren_token, paren_token,
inputs, inputs,
output, output,
where_clause,
block, block,
cx_token, cx_token,
attrs, attrs,
@ -73,6 +80,7 @@ impl ToTokens for InlinePropsBody {
generics, generics,
inputs, inputs,
output, output,
where_clause,
block, block,
cx_token, cx_token,
attrs, attrs,
@ -136,12 +144,16 @@ impl ToTokens for InlinePropsBody {
out_tokens.append_all(quote! { out_tokens.append_all(quote! {
#modifiers #modifiers
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#vis struct #struct_name #struct_generics { #vis struct #struct_name #struct_generics
#where_clause
{
#(#fields),* #(#fields),*
} }
#(#attrs)* #(#attrs)*
#vis fn #ident #fn_generics (#cx_token: Scope<#scope_lifetime #struct_name #generics>) #output { #vis fn #ident #fn_generics (#cx_token: Scope<#scope_lifetime #struct_name #generics>) #output
#where_clause
{
let #struct_name { #(#field_names),* } = &cx.props; let #struct_name { #(#field_names),* } = &cx.props;
#block #block
} }

View file

@ -653,7 +653,9 @@ Finally, call `.build()` to create the instance of `{name}`.
} }
} }
impl #impl_generics dioxus::prelude::Properties for #name #ty_generics{ impl #impl_generics dioxus::prelude::Properties for #name #ty_generics
#b_generics_where_extras_predicates
{
type Builder = #builder_name #generics_with_empty; type Builder = #builder_name #generics_with_empty;
const IS_STATIC: bool = #is_static; const IS_STATIC: bool = #is_static;
fn builder() -> Self::Builder { fn builder() -> Self::Builder {