Skip to content

Commit 91f0e77

Browse files
ArgoZhangMadLongTomdensen2014
authored
fix(BootstrapInputNumber): throw exception when NumberDecimalSeparator is comma (#5028)
* feat: 增加 ShouldRenderInputNumber 扩展方法 * chore: bump version 9.2.3 Co-Authored-By: MadLongTom <[email protected]> Co-Authored-By: Alex chow <[email protected]>
1 parent 47d2463 commit 91f0e77

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.2.3-beta01</Version>
4+
<Version>9.2.3</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Extensions/ObjectExtensions.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,24 @@ public static string ConvertToPercentString(this string? val)
5050
/// <returns></returns>
5151
public static bool IsNumber(this Type t)
5252
{
53-
var separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
53+
var targetType = Nullable.GetUnderlyingType(t) ?? t;
54+
return targetType == typeof(int) || targetType == typeof(long) || targetType == typeof(short) ||
55+
targetType == typeof(float) || targetType == typeof(double) || targetType == typeof(decimal);
56+
}
57+
58+
/// <summary>
59+
/// 检查是否应该渲染成 <see cref="BootstrapInputNumber{TValue}"/>
60+
/// </summary>
61+
/// <param name="t"></param>
62+
/// <returns></returns>
63+
public static bool IsNumberWithDotSeparator(this Type t)
64+
{
65+
var separator = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
5466
if (separator != ".")
5567
{
5668
return false;
5769
}
58-
59-
var targetType = Nullable.GetUnderlyingType(t) ?? t;
60-
return targetType == typeof(int) || targetType == typeof(long) || targetType == typeof(short) ||
61-
targetType == typeof(float) || targetType == typeof(double) || targetType == typeof(decimal);
70+
return t.IsNumber();
6271
}
6372

6473
/// <summary>

src/BootstrapBlazor/Utils/Utility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ private static Type GenerateComponentType(IEditorItem item)
638638
{
639639
ret = typeof(NullSwitch);
640640
}
641-
else if (fieldType.IsNumber())
641+
else if (fieldType.IsNumberWithDotSeparator())
642642
{
643643
ret = typeof(BootstrapInputNumber<>).MakeGenericType(fieldType);
644644
}
@@ -704,7 +704,7 @@ private static Dictionary<string, object> CreateMultipleAttributes(Type fieldTyp
704704
ret.Add("rows", item.Rows);
705705
}
706706
}
707-
else if (type.IsNumber())
707+
else if (type.IsNumberWithDotSeparator())
708708
{
709709
if (!string.IsNullOrEmpty(item.Step))
710710
{

test/UnitTest/Extensions/ObjectExtensionsTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ public static void IsNumber_Ok(Type source, bool expect)
4848
public void IsNumber_Culture()
4949
{
5050
var culture = new CultureInfo("es-ES");
51-
CultureInfo.CurrentCulture = culture;
52-
Assert.False(typeof(long).IsNumber());
51+
CultureInfo.CurrentUICulture = culture;
52+
Assert.True(typeof(long).IsNumber());
53+
Assert.False(typeof(long).IsNumberWithDotSeparator());
5354

5455
culture = new CultureInfo("en-US");
55-
CultureInfo.CurrentCulture = culture;
56+
CultureInfo.CurrentUICulture = culture;
5657
Assert.True(typeof(long).IsNumber());
58+
Assert.True(typeof(long).IsNumberWithDotSeparator());
5759
}
5860

5961
[Theory]

0 commit comments

Comments
 (0)