-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Bug - Conditional Attributes #16333
Comments
This works:
|
Ok and what if I want to then reenable it through code? The value of the disabled attribute is ignored by browsers. So the mere presence of the attribute causes it to be disabled. |
The generated code for @pmoorelegistek example looks crazy: if (false) { } builder.OpenElement(18, "button");
builder.AddAttribute(19, "disabled", "");
builder.AddContent(20, "\n I shouldn\'t be disabled\n");
builder.CloseElement(); Simple booleans also don't work: cshtml: <input type="checkbox" checked="@(1==0)" /> generated code: builder.OpenElement(27, "input");
builder.AddAttribute(28, "type", "checkbox");
builder.AddAttribute(29, "checked", 1==0); rendered: <input type="checkbox" checked="False"> It looks like the problem for this is here: |
We still need to add support for conditional attributes. Normally the syntax in Razor would be |
Ah ok. Yes normally that would be the syntax and in fact it appears that would work with Blazor now with most attributes. The problem is a little unique to |
Can anyone tell me if this is normal? When I type |
@miroslavp the only way I was able to get conditional classing working was the following:
where I would then compose the value for That's consistent with your second example. Assume you need the first set of parentheses at minimum, e.g., Since you're using the ? and : operators, usually in my experience you need to wrap that whole expression in parentheses as well. For example: So that makes sense you'd need the 2 sets of parentheses as in your second example. |
@miroslavp the double parenthesis is to be expected for the conditional class syntax. Check out this site, it has some great documentation: https://learn-blazor.com/pages/data-binding/ |
It's not implemented yet, but my expectation was that we'd have the following special cases:
This would make it easy to do things like:
Can anyone think of any drawbacks to this design? |
Looks great. Would it be possible to extend this a little bit and support something like Angular's class bindings? |
It could be like a C# dictionary I presume? maybe |
Exactly. We need to cover the use case if we have both a conditional and non-conditional class we need to apply. For example we have the element: ...to which we wish to apply the class "active" when the boolean isActive is true. |
All good ideas. You could also have a special |
I'm going to start on this next with the following additional clarifications. We can certainly do more, but I think we don't have a reason NOT to do the following.
The last point might require some work in BrowserRenderer. I'm ok with leaving it out if there's a good reason to not do it, but it would then be fully consistent with Text-mode Razor.
Text-mode Razor does the same. You can use an explicit string if you really want to see the value. I also want to see if we can do something to get rid of the ternaries, or maybe build something specialized around class, but I don't think that's important for the first cut. |
Title
Attributes wrapped in a
@
expression always get applied.Functional impact
Cannot make attributes - such as
disabled
- conditional on a C# expression/property.Minimal repro steps
Try the following CSHTML:
Expected result
Button should not be disabled because the conditional evaluates to
false
.Actual result
The button is actually disabled.
Further technical details
There is also no obvious way to add or remove the
disabled
attribute through C# code without going intointernal
s.Notably, specifying CSS properties with
@
expressions inside a <style> block works great.The text was updated successfully, but these errors were encountered: