From edb2249a43cc2b2429a1d629ad97023d6db1f8c2 Mon Sep 17 00:00:00 2001 From: stm Date: Tue, 7 Feb 2023 10:49:18 +0100 Subject: [PATCH] Progress --- .../Pages/Index.razor | 29 ++++--- .../Components/InputNumber.razor | 2 +- .../Components/InputNumber.razor.cs | 79 ++++++++++++++++--- .../Components/InputText.razor.cs | 6 +- src/Connected.Components/Models/InputBase.cs | 26 +++++- src/Connected.Components/Navigation.cs | 8 +- src/Connected.Components/Utilities/Helper.cs | 62 ++++++++++++++- 7 files changed, 180 insertions(+), 32 deletions(-) diff --git a/src/Connected.Components.Showcase.Runner/Pages/Index.razor b/src/Connected.Components.Showcase.Runner/Pages/Index.razor index 18d6d98..b6cc731 100644 --- a/src/Connected.Components.Showcase.Runner/Pages/Index.razor +++ b/src/Connected.Components.Showcase.Runner/Pages/Index.razor @@ -9,7 +9,8 @@ @@ -17,6 +18,7 @@ Label="Integer" Required="true" Step="0.002" + @bind-Value="@inputValueInt"> @@ -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!!"; } \ No newline at end of file diff --git a/src/Connected.Components/Components/InputNumber.razor b/src/Connected.Components/Components/InputNumber.razor index 03a27c4..ef5be8a 100644 --- a/src/Connected.Components/Components/InputNumber.razor +++ b/src/Connected.Components/Components/InputNumber.razor @@ -8,7 +8,7 @@ :InputBase where NumberType : INumber -{ - +{ + 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: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: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(); } diff --git a/src/Connected.Components/Components/InputText.razor.cs b/src/Connected.Components/Components/InputText.razor.cs index 08ef495..8f4871f 100644 --- a/src/Connected.Components/Components/InputText.razor.cs +++ b/src/Connected.Components/Components/InputText.razor.cs @@ -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(); } diff --git a/src/Connected.Components/Models/InputBase.cs b/src/Connected.Components/Models/InputBase.cs index cea3e5f..3bcd5fc 100644 --- a/src/Connected.Components/Models/InputBase.cs +++ b/src/Connected.Components/Models/InputBase.cs @@ -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 { diff --git a/src/Connected.Components/Navigation.cs b/src/Connected.Components/Navigation.cs index 2c5ec49..2ee15c0 100644 --- a/src/Connected.Components/Navigation.cs +++ b/src/Connected.Components/Navigation.cs @@ -88,25 +88,25 @@ internal class Navigation /// /// Navigates to the previous url if possible or does nothing if it is not. /// - public void Back() + public async Task Back() { if (!CanNavigateBack()) return; var backPageUrl = GetFromBackPageList(); UpdateForwardPageList(); - NavigateTo(backPageUrl); + await NavigateTo(backPageUrl); } /// /// Navigates to the forward url if possible or does nothing if it is not. /// - public void Forward() + public async Task Forward() { if (!CanNavigateForward()) return; var forwardPageUrl = GetFromForwardPageList(); UpdateBackPageList(); - NavigateTo(forwardPageUrl); + await NavigateTo(forwardPageUrl); } } diff --git a/src/Connected.Components/Utilities/Helper.cs b/src/Connected.Components/Utilities/Helper.cs index 17d3193..5737498 100644 --- a/src/Connected.Components/Utilities/Helper.cs +++ b/src/Connected.Components/Utilities/Helper.cs @@ -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