Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NumberField] Add sbyte type #2308

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading