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

@ -9,7 +9,8 @@
<InputNumber
Label="Double"
Required="true"
Step="0.002"
Step="0.001"
DecimalPlaces="2"
@bind-Value="@inputValueDouble">
</InputNumber>
@ -17,6 +18,7 @@
Label="Integer"
Required="true"
Step="0.002"
@bind-Value="@inputValueInt">
</InputNumber>
@ -38,7 +40,21 @@
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;
int inputValueInt = 12;
@ -50,13 +66,6 @@
StateHasChanged();
}
string errorText
{
get
{
if (string.IsNullOrEmpty(inputValueText)) return string.Empty;
return "Error!!";
}
}
string errorText = "Error!!";
}

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

@ -1,13 +1,25 @@
using Connected.Models;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using System.Numerics;
using static Connected.Colors;
namespace Connected.Components;
public partial class InputNumber<NumberType>:InputBase where NumberType : INumber<NumberType>
{
{
private double _step;
[Parameter]
public double Step { get; set; }
public double Step {
get
{
return _step;
}
set
{
_step = value;
AdjustStep();
}
}
[Parameter]
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.ToString().Length > 0)
NumberType originalValue = (NumberType)Convert.ChangeType(args.Value, typeof(NumberType));
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
{
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()
{
await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType)));
@ -42,14 +92,23 @@ public partial class InputNumber<NumberType>:InputBase where NumberType : INumbe
#region Lifecycle
protected override async Task OnInitializedAsync()
{
if (DecimalPlaces > 0)
{
Value = AdjustDecimalPlaces(Value);
await ValueChanged.InvokeAsync(Value);
}
AdjustStep();
}
protected override async Task OnParametersSetAsync()
{
{
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;
}
await base.OnParametersSetAsync();
}

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

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

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

@ -35,6 +35,12 @@ public static class Helper
{
switch (type.ToLower())
{
case "long":
case "ulong":
case "short":
case "ushort":
case "byte":
case "sbyte":
case "uint16":
case "uint32":
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)
{
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)
{
switch (type.ToLower())
@ -79,7 +139,7 @@ public static class Helper
{
try
{
var number = Double.Parse(input);
var number = Decimal.Parse(input);
return true;
}
catch

Loading…
Cancel
Save