Skip to content

Commit

Permalink
🐛 fix(TextField): support nullable numeric types
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem committed Dec 23, 2024
1 parent 22eb782 commit 7a15956
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Masa.Blazor/Components/TextField/MTextField.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,22 @@ private bool IsNumeric(TValue? value, out IConvertible numeric)
{
var newValue = Math.Round(decimalValue, Props.Precision.Value);
_ = SetValueByJsInterop(newValue.ToString(Props.PrecisionFormat, CultureInfo.InvariantCulture));
return (TValue)Convert.ChangeType(newValue, typeof(TValue), CultureInfo.InvariantCulture);
var isNullable = IsNullable<TValue>();
return (TValue)Convert.ChangeType(
newValue,
isNullable ? Nullable.GetUnderlyingType(typeof(TValue))! : typeof(TValue),
CultureInfo.InvariantCulture);
}
}

return base.ConvertAndSetValueByJSInterop(val);
}

private static bool IsNullable<T>()
{
return Nullable.GetUnderlyingType(typeof(T)) != null || !typeof(T).IsValueType;
}

private static bool TryConvertTo<T>(string? value, out T? result)
{
var succeeded = BindConverter.TryConvertTo<T>(value, CultureInfo.InvariantCulture, out var val);
Expand Down

0 comments on commit 7a15956

Please sign in to comment.