Skip to content

Commit

Permalink
Merge pull request #2105 from DioxusLabs/jk/props-expand
Browse files Browse the repository at this point in the history
Fix #1938, allow explicit props
  • Loading branch information
jkelleyrtp authored Mar 18, 2024
2 parents f266213 + 9c64fc4 commit 468d2b6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
85 changes: 44 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {

fn app() -> Element {
rsx! {
spreadable_component {
SpreadableComponent {
width: "10px",
extra_data: "hello{1}",
extra_data2: "hello{2}",
Expand All @@ -34,7 +34,8 @@ struct Props {
extra_data2: String,
}

fn spreadable_component(props: Props) -> Element {
#[component]
fn SpreadableComponent(props: Props) -> Element {
rsx! {
audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
}
Expand Down
25 changes: 25 additions & 0 deletions packages/core-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ impl Parse for ComponentBody {

impl ToTokens for ComponentBody {
fn to_tokens(&self, tokens: &mut TokenStream) {
// https://github.com/DioxusLabs/dioxus/issues/1938
// If there's only one input and the input is `props: Props`, we don't need to generate a props struct
// Just attach the non_snake_case attribute to the function
// eventually we'll dump this metadata into devtooling that lets us find all these components
if self.is_explicit_props_ident() {
let comp_fn = &self.item_fn;
tokens.append_all(quote! {
#[allow(non_snake_case)]
#comp_fn
});
return;
}

let comp_fn = self.comp_fn();

// If there's no props declared, we simply omit the props argument
Expand Down Expand Up @@ -196,6 +209,18 @@ impl ComponentBody {

props_docs
}

fn is_explicit_props_ident(&self) -> bool {
if self.item_fn.sig.inputs.len() == 1 {
if let FnArg::Typed(PatType { pat, .. }) = &self.item_fn.sig.inputs[0] {
if let Pat::Ident(ident) = pat.as_ref() {
return ident.ident == "props";
}
}
}

false
}
}

struct DocField<'a> {
Expand Down
Loading

0 comments on commit 468d2b6

Please sign in to comment.