I'm creating a Blazor component similar to DataTables and also I want to implement generic filters. For that, I have created 2 components for numbers: one integer number and another one for floating number.
From the main component, I want to pass type of number.
<NumberInput TNumber="short"
Value="@FilterRule.FilterValue"
OnValueChangedEvent="@FilterRule.UpdateFilterValue"
IncludeApply="IncludeApply()"
IsApplied="@FilterRule.IsApplied"
AppliedText="@FilterRule.FilterValue!.ToString()"
ApplyFilterEvent="(e) => ApplyFilter(FilterRule.Guid, true)"
UnApplyFilterEvent="(e) => UnApplyFilter(FilterRule.Guid, true)"
Attributes="Attributes"
ApplyButtonCssClass="@ApplyButtonCssClass"
InputCssClass="@InputCssClass"
MaxWidth="MaxWidth" />
For example, TNumber can have short, int, long and so on. This is because I have a function that gives me the minimum and maximum values for each type of number.
public static Tuple<T, T> GetMinMaxValue<T>()
{
object obj1 = (object)default(T);
object obj2 = (object)default(T);
if (obj1 == null || obj2 == null)
return (Tuple<T, T>)null;
switch (Type.GetTypeCode(typeof(T)))
{
case TypeCode.Int16:
obj1 = (object)char.MinValue;
obj2 = (object)char.MaxValue;
break;
case TypeCode.SByte:
obj1 = (object)sbyte.MinValue;
obj2 = (object)sbyte.MaxValue;
break;
// and so on...
}
}
The NumberInput has this code:
<input class="@DefaultClass form-control-sm @InputCssClass" type="number" value="@Value"
style="@(MaxWidth != 0 ? string.Format("max-width: {0}px", (object) this.MaxWidth) : "")"
@onchange="@UpdateValue" step="any">
@code {
[Parameter] public long Value { get; set; }
[Parameter] public EventCallback<ValueChangedEventArgs> OnValueChangedEvent { get; set; }
[Parameter] public string DefaultClass { get; set; } = "form-control";
[Parameter] public bool IncludeApply { get; set; } = false;
[Parameter] public bool IsApplied { get; set; } = false;
[Parameter] public string AppliedText { get; set; } = "";
[Parameter] public string InputCssClass { get; set; } = "";
[Parameter] public string ApplyButtonCssClass { get; set; } = "";
[Parameter] public Dictionary<string, object> Attributes { get; set; }
[Parameter] public int MaxWidth { get; set; }
[Parameter] public EventCallback<MouseEventArgs> ApplyFilterEvent { get; set; }
[Parameter] public EventCallback<MouseEventArgs> UnApplyFilterEvent { get; set; }
private async Task UpdateValue(ChangeEventArgs args)
{
Value = Convert.ToInt32(args.Value.ToString());
await this.OnValueChangedEvent.InvokeAsync(new ValueChangedEventArgs((object)this.Value));
}
}
So, the question is: how can I pass as parameter the type of variable I want to use?
@typeparam TNumber Valueat the top of your razor file - Brian Parker[Parameter]values inside the component (like you are forValue) - please see github.com/dotnet/aspnetcore/issues/… for the reason - Mister Magoo