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

feat(InputNumber): add global setting for step #2268

Merged
merged 6 commits into from
Oct 14, 2023
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
10 changes: 9 additions & 1 deletion src/BootstrapBlazor.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@
"TableSettings": {
"CheckboxColumnWidth": 36
},
"IgnoreLocalizerMissing": true
"IgnoreLocalizerMissing": true,
"StepSettings": {
"Short": 1,
"Int": 1,
"Long": 1,
"Float": "0.1",
"Double": "0.01",
"Decimal": "0.01"
}
},
"Cache-Control": {
"Files": [ ".png", ".gif", ".jpg", ".jpeg", ".svg" ],
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>7.11.1-beta06</Version>
<Version>7.11.1-beta07</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using BootstrapBlazor.Extensions;
using Microsoft.Extensions.Localization;
using System.Globalization;

Expand Down Expand Up @@ -90,6 +91,10 @@ public partial class BootstrapInputNumber<TValue>
[NotNull]
private IIconTheme? IconTheme { get; set; }

[Inject]
[NotNull]
private IOptions<BootstrapBlazorOptions>? StepOption { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand Down Expand Up @@ -155,6 +160,8 @@ private void SetStep()
StepString = Step;
break;
}

StepString ??= StepOption.Value.GetStep<TValue>();
}

/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions src/BootstrapBlazor/Extensions/BootstrapBlazorOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

namespace BootstrapBlazor.Extensions;

/// <summary>
/// BootstrapBlazorOptions 配置类扩展方法
/// </summary>
public static class BootstrapBlazorOptionsExtensions
{
/// <summary>
/// 获取步长泛型方法
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <param name="options"></param>
/// <returns></returns>
public static string? GetStep<TType>(this BootstrapBlazorOptions options) => options.GetStep(typeof(TType));

/// <summary>
/// 获取步长方法
/// </summary>
/// <param name="options">配置实体类实例</param>
/// <param name="type">数据类型</param>
/// <returns></returns>
public static string? GetStep(this BootstrapBlazorOptions options, Type type)
{
var t = Nullable.GetUnderlyingType(type) ?? type;
return options.StepSettings.GetStep(t);
}
}
5 changes: 5 additions & 0 deletions src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public class BootstrapBlazorOptions
/// </summary>
public TableSettings TableSettings { get; set; } = new();

/// <summary>
/// 获得/设置 Step 配置实例
/// </summary>
public StepSettings StepSettings { get; set; } = new();

/// <summary>
/// 获得/设置 是否禁用表单内回车自动提交功能 默认 null 未设置
/// </summary>
Expand Down
76 changes: 76 additions & 0 deletions src/BootstrapBlazor/Options/StepSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

namespace BootstrapBlazor.Components;

/// <summary>
/// StepSettings 配置类
/// </summary>
public class StepSettings
{
/// <summary>
/// 获得/设置 int 数据类型步长 默认 null 未设置
/// </summary>
public int? Int { get; set; }

/// <summary>
/// 获得/设置 long 数据类型步长 默认 null 未设置
/// </summary>
public int? Long { get; set; }

/// <summary>
/// 获得/设置 short 数据类型步长 默认 null 未设置
/// </summary>
public int? Short { get; set; }

/// <summary>
/// 获得/设置 float 数据类型步长 默认 null 未设置
/// </summary>
public float? Float { get; set; }

/// <summary>
/// 获得/设置 double 数据类型步长 默认 null 未设置
/// </summary>
public double? Double { get; set; }

/// <summary>
/// 获得/设置 decimal 数据类型步长 默认 null 未设置
/// </summary>
public decimal? Decimal { get; set; }

/// <summary>
/// 获得步长字符串
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public string? GetStep(Type type)
{
string? ret = null;
if (type == typeof(int))
{
ret = Int?.ToString();
}
if (type == typeof(long))
{
ret = Long?.ToString();
}
if (type == typeof(short))
{
ret = Short?.ToString();
}
if (type == typeof(float))
{
ret = Float?.ToString();
}
if (type == typeof(double))
{
ret = Double?.ToString();
}
if (type == typeof(decimal))
{
ret = Decimal?.ToString();
}
return ret;
}
}
33 changes: 33 additions & 0 deletions test/UnitTest/Options/BootstrapBlazorOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using BootstrapBlazor.Extensions;

namespace UnitTest.Options;

public class BootstrapBlazorOptionsTest
Expand Down Expand Up @@ -39,4 +41,35 @@ public void Options_IgnoreLocalizerMissing_Null()
options.IgnoreLocalizerMissing = true;
Assert.True(options.IgnoreLocalizerMissing.Value);
}

[Fact]
public void Options_StepSettings()
{
var options = new BootstrapBlazorOptions();
Assert.NotNull(options.StepSettings);

options.StepSettings = new();

Assert.Null(options.GetStep<short?>());
Assert.Null(options.GetStep<int?>());
Assert.Null(options.GetStep<long?>());
Assert.Null(options.GetStep<float?>());
Assert.Null(options.GetStep<double?>());
Assert.Null(options.GetStep<decimal?>());

options.StepSettings.Short = 1;
options.StepSettings.Int = 2;
options.StepSettings.Long = 3;
options.StepSettings.Float = 0.1f;
options.StepSettings.Double = 0.01d;
options.StepSettings.Decimal = 0.001M;

Assert.Equal("1", options.GetStep<short?>());
Assert.Equal("2", options.GetStep<int?>());
Assert.Equal("3", options.GetStep(typeof(long?)));

Assert.Equal("0.1", options.GetStep<float?>());
Assert.Equal("0.01", options.GetStep<double?>());
Assert.Equal("0.001", options.GetStep<decimal?>());
}
}