!2364 feat(#I4SFT5): table dynamic mode support CheckboxList<string> component

* chore: bump version beta08
* refactor: 更新判断 ChekboxList 逻辑
* 增加 CheckboxList 组件测试单元
* 增加手动设定 ComponentType 为 CheckboxList<string> 并且 Items 有值 自动生成组件
This commit is contained in:
alex_zou 2022-01-28 13:04:15 +00:00 committed by Argo
parent fe2d87ee81
commit d25b31ec6d
3 changed files with 105 additions and 5 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<Version>6.2.9-beta07</Version>
<Version>6.2.9-beta08</Version>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">

View File

@ -310,7 +310,7 @@ public static class Utility
}
}
if (IsCheckboxList(fieldType) && item.Items != null)
if (IsCheckboxList(fieldType, componentType) && item.Items != null)
{
builder.AddAttribute(7, nameof(CheckboxList<IEnumerable<string>>.Items), item.Items.Clone());
}
@ -423,11 +423,21 @@ public static class Utility
/// 通过指定数据类型判断是否可使用 CheckboxList 进行渲染
/// </summary>
/// <param name="fieldType"></param>
/// <param name="componentType">组件类型</param>
/// <returns></returns>
private static bool IsCheckboxList(Type fieldType)
private static bool IsCheckboxList(Type fieldType, Type? componentType = null)
{
var type = (Nullable.GetUnderlyingType(fieldType) ?? fieldType);
return type.IsAssignableTo(typeof(IEnumerable<string>));
var ret = false;
if (componentType != null)
{
ret = componentType.GetGenericTypeDefinition() == typeof(CheckboxList<>);
}
if (!ret)
{
var type = Nullable.GetUnderlyingType(fieldType) ?? fieldType;
ret = type.IsAssignableTo(typeof(IEnumerable<string>));
}
return ret;
}
private static bool IsValidatableComponent(Type componentType) => componentType.GetProperties().FirstOrDefault(p => p.Name == nameof(IEditorItem.SkipValidate)) != null;

View File

@ -0,0 +1,90 @@
// 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 UnitTest.Components;
public class CheckboxListTest : BootstrapBlazorTestBase
{
IEnumerable<SelectedItem>? Items1 { get; set; } = new List<SelectedItem>(new List<SelectedItem> {
new SelectedItem { Text = "Item 1", Value = "1" },
new SelectedItem { Text = "Item 2", Value = "2" },
new SelectedItem { Text = "Item 3", Value = "3" },
new SelectedItem { Text = "Item 4", Value = "4" }
});
string Value1 { get; set; } = "1,3";
IEnumerable<int>? Value2 { get; set; } = new int[] { 3, 4 };
[Fact]
public void Components_Ok()
{
var cut = Context.RenderComponent<CheckboxList<string>>(builder => {
builder.Add(a => a.DisplayText, "长沙");
builder.Add(a => a.ShowLabel, true);
builder.Add(a => a.Value, Value1);
builder.Add(a => a.Items, Items1);
}
);
Assert.Contains("class=\"form-control checkbox-list\"", cut.Markup);
}
[Fact]
public void Items_Ok()
{
var cut = Context.RenderComponent<CheckboxList<string>>(builder => {
builder.Add(a => a.ShowLabel, true);
builder.Add(a => a.Items, Items1);
}
);
Assert.Contains("Item 3", cut.Markup);
}
[Fact]
public void Value_String_Ok()
{
var cut = Context.RenderComponent<CheckboxList<string>>(builder => {
builder.Add(a => a.Value, Value1);
builder.Add(a => a.Items, Items1);
}
);
Assert.Contains("form-check is-checked", cut.Markup);
Assert.Contains("Item 3", cut.Markup);
}
//[Fact]
//public void Value_IEnumerable_Ok()
//{
// var cut = Context.RenderComponent<CheckboxList<string>>(builder => {
// builder.Add(a => a.Value, Value2);
// builder.Add(a => a.Items, Items1);
// }
// );
// Assert.Contains("form-check is-checked", cut.Markup);
// Assert.Contains("Item 4", cut.Markup);
//}
private RenderFragment GenerateCheckboxList() => builder =>
{
builder.OpenComponent(0, typeof(CheckboxList<string>));
builder.AddAttribute(1, nameof(CheckboxList<string>.DisplayText), "长沙市");
builder.AddAttribute(2, nameof(CheckboxList<string>.ShowLabel), true);
builder.AddAttribute(2, nameof(CheckboxList<string>.Value), Value1);
builder.AddAttribute(7, nameof(CheckboxList<string>.Items), Items1);
builder.CloseComponent();
};
[Fact]
public void Generate_Ok()
{
var cut = Context.Render(GenerateCheckboxList());
Assert.Contains("form-check is-checked", cut.Markup);
Assert.Contains("Item 3", cut.Markup);
}
}