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

yew-macro: Handling of boolean properties #745

Closed
Follpvosten opened this issue Nov 18, 2019 · 5 comments
Closed

yew-macro: Handling of boolean properties #745

Follpvosten opened this issue Nov 18, 2019 · 5 comments

Comments

@Follpvosten
Copy link

Follpvosten commented Nov 18, 2019

Description

I'm submitting a feature request (I intend to work on this myself if I can find the time).

  • Since bool defaults to false, the property being missing already implies false, which is great. However, I'd really like it to work the other way around too: If a bool property is present, it should imply true (meaning I can write <MyComp myprop /> instead of <MyComp myprop=true />).
@jstarry
Copy link
Member

jstarry commented Nov 28, 2019

These both sound like great features, would gladly review PRs to fix them! Could you please split this issue into two? One for boolean handling and one for Option conversion?

@Follpvosten
Copy link
Author

Alright, I'll do that now.

I actually came across a similar situation to what you described in #756 - I have something like:

let placeholder = if something { "something" } else { "something else" };
html! {
    <input type="text" /* ... */ placeholder=placeholder />
}

Being able to omit the =placeholder if there's a variable with the exact name of the property in scope would be a very nice improvement. I guess I'll create an issue for that as well.

@Follpvosten Follpvosten changed the title yew-macro/Properties derive: Handling of Option<T> and boolean properties yew-macro: Handling of boolean properties Dec 3, 2019
@samuelvanderwaal
Copy link
Contributor

Missing label:

  • good first issue

@CalliEve
Copy link

CalliEve commented Mar 9, 2020

Hi! I am still pretty junior, but I am interested to work on this if it is something that is still wanted? I see that something similar will be done with #769 with the syntax there potentially colliding for the syntax here?

Also after looking at the code I see that the lib doesn't "know" what type a prop is till the to_tokens call in the HtmlComponent struct (I could be wrong in this). While I could set the value to true automatically and then check over there if it is the correct type, I don't know if this will result in a well-understandable error in case the type of the prop isn't a bool.

@jstarry
Copy link
Member

jstarry commented Mar 10, 2020

@Baev1 thanks for taking a look, you're totally right about that. I think we could only have this special handling for boolean html elements:

static ref BOOLEAN_SET: HashSet<&'static str> = {
HashSet::from_iter(
vec![
"async",
"autofocus",
"controls",
"default",
"defer",
"disabled",
"hidden",
"ismap",
"loop",
"multiple",
"muted",
"novalidate",
"open",
"readonly",
"required",
"selected",
]
.into_iter(),
)
};

Even then, it's probably not so useful because it's much easier to enable/disable these types of attributes using a dynamic bool.

I think it's safe to close this, so I'll do so. @Baev1 are you specifically interested in helping with the macro or would you be interested in helping on other issues? We can discuss in the gitter room if you want!

@jstarry jstarry closed this as completed Mar 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants