From e416c42952338c5dee14935f3671cbfe9b5ca910 Mon Sep 17 00:00:00 2001 From: stm Date: Wed, 15 Feb 2023 08:12:42 +0100 Subject: [PATCH] NumberInput - added onchange method to run Math.Round on the Value when leaving field, adjusting the decimal places provided with DecimalPlaces parameter --- .../Components/NumberInput.razor | 3 ++- .../Components/NumberInput.razor.cs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Connected.Components/Components/NumberInput.razor b/src/Connected.Components/Components/NumberInput.razor index 70f581a..aebe254 100644 --- a/src/Connected.Components/Components/NumberInput.razor +++ b/src/Connected.Components/Components/NumberInput.razor @@ -10,12 +10,13 @@ step="@_step" disabled="@Disabled" readonly="@Readonly" + value="@_value" @onkeydown=@(args => ChangeValue(args)) @onkeydown:preventDefault="@_preventDefaultAction" @oninput=@GetValueAsync @onmousewheel=@OnMouseWheel + @onchange="@Change" @onwheel="OnMouseWheel" - @bind-value="@_value" @attributes="@InputAttributes" /> diff --git a/src/Connected.Components/Components/NumberInput.razor.cs b/src/Connected.Components/Components/NumberInput.razor.cs index 3615c27..01a81c8 100644 --- a/src/Connected.Components/Components/NumberInput.razor.cs +++ b/src/Connected.Components/Components/NumberInput.razor.cs @@ -121,16 +121,25 @@ public partial class NumberInput:InputBase where NumberType : INumbe } if (string.IsNullOrEmpty(newVal)) { - await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(AdjustDecimalPlaces((NumberType)Convert.ChangeType(null, typeof(NumberType))), typeof(NumberType))); + await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType((NumberType)Convert.ChangeType(null, typeof(NumberType)), typeof(NumberType))); } else { if (!newVal.Equals(_value)) - await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(AdjustDecimalPlaces((NumberType)Convert.ChangeType(newVal, typeof(NumberType))), typeof(NumberType))); + await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType((NumberType)Convert.ChangeType(newVal, typeof(NumberType)), typeof(NumberType))); } } } + public async Task Change(ChangeEventArgs args) + { + if (args.Value is not null) + { + Value = AdjustDecimalPlaces((NumberType)Convert.ChangeType(args.Value, typeof(NumberType))); + } + ValueChanged.InvokeAsync(Value); + } + [Parameter] public EventCallback OnKeyDown { get; set; } private bool CheckKey(string key)