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

feat: implement simple spread attributes #1619

Merged
merged 1 commit into from
Sep 2, 2023

Conversation

mrvillage
Copy link
Contributor

@mrvillage mrvillage commented Aug 31, 2023

As discussed in Discord, here's a simple PR for some basic spread attributes support for items implementing IntoIterator<Item= (&'static str, Attribute)>.

Essentially, you can do the following:

let attrs = ["value", 1.into_attribute()];
view! { <input {..attrs} /> }

and it'll spread the attributes from the iterator into the component as if they were applied normally.

I've tested it and it seems to work correctly, this is my first PR to leptos as well so no idea if I did any of this correctly but please let me know. :)

@gbj
Copy link
Collaborator

gbj commented Sep 1, 2023

Wow this is great, thanks so much. I should've done this ages ago.

This will also allow me to add something to <Component/>s like an attr: syntax and also the spread syntax, and interpret both of those as something like "pass a prop named attributes which is a Vec<&'static str, Attribute> to the component.

So you can have

#[component]
pub fn Input(attrs: Vec<(&'static str, Attribute)>) -> impl IntoView {
  view! {
    <input class="fancy-input" {...attrs}/>
  }
}

view! { 
  <Input attr:type="text" attr:placeholder="Hello!"/>
}

I'll do that in a separate PR of course.

@mrvillage
Copy link
Contributor Author

Thank you for letting me do it! :) And that syntax sounds great, if you'd like I can make a PR for that or something similar, whatever you'd like (it's the exact reason I wanted spread props in the first place so I'm more than happy to implement it :) ).

@gbj gbj merged commit 4a43983 into leptos-rs:main Sep 2, 2023
@gbj
Copy link
Collaborator

gbj commented Sep 2, 2023

If you want to work on it, that would be awesome! You may have time to get to it sooner than I do.

@mrvillage
Copy link
Contributor Author

Awesome! I'll get something worked out. :)

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