features/new
stm 2 years ago
parent 457df20346
commit edb2249a43

@ -9,7 +9,8 @@
<InputNumber <InputNumber
Label="Double" Label="Double"
Required="true" Required="true"
Step="0.002" Step="0.001"
DecimalPlaces="2"
@bind-Value="@inputValueDouble"> @bind-Value="@inputValueDouble">
</InputNumber> </InputNumber>
@ -17,6 +18,7 @@
Label="Integer" Label="Integer"
Required="true" Required="true"
Step="0.002" Step="0.002"
@bind-Value="@inputValueInt"> @bind-Value="@inputValueInt">
</InputNumber> </InputNumber>
@ -38,7 +40,21 @@
int counter { get; set; } = 0; int counter { get; set; } = 0;
string inputValueText = "Sample text"; private string _inputText = string.Empty;
string inputValueText
{
get
{
return _inputText;
}
set
{
if (value.Length > 0) errorText = "Error!!!";
else errorText = "";
_inputText = value;
StateHasChanged();
}
}
double inputValueDouble = 12.756; double inputValueDouble = 12.756;
int inputValueInt = 12; int inputValueInt = 12;
@ -50,13 +66,6 @@
StateHasChanged(); StateHasChanged();
} }
string errorText string errorText = "Error!!";
{
get
{
if (string.IsNullOrEmpty(inputValueText)) return string.Empty;
return "Error!!";
}
}
} }

@ -8,7 +8,7 @@
<input type="number" <input type="number"
value="@Value" value="@Value"
placeholder="@Placeholder" placeholder="@Placeholder"
step="@Step" step="@_step"
disabled="@Disabled" disabled="@Disabled"
readonly="@Readonly" readonly="@Readonly"
@oninput=@ChangeValueAsync @oninput=@ChangeValueAsync

@ -1,13 +1,25 @@
using Connected.Models; using Connected.Models;
using Connected.Utilities;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using System.Numerics; using System.Numerics;
using static Connected.Colors;
namespace Connected.Components; namespace Connected.Components;
public partial class InputNumber<NumberType>:InputBase where NumberType : INumber<NumberType> public partial class InputNumber<NumberType>:InputBase where NumberType : INumber<NumberType>
{ {
private double _step;
[Parameter] [Parameter]
public double Step { get; set; } public double Step {
get
{
return _step;
}
set
{
_step = value;
AdjustStep();
}
}
[Parameter] [Parameter]
public NumberType Value { get; set; } public NumberType Value { get; set; }
@ -23,18 +35,56 @@ public partial class InputNumber<NumberType>:InputBase where NumberType : INumbe
{ {
if (args.Value is not null) if (args.Value is not null)
{ {
NumberType originalValue = (NumberType)Convert.ChangeType(args.Value, typeof(NumberType));
if (args.Value.ToString().Length > 0) NumberType value = originalValue;
if (value.ToString().Length > 0)
{ {
await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(args.Value, typeof(NumberType)));
value = AdjustDecimalPlaces(value);
//await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(value, typeof(NumberType)));
} }
else else
{ {
await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType))); value = (NumberType)Convert.ChangeType(0, typeof(NumberType));
//await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType)));
} }
Value = value;
await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(Value, typeof(NumberType)));
} }
} }
private void AdjustStep()
{
if (DecimalPlaces > 0)
{
var StepDecmalPlaces = Helper.GetDecimalPlaces(_step);
if (StepDecmalPlaces > DecimalPlaces)
{
double MinStep = 1;
for (int i = 0; i < DecimalPlaces; i++)
{
MinStep = MinStep / 10;
}
_step = MinStep;
}
}
}
private NumberType AdjustDecimalPlaces(NumberType value)
{
NumberType result = value;
if (Helper.NumberHasDecimalPlaces(value))
{
if (DecimalPlaces > 0)
{
decimal rounded = (decimal)Convert.ChangeType(value, typeof(decimal));
rounded = Math.Round(rounded, DecimalPlaces);
result = (NumberType)Convert.ChangeType(rounded, typeof(NumberType));
}
}
return result;
}
private async Task Clear() private async Task Clear()
{ {
await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType))); await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType)));
@ -42,14 +92,23 @@ public partial class InputNumber<NumberType>:InputBase where NumberType : INumbe
#region Lifecycle #region Lifecycle
protected override async Task OnInitializedAsync()
{
if (DecimalPlaces > 0)
{
Value = AdjustDecimalPlaces(Value);
await ValueChanged.InvokeAsync(Value);
}
AdjustStep();
}
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
if (typeof(NumberType).Name.ToLower().Contains("int")) if (typeof(NumberType).Name.ToLower().Contains("int"))
{ {
if (Step-(int)Step>0) Step= (int)Step; if (Step - (int)Step > 0) Step = (int)Step;
if (Step < 1) Step = 1; if (Step < 1) Step = 1;
} }
await base.OnParametersSetAsync(); await base.OnParametersSetAsync();
} }

@ -35,16 +35,14 @@ public partial class InputText: InputBase
private async Task ChangeValueAsync(ChangeEventArgs args) private async Task ChangeValueAsync(ChangeEventArgs args)
{ {
if (string.IsNullOrEmpty(args.Value.ToString())) ErrorText = string.Empty;
await ValueChanged.InvokeAsync(args.Value.ToString()); await ValueChanged.InvokeAsync(args.Value.ToString());
StateHasChanged(); StateHasChanged();
} }
private async Task Clear() private async Task Clear()
{ {
ChangeEventArgs arg = new ChangeEventArgs(); await ValueChanged.InvokeAsync(string.Empty);
arg.Value = string.Empty; StateHasChanged();
await ChangeValueAsync(arg);
} }

@ -50,8 +50,18 @@ public class InputBase : ComponentBase
[Parameter] [Parameter]
public bool ShowCharacterCounter { get; set; } public bool ShowCharacterCounter { get; set; }
private string _errorText = string.Empty;
[Parameter] [Parameter]
public string ErrorText { get; set; } = string.Empty; public string ErrorText {
get
{
return _errorText;
}
set
{
_errorText = value;
}
}
public bool IsError public bool IsError
{ {
@ -61,8 +71,20 @@ public class InputBase : ComponentBase
} }
} }
private string _helperText = string.Empty;
[Parameter] [Parameter]
public string HelperText { get; set; } = string.Empty; public string HelperText
{
get
{
return _helperText;
}
set
{
_helperText = value;
}
}
public bool IsHelperText public bool IsHelperText
{ {

@ -88,25 +88,25 @@ internal class Navigation
/// <summary> /// <summary>
/// Navigates to the previous url if possible or does nothing if it is not. /// Navigates to the previous url if possible or does nothing if it is not.
/// </summary> /// </summary>
public void Back() public async Task Back()
{ {
if (!CanNavigateBack()) return; if (!CanNavigateBack()) return;
var backPageUrl = GetFromBackPageList(); var backPageUrl = GetFromBackPageList();
UpdateForwardPageList(); UpdateForwardPageList();
NavigateTo(backPageUrl); await NavigateTo(backPageUrl);
} }
/// <summary> /// <summary>
/// Navigates to the forward url if possible or does nothing if it is not. /// Navigates to the forward url if possible or does nothing if it is not.
/// </summary> /// </summary>
public void Forward() public async Task Forward()
{ {
if (!CanNavigateForward()) return; if (!CanNavigateForward()) return;
var forwardPageUrl = GetFromForwardPageList(); var forwardPageUrl = GetFromForwardPageList();
UpdateBackPageList(); UpdateBackPageList();
NavigateTo(forwardPageUrl); await NavigateTo(forwardPageUrl);
} }
} }

@ -35,6 +35,12 @@ public static class Helper
{ {
switch (type.ToLower()) switch (type.ToLower())
{ {
case "long":
case "ulong":
case "short":
case "ushort":
case "byte":
case "sbyte":
case "uint16": case "uint16":
case "uint32": case "uint32":
case "uint64": case "uint64":
@ -51,6 +57,28 @@ public static class Helper
} }
} }
public static int GetDecimalPlaces(object number)
{
try
{
decimal n = (decimal)Convert.ChangeType(number, TypeCode.Decimal);
n = Math.Abs(n); //make sure it is positive.
n -= (int)n; //remove the integer part of the number.
var decimalPlaces = 0;
while (n > 0)
{
decimalPlaces++;
n *= 10;
n -= (int)n;
}
return decimalPlaces;
}
catch
{
return 0;
}
}
public static bool IsTypeDate(string type) public static bool IsTypeDate(string type)
{ {
switch (type.ToLower()) switch (type.ToLower())
@ -64,6 +92,38 @@ public static class Helper
} }
} }
public static bool NumberHasDecimalPlaces(object number)
{
if (number == null) return false;
string type = number.GetType().Name.ToLower();
if (IsTypeNumeric(type))
{
if (!type.Contains("double") && !type.Contains("decimal") && !type.Contains("float")) return false;
else
{
if (type.Equals("double"))
{
var doubleVal = (double)Convert.ChangeType(number, TypeCode.Double);
if (doubleVal % 1 == 0) return false;
return true;
}
if (type.Equals("decimal"))
{
var doubleVal = (decimal)Convert.ChangeType(number, TypeCode.Decimal);
if (doubleVal % 1 == 0) return false;
return true;
}
if (type.Equals("float"))
{
var doubleVal = (float)number;
if (doubleVal % 1 == 0) return false;
return true;
}
}
}
return false;
}
public static bool IsTypeString(string type) public static bool IsTypeString(string type)
{ {
switch (type.ToLower()) switch (type.ToLower())
@ -79,7 +139,7 @@ public static class Helper
{ {
try try
{ {
var number = Double.Parse(input); var number = Decimal.Parse(input);
return true; return true;
} }
catch catch

Loading…
Cancel
Save