Skip to content

Commit eeb045d

Browse files
authored
feat(IEditor): add Cols parameter (#5778)
* doc: 更新 DemoTableEditTemplate 逻辑 * feat: 增加 Cols 参数 * test: 更新单元测试 * refactor: IsCheckBoxList 公开化 * refactor: 重构自动判定列宽逻辑 * refactor: 重构代码 * test: 更新单元测试
1 parent c2aee77 commit eeb045d

File tree

15 files changed

+80
-36
lines changed

15 files changed

+80
-36
lines changed

src/BootstrapBlazor.Server/Components/Components/DemoTableEditTemplate.razor

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,18 @@
44

55
<div class="row form-inline g-3">
66
<div class="col-12 col-sm-6">
7-
<BootstrapInput @bind-Value="Model.Name" />
7+
<BootstrapInput @bind-Value="Model.Name"></BootstrapInput>
88
</div>
99
<div class="col-12 col-sm-6">
10-
<MultiSelect @bind-Value="@Model.Hobby" Items="Hobbies" />
10+
<MultiSelect @bind-Value="@Model.Hobby" Items="Hobbies"></MultiSelect>
1111
</div>
1212
<div class="col-12">
13-
<Textarea @bind-Value="Model.Address" rows="3"/>
13+
<Textarea @bind-Value="Model.Address" rows="3"></Textarea>
1414
</div>
1515
<div class="col-12 col-sm-6">
16-
<Select @bind-Value="Model.Education" />
16+
<Select @bind-Value="Model.Education"></Select>
1717
</div>
1818
<div class="col-12 col-sm-6">
19-
<Display Value="@EducationDesc" ShowLabel="true" DisplayText="@Localizer["TablesEditTemplateDisplayLabel"]" />
19+
<Display Value="@EducationDesc" ShowLabel="true" DisplayText="@Localizer["TablesEditTemplateDisplayLabel"]"></Display>
2020
</div>
2121
</div>
22-
23-
@code {
24-
[Parameter]
25-
[NotNull]
26-
public Foo? Model { get; set; }
27-
28-
private IEnumerable<SelectedItem>? Hobbies { get; set; }
29-
30-
private string? EducationDesc => Model.Education == EnumEducation.Primary ? Localizer["TablesEditTemplateDisplayDetail1"] : Localizer["TablesEditTemplateDisplayDetail2"];
31-
32-
/// <summary>
33-
/// <inheritdoc/>
34-
/// </summary>
35-
protected override void OnInitialized()
36-
{
37-
Hobbies = Foo.GenerateHobbies(LocalizerFoo);
38-
}
39-
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Server.Components.Components;
7+
8+
/// <summary>
9+
/// DemoTableEditTemplate component
10+
/// </summary>
11+
public partial class DemoTableEditTemplate
12+
{
13+
/// <summary>
14+
/// Gets or sets the Foo instance.
15+
/// </summary>
16+
[Parameter]
17+
[NotNull]
18+
public Foo? Model { get; set; }
19+
20+
private IEnumerable<SelectedItem>? Hobbies { get; set; }
21+
22+
private string? EducationDesc => Model.Education switch
23+
{
24+
EnumEducation.Primary => Localizer["TablesEditTemplateDisplayDetail1"],
25+
EnumEducation.Middle => Localizer["TablesEditTemplateDisplayDetail2"],
26+
_ => ""
27+
};
28+
29+
/// <summary>
30+
/// <inheritdoc/>
31+
/// </summary>
32+
protected override void OnInitialized()
33+
{
34+
Hobbies = Foo.GenerateHobbies(LocalizerFoo);
35+
}
36+
}

src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ public partial class EditorForm<TModel> : IShowLabel
2727
/// <returns></returns>
2828
private string? GetCssString(IEditorItem item)
2929
{
30-
int cols = 0;
30+
int cols = Math.Max(0, Math.Min(12, item.Cols));
3131
double mdCols = 6;
32-
if (item is AutoGenerateColumnAttribute a && a.Cols > 0 && a.Cols < 13)
33-
{
34-
cols = a.Cols;
35-
}
3632
if (ItemsPerRow.HasValue)
3733
{
38-
mdCols = Math.Min(12, Math.Ceiling(12d / ItemsPerRow.Value));
34+
mdCols = Math.Max(0, Math.Min(12, Math.Ceiling(12d / ItemsPerRow.Value)));
3935
}
4036
return CssBuilder.Default("col-12")
4137
.AddClass($"col-sm-{cols}", cols > 0) // 指定 Cols
42-
.AddClass($"col-sm-6 col-md-{mdCols}", mdCols < 12 && cols == 0 && item.Items == null && item.Rows == 0) // 指定 ItemsPerRow
38+
.AddClass($"col-sm-6 col-md-{mdCols}", mdCols > 0 && cols == 0 && item.Rows == 0 && !Utility.IsCheckboxList(item.PropertyType, item.ComponentType)) // 指定 ItemsPerRow
4339
.Build();
4440
}
4541

src/BootstrapBlazor/Components/EditorForm/EditorItem.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public class EditorItem<TModel, TValue> : ComponentBase, IEditorItem
9999
[Parameter]
100100
public int Rows { get; set; }
101101

102+
/// <summary>
103+
/// <inheritdoc/>
104+
/// </summary>
105+
[Parameter]
106+
public int Cols { get; set; }
107+
102108
/// <summary>
103109
/// <inheritdoc/>
104110
/// </summary>

src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public interface IEditorItem : ILookup
6666
/// </summary>
6767
int Rows { get; set; }
6868

69+
/// <summary>
70+
/// Gets or sets the field expand columns. Default is 0.
71+
/// </summary>
72+
int Cols { get; set; }
73+
6974
/// <summary>
7075
/// Gets or sets the edit template.
7176
/// </summary>

src/BootstrapBlazor/Components/Table/InternalTableColumn.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ class InternalTableColumn(string fieldName, Type fieldType, string? fieldText =
183183
/// </summary>
184184
public int Rows { get; set; }
185185

186+
/// <summary>
187+
/// <inheritdoc/>
188+
/// </summary>
189+
public int Cols { get; set; }
190+
186191
/// <summary>
187192
/// <inheritdoc/>
188193
/// </summary>

src/BootstrapBlazor/Components/Table/TableColumn.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,19 @@ public class TableColumn<TItem, TType> : BootstrapComponentBase, ITableColumn
112112
public string? Step { get; set; }
113113

114114
/// <summary>
115-
/// 获得/设置 Textarea 行数 默认为 0
115+
/// <inheritdoc/>
116116
/// </summary>
117117
[Parameter]
118118
public int Rows { get; set; }
119119

120120
/// <summary>
121-
/// 获得/设置 是否为默认排序规则 默认为 SortOrder.Unset
121+
/// <inheritdoc/>
122+
/// </summary>
123+
[Parameter]
124+
public int Cols { get; set; }
125+
126+
/// <summary>
127+
/// <inheritdoc/>
122128
/// </summary>
123129
[Parameter]
124130
public SortOrder DefaultSortOrder { get; set; }

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static void CopyValue(this ITableColumn dest, IEditorItem source)
5353
if (source.LookupService != null) dest.LookupService = source.LookupService;
5454
if (source.Readonly.HasValue) dest.Readonly = source.Readonly;
5555
if (source.Rows > 0) dest.Rows = source.Rows;
56+
if (source.Cols > 0) dest.Cols = source.Cols;
5657
if (source.SkipValidate) dest.SkipValidate = source.SkipValidate;
5758
if (!string.IsNullOrEmpty(source.Text)) dest.Text = source.Text;
5859
if (source.ValidateRules != null) dest.ValidateRules = source.ValidateRules;

src/BootstrapBlazor/Utils/Utility.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,12 @@ public static void CreateComponentByFieldType(this RenderTreeBuilder builder, Co
574574
return ret;
575575
}
576576

577-
private static List<SelectedItem> Clone(this IEnumerable<SelectedItem> source) => source.Select(d => new SelectedItem(d.Value, d.Text)
577+
private static List<SelectedItem> Clone(this IEnumerable<SelectedItem> source) => [.. source.Select(d => new SelectedItem(d.Value, d.Text)
578578
{
579579
Active = d.Active,
580580
IsDisabled = d.IsDisabled,
581581
GroupName = d.GroupName
582-
}).ToList();
582+
})];
583583

584584
private static object? GenerateValue(object model, string fieldName) => GetPropertyValue<object, object?>(model, fieldName);
585585

@@ -671,7 +671,7 @@ private static Type GenerateComponentType(IEditorItem item)
671671
/// <param name="fieldType"></param>
672672
/// <param name="componentType">组件类型</param>
673673
/// <returns></returns>
674-
private static bool IsCheckboxList(Type fieldType, Type? componentType = null)
674+
public static bool IsCheckboxList(Type fieldType, Type? componentType = null)
675675
{
676676
var ret = false;
677677
if (componentType != null)

test/UnitTest/Attributes/AutoGenerateClassTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void AutoGenerateColumn_Ok()
5959
ComponentType = typeof(Select<string>),
6060
Step = "1",
6161
Rows = 1,
62+
Cols = 6,
6263
LookupStringComparison = StringComparison.Ordinal,
6364
LookupServiceKey = "test-lookup",
6465
LookupServiceData = true,
@@ -93,6 +94,7 @@ public void AutoGenerateColumn_Ok()
9394
Assert.Equal(typeof(Select<string>), attr.ComponentType);
9495
Assert.Equal("1", attr.Step);
9596
Assert.Equal(1, attr.Rows);
97+
Assert.Equal(6, attr.Cols);
9698
Assert.Equal(StringComparison.Ordinal, attr.LookupStringComparison);
9799
Assert.Equal("Test", attr.GroupName);
98100
Assert.Equal(1, attr.GroupOrder);

0 commit comments

Comments
 (0)