|
|
|
@ -7,24 +7,38 @@ using System.Numerics;
|
|
|
|
|
namespace Connected.Components;
|
|
|
|
|
public partial class NumberInput<NumberType> : InputBase where NumberType : INumber<NumberType>
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Step for up and down on numeric field
|
|
|
|
|
/// Options: Any double number
|
|
|
|
|
/// Default: 1
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public double Step { get; set; } = 1;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Mouse wheel disable to prevent StepUp/StepDown on number filed
|
|
|
|
|
/// Options: true, false
|
|
|
|
|
/// Default: false
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool DisableMouseWheel { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Increase 'Value' for the 'Step'
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>'Value' increased for the 'Step' parameter</returns>
|
|
|
|
|
private async Task StepUp()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var num = (double)Convert.ChangeType(Value, typeof(double));
|
|
|
|
|
var num = Helper.ConvertToType<double>(Value);
|
|
|
|
|
|
|
|
|
|
num += Step;
|
|
|
|
|
|
|
|
|
|
if (DecimalPlaces > 0)
|
|
|
|
|
num = Math.Round(num, DecimalPlaces);
|
|
|
|
|
|
|
|
|
|
Value = (NumberType)Convert.ChangeType(num, typeof(NumberType));
|
|
|
|
|
Value = Helper.ConvertToType<NumberType>(num);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (IsError)
|
|
|
|
|
ErrorText = string.Empty;
|
|
|
|
@ -38,18 +52,22 @@ public partial class NumberInput<NumberType> : InputBase where NumberType : INum
|
|
|
|
|
await ValueChanged.InvokeAsync(Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Decrease 'Value' for the 'Step'
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>'Value' decreased for the 'Step' parameter</returns>
|
|
|
|
|
private async Task StepDown()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var num = (double)Convert.ChangeType(Value, typeof(double));
|
|
|
|
|
var num = Helper.ConvertToType<double>(Value);
|
|
|
|
|
|
|
|
|
|
num -= Step;
|
|
|
|
|
|
|
|
|
|
if (DecimalPlaces > 0)
|
|
|
|
|
num = Math.Round(num, DecimalPlaces);
|
|
|
|
|
|
|
|
|
|
Value = (NumberType)Convert.ChangeType(num, typeof(NumberType));
|
|
|
|
|
Value = Helper.ConvertToType<NumberType>(num);
|
|
|
|
|
|
|
|
|
|
if (IsError)
|
|
|
|
|
ErrorText = string.Empty;
|
|
|
|
@ -62,6 +80,12 @@ public partial class NumberInput<NumberType> : InputBase where NumberType : INum
|
|
|
|
|
|
|
|
|
|
await ValueChanged.InvokeAsync(Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event triggered when mouse wheel is activated inside component
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args">WheelEventArgs argument</param>
|
|
|
|
|
/// <returns>Doesnt return values just increasing/decreasing values</returns>
|
|
|
|
|
protected async Task OnMouseWheel(WheelEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (DisableMouseWheel)
|
|
|
|
@ -82,15 +106,30 @@ public partial class NumberInput<NumberType> : InputBase where NumberType : INum
|
|
|
|
|
|
|
|
|
|
private string? _value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Value of any numeric type
|
|
|
|
|
/// Options: any numeric type variable
|
|
|
|
|
/// Default: null
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
[EditorRequired]
|
|
|
|
|
public NumberType? Value
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_value))
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
return (NumberType)Convert.ChangeType(_value, typeof(NumberType));
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Helper.ConvertToType<NumberType>(_value);
|
|
|
|
|
} catch
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
@ -98,13 +137,22 @@ public partial class NumberInput<NumberType> : InputBase where NumberType : INum
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Number of decimal places for Value. If set, Value is corrected when input looses focus
|
|
|
|
|
/// Options: any integer number greater or equal 0
|
|
|
|
|
/// Default: 0
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public int DecimalPlaces { get; set; } = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Value change event
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<NumberType> ValueChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
public async Task GetValueAsync(ChangeEventArgs args)
|
|
|
|
|
public async Task SetValueAsync(ChangeEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Value is not null)
|
|
|
|
|
{
|
|
|
|
@ -123,18 +171,21 @@ public partial class NumberInput<NumberType> : InputBase where NumberType : INum
|
|
|
|
|
await ValueChanged.InvokeAsync(default);
|
|
|
|
|
|
|
|
|
|
if (!newVal.Equals(_value))
|
|
|
|
|
await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(newVal, typeof(NumberType)));
|
|
|
|
|
await ValueChanged.InvokeAsync(Helper.ConvertToType<NumberType>(newVal));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Change(ChangeEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Value is not null)
|
|
|
|
|
Value = AdjustDecimalPlaces((NumberType)Convert.ChangeType(args.Value, typeof(NumberType)));
|
|
|
|
|
Value = AdjustDecimalPlaces(Helper.ConvertToType<NumberType>(args.Value));
|
|
|
|
|
|
|
|
|
|
await ValueChanged.InvokeAsync(Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// On keyboard key press event
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<KeyboardEventArgs> OnKeyDown { get; set; }
|
|
|
|
|
|
|
|
|
@ -186,33 +237,30 @@ public partial class NumberInput<NumberType> : InputBase where NumberType : INum
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NumberType AdjustDecimalPlaces(NumberType value)
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Method for adjusting decimal places provided with parameter
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">Value whose decimal places we want to change</param>
|
|
|
|
|
/// <returns>NumberType result with adjusted decimal places</returns>
|
|
|
|
|
private NumberType? AdjustDecimalPlaces(NumberType? value)
|
|
|
|
|
{
|
|
|
|
|
var result = value;
|
|
|
|
|
if (DecimalPlaces > 0)
|
|
|
|
|
{
|
|
|
|
|
double converted = Math.Round((double)Convert.ChangeType(result, typeof(double)), DecimalPlaces);
|
|
|
|
|
return (NumberType)Convert.ChangeType(converted, typeof(NumberType));
|
|
|
|
|
double converted = Math.Round(Helper.ConvertToType<double>(result), DecimalPlaces);
|
|
|
|
|
return Helper.ConvertToType<NumberType>(converted);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clear event for user clear icon click. It clears the Value and set it to
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task Clear()
|
|
|
|
|
{
|
|
|
|
|
_value = string.Empty;
|
|
|
|
|
await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
if (firstRender)
|
|
|
|
|
{
|
|
|
|
|
if (!DecimalPlaces.Equals(Helper.GetDecimalPlaces(Value)))
|
|
|
|
|
{
|
|
|
|
|
Value = AdjustDecimalPlaces(Value);
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var val = Helper.ConvertToType<NumberType>(null);
|
|
|
|
|
await ValueChanged.InvokeAsync(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Lifecycle
|
|
|
|
@ -228,12 +276,18 @@ public partial class NumberInput<NumberType> : InputBase where NumberType : INum
|
|
|
|
|
await base.OnParametersSetAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
protected override void OnAfterRender(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
await base.OnInitializedAsync();
|
|
|
|
|
if (Required)
|
|
|
|
|
if (firstRender)
|
|
|
|
|
{
|
|
|
|
|
if (!InputAttributes.ContainsKey("required")) InputAttributes.Add("required", true);
|
|
|
|
|
if (Value is not null)
|
|
|
|
|
{
|
|
|
|
|
if (!DecimalPlaces.Equals(Helper.GetDecimalPlaces(Value)))
|
|
|
|
|
{
|
|
|
|
|
Value = AdjustDecimalPlaces(Value);
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|