mirror of
https://gitee.com/LongbowEnterprise/BootstrapBlazor.git
synced 2024-12-05 05:29:47 +08:00
!3579 feat(#I641HK): add AutoSelectFirstWhenValueIsNull on RadioList component
* chore: bump version 7.0.10 * test: 增加单元测试 * doc: 更新示例 * feat: 增加 AutoSelectFirstWhenValueIsNull 参数
This commit is contained in:
parent
6cf735da98
commit
13de2669e8
@ -70,7 +70,7 @@ public sealed partial class Radios
|
||||
};
|
||||
Items = new SelectedItem[]
|
||||
{
|
||||
new SelectedItem("1", Localizer["Add1"]) { Active = true },
|
||||
new SelectedItem("1", Localizer["Add1"]),
|
||||
new SelectedItem("2", Localizer["Add2"])
|
||||
};
|
||||
Model = Foo.Generate(LocalizerFoo);
|
||||
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>7.0.10-beta03</Version>
|
||||
<Version>7.0.10</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
|
||||
|
@ -42,6 +42,12 @@ public partial class RadioList<TValue>
|
||||
[Parameter]
|
||||
public RenderFragment<SelectedItem>? ItemTemplate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 未设置选中项时是否自动选择第一项 默认 true
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public bool AutoSelectFirstWhenValueIsNull { get; set; } = true;
|
||||
|
||||
private string? GroupName => Id;
|
||||
|
||||
private string? RadioClassString => CssBuilder.Default("radio-list")
|
||||
@ -64,7 +70,7 @@ public partial class RadioList<TValue>
|
||||
|
||||
NullItemText ??= "";
|
||||
|
||||
if (!Items.Any(i => i.Value == CurrentValueAsString))
|
||||
if (AutoSelectFirstWhenValueIsNull && !Items.Any(i => i.Value == CurrentValueAsString))
|
||||
{
|
||||
CurrentValueAsString = Items.FirstOrDefault()?.Value ?? "";
|
||||
}
|
||||
@ -77,6 +83,31 @@ public partial class RadioList<TValue>
|
||||
/// <returns></returns>
|
||||
protected override string? FormatValueAsString(TValue value) => value is SelectedItem v ? v.Value : base.FormatValueAsString(value);
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc/>
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="result"></param>
|
||||
/// <param name="validationErrorMessage"></param>
|
||||
/// <returns></returns>
|
||||
protected override bool TryParseValueFromString(string value, [MaybeNullWhen(false)] out TValue result, out string? validationErrorMessage)
|
||||
{
|
||||
var ret = false;
|
||||
var t = NullableUnderlyingType ?? typeof(TValue);
|
||||
result = default;
|
||||
if (t == typeof(SelectedItem))
|
||||
{
|
||||
var item = Items.FirstOrDefault(i => i.Value == value);
|
||||
if (item != null)
|
||||
{
|
||||
result = (TValue)(object)item;
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
validationErrorMessage = null;
|
||||
return ret || base.TryParseValueFromString(value, out result, out validationErrorMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
@ -170,6 +170,23 @@ public class RadioTest : BootstrapBlazorTestBase
|
||||
cut.Contains("item-template-2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AutoSelectFirstWhenValueIsNull_Ok()
|
||||
{
|
||||
var cut = Context.RenderComponent<RadioList<SelectedItem>>(pb =>
|
||||
{
|
||||
pb.Add(a => a.AutoSelectFirstWhenValueIsNull, false);
|
||||
pb.Add(a => a.Items, new List<SelectedItem>
|
||||
{
|
||||
new("1", "Test1"),
|
||||
new("2", "Test2")
|
||||
});
|
||||
pb.Add(a => a.Value, new SelectedItem());
|
||||
});
|
||||
cut.Contains("class=\"form-check-label\"");
|
||||
cut.DoesNotContain("is-checked");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateForm_Ok()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user