Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Varargs for Seq[ReactElement] children #478

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jedahu
Copy link
Contributor

@jedahu jedahu commented May 16, 2021

The way the @react macro works, if you want an apply method that takes varargs children you have two options:

  1. ReactElement* in the first parameter list.
  2. ReactElement* in another parameter list.

Option 1 means no other parameters can have default values. Option 2 means props values cannot be easily manipulated because no copy method is generated.

A 3rd option: convert Seq[ReactElement] into a varargs parameter would break compatibility with existing code.

This change introduces a boolean expandChildrenSeq parameter to the @react annotation. If true a children: Seq[ReactElement] parameter will be converted into a varargs ReactElement* parameter in the generated apply method, enabling both default parameter values for other props parameters and a generated copy method, and preserving compatibility with existing code.

The way the @react macro works, if you want an apply method that takes
varargs children you have two options:

1. ReactElement* in the first parameter list.
2. ReactElement* in another parameter list.

Option 1 means no other parameters can have default values. Option 2 means
props values cannot be easily manipulated because no copy method is generated.

A 3rd option: convert Seq[ReactElement] into a varargs parameter would
break compatibility with existing code.

This change introduces a boolean `expandChildrenSeq` parameter to the
@react annotation. If `true` a `children: Seq[ReactElement]` parameter
will be converted into a varargs `ReactElement*` parameter in the
generated apply method, enabling both default parameter values for other
props parameters and a generated copy method.
@jedahu
Copy link
Contributor Author

jedahu commented May 16, 2021

scalafmt seems to have reformatted more than my changes. Not sure why.

q"${ps.name}: _*"
} else q"${ps.name}"
}
childrenParam.get.tpt match {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could make this even more general, based on the comments in #237, by having expandChildren make children curried regardless of the parameter type. That would line up with JSX behavior better too. And since expandChildren is false by default, there's no worry of unexpected behavior.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jedahu thinking about things a bit more, I'm fairly convinced myself that removing the type restriction is the way to go; if you think that makes sense could you go ahead and remove it?

@shadaj
Copy link
Owner

shadaj commented May 25, 2021

Looks great overall! Just one API design possibility to ponder.

@shadaj
Copy link
Owner

shadaj commented Jul 26, 2021

@jedahu I think we can merge this PR and then generalize the support later; could you add a quick note to the changelog and docs explaining this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants