Blazor InputText: conditionally rendering an attribute
As it turns out, Blazor will not render the attribute if the value of the attribute is false
or null
https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#conditional-html-element-attributes
HTML element attributes are conditionally rendered based on the .NET value. If the value is false or null, the attribute isn't rendered. If the value is true, the attribute is rendered minimized.
<InputText @bind-Value="@TextProperty" autofocus="@MyModel.isAutoFocus" />
This could potentially be achieved with the @attributes tag. See more here
Basically, you can add an @attributes tag to your InputText. This can bind to a parameter, or to a handy little method.
<InputText @bind-Value="@TextProperty" @attributes="HandyFunction()" />
@code{
Dictionary<string,object> HandyFunction()
{
var dict = new Dictionary<string, object>();
if(MyModel.isAutoFocus) dict.Add("autofocus",true);
return dict;
}
}