Skip to content

Commit

Permalink
[NumberField] Add sbyte type (#2308)
Browse files Browse the repository at this point in the history
* Add sbyte to number filed

* Fix error message

* Remove empy line

* Fix converter

---------

Co-authored-by: Vincent Baaij <[email protected]>
  • Loading branch information
franklupo and vnbaaij authored Jul 2, 2024
1 parent 292c7f4 commit 80bd058
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/Core/Components/Base/InputHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Microsoft.FluentUI.AspNetCore.Components;

internal static class InputHelpers<TValue>
{

public static string GetMaxValue()
{
Type? targetType = Nullable.GetUnderlyingType(typeof(TValue)) ?? typeof(TValue);
Expand Down Expand Up @@ -51,17 +50,26 @@ public static string GetMinValue()
return value;
}

internal static void ValidateIntegerInputs(string? max, string? min)
internal static void ValidateSByteInputs(string? max, string? min)
{
var maxValue = Convert.ToSByte(max);
var minValue = Convert.ToSByte(min);

if (maxValue < minValue)
{
throw new ArgumentException("Signed Integer Max value is smaller than Min value.");
}
}

internal static void ValidateIntegerInputs(string? max, string? min)
{
var maxValue = Convert.ToInt32(max);
var minValue = Convert.ToInt32(min);

if (maxValue < minValue)
{
throw new ArgumentException("Integer Max value is smaller then Min value.");
}

}

internal static void ValidateLongInputs(string? max, string? min)
Expand Down Expand Up @@ -137,6 +145,11 @@ internal static void ValidateInputParameters(string? max, string? min)
return; //nothing to validate
}

if (typeof(TValue) == typeof(sbyte))
{
ValidateSByteInputs(max, min);
}

if (typeof(TValue) == typeof(int))
{
ValidateIntegerInputs(max, min);
Expand All @@ -149,7 +162,6 @@ internal static void ValidateInputParameters(string? max, string? min)

if (typeof(TValue) == typeof(short))
{

ValidateShortInputs(max, min);
}

Expand All @@ -165,7 +177,6 @@ internal static void ValidateInputParameters(string? max, string? min)

if (typeof(TValue) == typeof(decimal))
{

ValidateDecimalInputs(max, min);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Components/NumberField/FluentNumberField.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ private static string GetStepAttributeValue()
// Unwrap Nullable<T>, because InputBase already deals with the Nullable aspect
// of it for us. We will only get asked to parse the T for nonempty inputs.
var targetType = Nullable.GetUnderlyingType(typeof(TValue)) ?? typeof(TValue);
if (targetType == typeof(int) ||
if (targetType == typeof(sbyte) ||
targetType == typeof(int) ||
targetType == typeof(long) ||
targetType == typeof(short) ||
targetType == typeof(float) ||
Expand Down Expand Up @@ -119,6 +120,7 @@ protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(fa
return value switch
{
null => null,
sbyte @sbyte => BindConverter.FormatValue(Convert.ToInt16(@sbyte), CultureInfo.InvariantCulture),
int @int => BindConverter.FormatValue(@int, CultureInfo.InvariantCulture),
long @long => BindConverter.FormatValue(@long, CultureInfo.InvariantCulture),
short @short => BindConverter.FormatValue(@short, CultureInfo.InvariantCulture),
Expand Down

0 comments on commit 80bd058

Please sign in to comment.