I am trying to generate my own component with checkbox system to know if I need the attribute or not (of type int / float etc)
<input type="checkbox" @bind="isMinInt" />
@if (isMinInt == true) {
<input type="number" @bind="MinInt"/>
}
So I would like to replace this @if:
@if(isMinInt == true) {
<MyComponent @bind-Value="ValueInt" Min="@MinInt"/>
} else {
<MyComponent @bind-Value="ValueInt"/>
}
by something like
<MyComponent @bind-Value="ValueInt"
@if(isMinInt == true ? Min="@MinInt" : String.Empty />
because I will have many attributes on my component and I would like to make it simplier
EDIT + Solution
Now using the @attributes:
<input type="checkbox" @bind="isMinInt" />
@if (isMinInt == true) {
<input type="number" @bind="MinInt" />
}
<MyComponent @bind-Value="ValueInt" @attributes="GetAttributesInt()" />
@code {
private bool isMinInt = false;
private int MinInt;
private IDictionary<string, object> GetAttributesInt() {
var dict = new Dictionary<string, object>() { };
if (isMinInt)
dict.Add("Min", MinInt);
return dict;
}
}
EDIT + Solution 2
Now using the @attributes:
<input type="checkbox" @bind="isMinInt" />
@if (isMinInt == true) {
<input type="number" @bind="MinInt" />
}
<MyComponent @bind-Value="ValueInt" @attributes="GetAttributesInt()" />
@code {
private bool isMinInt = false;
private int MinInt;
private IDictionary<string, object> GetAttributesInt() {
var dict = new Dictionary<string, object>() { };
dict["Min"] = this.isMinInt ? MinInt : Int32.MinValue;
return dict;
}
}
The reason why I'm using Int32.MinValue it's because MyComponent correspond to an <input type="number"> where his min is bind to my MinInt, so if I use 0 in the place of Int32.MinValue it won't allow me to go for negative numbers.