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: Allow omitting redundant property names #769
Comments
This conflicts with attributes that don't need a value like hidden. I don't know how yew currently handles those, but personally I don't like the syntax you propose for implicitly using a variable as the value for some HTML attribute, because of this conflict. What about instead of
this could be expressed as
in the style of Python 3.8's |
I'm pretty junior but would be willing to take a stab at this if someone is available to review it and point me in the right direction. Looks like the HtmlProp parsing is defined here: https://github.com/yewstack/yew/blob/master/crates/macro/src/html_tree/html_prop.rs#L23. Is this going to be the right place to implement this feature? |
@samuelvanderwaal yup, that's the place! |
Ok, I have taken a look at this and have some ideas. The most straight-forward approach seems to be to copy the input ParseStream and turn the parsed Expr from a result into an Option, something like this: impl Parse for HtmlProp {
fn parse(input: ParseStream) -> ParseResult<Self> {
let fork_input = input.clone();
let label = input.parse::<HtmlPropLabel>()?;
input.parse::<Token![=]>()?;
let value_opt = input.parse::<Expr>().ok();
let value: Expr = match value_opt {
Some(expr) => expr,
None => fork_input.parse::<Expr>()?,
};
// backwards compat
let _ = input.parse::<Token![,]>();
Ok(HtmlProp { label, value })
}
} I am not sure if using Let me know if I'm going in the right direction here or if I need to rethink my approach. |
I think we have everything we need from cloning Path(
ExprPath {
attrs: [],
qself: None,
path: Path {
leading_colon: None,
segments: [
PathSegment {
ident: <IDENT HERE>,
arguments: None,
},
],
},
},
) |
This is great, thanks! I should be able to work on it tomorrow after the day job, or this weekend. |
One could also use |
Great suggestion, wasn't aware of that macro! |
Using // html_dashed_name.rs
. . .
#[derive(Clone, PartialEq)]
pub struct HtmlDashedName {
pub name: Ident,
pub extended: Vec<(Token![-], Ident)>,
}
. . .
impl Into<Expr> for HtmlDashedName {
fn into(self) -> Expr {
parse_quote!(self.name)
}
} // html_prop.rs
. . .
impl Parse for HtmlProp {
fn parse(input: ParseStream) -> ParseResult<Self> {
let label = input.parse::<HtmlPropLabel>()?;
input.parse::<Token![=]>()?;
let value: Expr = match input.parse::<Expr>().ok() {
Some(expr) => expr,
None => label.clone().into(),
};
// backwards compat
let _ = input.parse::<Token![,]>();
Ok(HtmlProp { label, value })
}
}
. . . This compiles but I haven't set up any specific tests for it yet. |
That doesn't do what you want. It has to be let name = & self.name;
parse_quote!(#name) Otherwise you're converting every HtmlDashedName to literally the expression |
But you're right about the conversion being infallible, so it makes sense to impl |
Ah, after reading more about the |
@jstarry No rush, but when you get a chance, let me know if you like this solution and how you'd like me to approach testing it before putting in a PR. |
@samuelvanderwaal I prefer to iterate on PRs and I think this is ready to start work! I'm admittedly a bit unsure of the syntax still. Maybe we could add a feature flag for it for now? We could pass the feature through yew to yew-macro. Something like And I would want this syntax sugar to work for both "tags" and "components". |
@jstarry I'm open to a feature flag; right now, this doesn't apply to me as much as it used to, mostly because I've started to like using semantically sensible names for my callbacks (like |
@jstarry im happy to try this out by exchanging the crates.io dependency by a git dependency and/or add a cargo feature. |
Cool, thanks. Let's go with a feature then for now |
I like this! Would be very nice to have. Imo it makes most sense to follow struct shorthand: <input /* ... */ placeholder maxlength /> I'm not really a fan of implicit values like |
This was fixed by #1970 🎉 |
Description
I'm submitting a feature request:
Going along with #745, this would be a very nice usability improvement (as also mentioned in #756).
For example, this:
Could be written like this:
This is getting rid of a lot of redundance in the long run by allowing the developer to omit the
=property
when a variable with the property's name is in scope.The text was updated successfully, but these errors were encountered: