Skip to content

Commit

Permalink
🐛 fix(TextField): handle case when Type is 'number' and Value is type…
Browse files Browse the repository at this point in the history
… of string (#2264)
  • Loading branch information
capdiem authored Nov 26, 2024
1 parent ac2435a commit 5ae67de
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/Masa.Blazor/Components/TextField/MTextField.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,32 @@ protected override void UpdateInternalValue(TValue? value, InternalValueChangeTy
{
base.UpdateInternalValue(value, changeType);

if (IsNumberType && value is IConvertible convertible)
if (IsNumeric(value, out var numeric))
{
var decimalValue = Convert.ToDecimal(convertible, CultureInfo.InvariantCulture);
var decimalValue = Convert.ToDecimal(numeric, CultureInfo.InvariantCulture);

UpButtonEnabled = !Props.Max.HasValue || decimalValue < Props.Max;
DownButtonEnabled = !Props.Min.HasValue || decimalValue > Props.Min;
}
}

private bool IsNumeric(TValue? value, out IConvertible numeric)
{
numeric = default;
if (IsNumberType && value is not string && value is IConvertible convertible)
{
numeric = convertible;
return true;
}

return false;
}

protected override TValue? ConvertAndSetValueByJSInterop(TValue? val)
{
if (IsNumberType && Props.Precision.HasValue && val is IConvertible convertible)
if (Props.Precision.HasValue && IsNumeric(val, out var numeric))
{
var decimalValue = Convert.ToDecimal(convertible, CultureInfo.InvariantCulture);
var decimalValue = Convert.ToDecimal(numeric, CultureInfo.InvariantCulture);
var newValue = Math.Round(decimalValue, Props.Precision.Value);
_ = SetValueByJsInterop(newValue.ToString(Props.PrecisionFormat, CultureInfo.InvariantCulture));
return (TValue)Convert.ChangeType(newValue, typeof(TValue), CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -611,9 +623,9 @@ private void UpdateValue(string? originValue, bool succeeded, TValue? convertedV

private TValue GetValidValue(TValue val)
{
if (IsNumberType && val is IConvertible convertible)
if (IsNumeric(val, out var numeric))
{
var decimalValue = Convert.ToDecimal(convertible, CultureInfo.InvariantCulture);
var decimalValue = Convert.ToDecimal(numeric, CultureInfo.InvariantCulture);

if (Props.Min.HasValue && decimalValue < Props.Min)
{
Expand Down

0 comments on commit 5ae67de

Please sign in to comment.