Skip to content

Commit 48607cb

Browse files
authored
feat(EditorForm): add IgnoreItems parameter (#7607)
* refactor: 代码重构提高可读性 * feat: 增加 IgnoreItems 属性 * feat: 增加忽略集合过滤逻辑 * doc: 更新示例 * chore: bump version 10.3.1-beta01 * test: 更新单元测试
1 parent 88bed84 commit 48607cb

File tree

7 files changed

+108
-43
lines changed

7 files changed

+108
-43
lines changed

src/BootstrapBlazor.Server/Components/Samples/EditorForms.razor

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@
138138
</EditorForm>
139139
</DemoBlock>
140140

141+
<DemoBlock Title="@Localizer["IgnoreItemsTitle"]" Introduction="@Localizer["IgnoreItemsIntro"]" Name="IgnoreItems">
142+
<section ignore>
143+
<div>@((MarkupString)Localizer["IgnoreItemsDescription"].Value)</div>
144+
<Button OnClick="OnSwitchIgnoreItems" Text="@Localizer["IgnoreItemsSwitchButtonText"]" class="mt-3"></Button>
145+
</section>
146+
<GroupBox Title="@Localizer["GroupBoxTitle"]" style="margin-top: 1.5rem;">
147+
<EditorForm Model="@Model" AutoGenerateAllItem="true" IgnoreItems="@_ignoreItems">
148+
</EditorForm>
149+
</GroupBox>
150+
</DemoBlock>
151+
141152
<AttributeTable Type="typeof(EditorForm<>)" />
142153

143-
<AttributeTable Title="@Localizer["EditorFormAttributeTitle"]" Type="typeof(EditorItem<,>)"></AttributeTable>
154+
<AttributeTable Type="typeof(EditorItem<,>)"></AttributeTable>

src/BootstrapBlazor.Server/Components/Samples/EditorForms.razor.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -25,6 +25,21 @@ public sealed partial class EditorForms
2525
[NotNull]
2626
private IStringLocalizer<Foo>? FooLocalizer { get; set; }
2727

28+
private List<string> _ignoreItems = [];
29+
30+
private Task OnSwitchIgnoreItems()
31+
{
32+
if (_ignoreItems.Count != 0)
33+
{
34+
_ignoreItems.Clear();
35+
}
36+
else
37+
{
38+
_ignoreItems = [nameof(Foo.Hobby)];
39+
}
40+
return Task.CompletedTask;
41+
}
42+
2843
private List<SelectedItem> DummyItems { get; } =
2944
[
3045
new SelectedItem("1", "1"),

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,6 @@
21282128
"AutoGenerateIntro": "By setting the property <code>AutoGenerateAllItem</code> value of <code>false</code> the property is prohibited from being automatically generated, and the display property is controlled by setting the <code>FieldItems</code> internal collection",
21292129
"AutoGenerateTitle": "It is not automatically generated by default",
21302130
"Description": "Edit forms are automatically rendered through the bound data model",
2131-
"EditorFormAttributeTitle": "EditorItem Attribute",
21322131
"EditorFormTips1": "The binding model automatically generates all properties by default, and you can change to not automatically build by setting <code>AutoGenerateAllItem</code>",
21332132
"EditorFormTips2": "If you don't need to edit the columns, set the <code>Editable</code>, the default is <code>true</code> build editing components",
21342133
"EditorFormTips3": "Complex edit columns, set up <code>EditTemplate</code> templates, and edit custom components",
@@ -2153,7 +2152,11 @@
21532152
"ValidateFormIntro": "Data compliance checks are implemented through nested <code>ValidateForm</code> components",
21542153
"ValidateFormTips1": "The component is built into the <code>ValidateForm</code> to turn on data compliance checks, and <b>hobby</b>fields use the <code>EditTemplate</code> template to customize the component to render the data",
21552154
"ValidateFormTips2": "Make the <code>birthday</code> field read-only by setting the <code>Readonly</code> property",
2156-
"ValidateFormTitle": "Turn on data validation"
2155+
"ValidateFormTitle": "Turn on data validation",
2156+
"IgnoreItemsTitle": "IgnoreItems",
2157+
"IgnoreItemsIntro": "By setting the <code>IgnoreItems</code> value, you can notify the component not to render the specified column in the collection.",
2158+
"IgnoreItemsDescription": "This attribute has the same priority as `<editorItem>`, and if it is set to ignore anywhere else, it will not be rendered.",
2159+
"IgnoreItemsSwitchButtonText": "Switch"
21572160
},
21582161
"BootstrapBlazor.Server.Components.Samples.Editors": {
21592162
"DoMethodAsyncButton1": "Insert Html",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,6 @@
21282128
"AutoGenerateIntro": "通过设置属性 <code>AutoGenerateAllItem</code> 值为 <code>false</code>,禁止自动生成属性,通过设置 <code>FieldItems</code> 内部集合来控制显示属性",
21292129
"AutoGenerateTitle": "默认不自动生成",
21302130
"Description": "通过绑定数据模型自动呈现编辑表单",
2131-
"EditorFormAttributeTitle": "EditorItem 属性",
21322131
"EditorFormTips1": "绑定模型默认自动生成全部属性,可以通过设置 <code>AutoGenerateAllItem</code> 更改为不自动生成",
21332132
"EditorFormTips2": "如不需要编辑列,设置 <code>Editable</code> 即可,默认值为 <code>true</code> 生成编辑组件",
21342133
"EditorFormTips3": "复杂编辑列,设置 <code>EditTemplate</code> 模板,进行自定义组件进行编辑",
@@ -2153,7 +2152,11 @@
21532152
"ValidateFormIntro": "通过嵌套 <code>ValidateForm</code> 组件实现数据合规检查功能",
21542153
"ValidateFormTips1": "组件内置到 <code>ValidateForm</code> 内开启数据合规检查功能,<b>爱好</b> 字段使用 <code>EditTemplate</code> 模板自定义组件呈现数据",
21552154
"ValidateFormTips2": "通过设置 <code>Readonly</code> 属性,使 <code>生日</code> 字段为只读",
2156-
"ValidateFormTitle": "开启数据验证"
2155+
"ValidateFormTitle": "开启数据验证",
2156+
"IgnoreItemsTitle": "IgnoreItems 忽略集合",
2157+
"IgnoreItemsIntro": "通过设置 <code>IgnoreItems</code> 值通知组件不渲染集合中的指定列",
2158+
"IgnoreItemsDescription": "此属性的优先级与在 <code>IEditorItem</code> 相同,任何一个地方设置了忽略,最终都不会渲染",
2159+
"IgnoreItemsSwitchButtonText": "切换"
21572160
},
21582161
"BootstrapBlazor.Server.Components.Samples.Editors": {
21592162
"DoMethodAsyncButton1": "插入一段 Html",

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>10.3.0</Version>
4+
<Version>10.3.1-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ public partial class EditorForm<TModel> : IShowLabel, IDisposable
149149
[Parameter]
150150
public bool AutoGenerateAllItem { get; set; } = true;
151151

152+
/// <summary>
153+
/// <para lang="zh">获得/设置 忽略项目集合 默认 null 未设置</para>
154+
/// <para lang="en">Gets or sets the ignore items collection. Default is null</para>
155+
/// </summary>
156+
[Parameter]
157+
public List<string>? IgnoreItems { get; set; }
158+
152159
/// <summary>
153160
/// <para lang="zh">获得/设置 级联上下文绑定字段信息集合 设置此参数后 <see cref="FieldItems"/> 模板不生效</para>
154161
/// <para lang="en">Gets or sets Context Field Items Collection. <see cref="FieldItems"/> template will not be effective if set</para>
@@ -279,49 +286,55 @@ private List<IEditorItem> GetRenderItems()
279286
var items = new List<IEditorItem>();
280287
if (Items != null)
281288
{
282-
items.AddRange(Items.Where(i => !i.GetIgnore() && !string.IsNullOrEmpty(i.GetFieldName())));
289+
items.AddRange(Items.Where(i => !i.GetIgnore() && !string.IsNullOrEmpty(i.GetFieldName()) && FilterIgnoreItem(i)));
290+
return items;
283291
}
284-
else
292+
293+
// 如果 EditorItems 有值表示 用户自定义列
294+
// If EditorItems has value, it means user defined columns
295+
var columns = AutoGenerateAllItem
296+
? AutoGenerateColumns()
297+
: _editorItems.Where(i => !i.GetIgnore()
298+
&& !string.IsNullOrEmpty(i.GetFieldName())
299+
&& i.IsVisible(ItemChangedType, IsSearch.Value));
300+
items.AddRange(columns.Where(i => FilterIgnoreItem(i)));
301+
return items;
302+
}
303+
304+
private bool FilterIgnoreItem(IEditorItem item)
305+
{
306+
return IgnoreItems?.Find(i => i.Equals(item.GetFieldName(), StringComparison.OrdinalIgnoreCase)) == null;
307+
}
308+
309+
private List<ITableColumn> AutoGenerateColumns()
310+
{
311+
// 获取绑定模型所有属性
312+
// Get all properties of binding model
313+
var columns = Utility.GetTableColumns<TModel>(defaultOrderCallback: ColumnOrderCallback).ToList();
314+
315+
// 通过设定的 FieldItems 模板获取项进行渲染
316+
// Render items by setting FieldItems template
317+
foreach (var el in _editorItems)
285318
{
286-
// 如果 EditorItems 有值表示 用户自定义列
287-
// If EditorItems has value, it means user defined columns
288-
if (AutoGenerateAllItem)
319+
var item = columns.FirstOrDefault(i => i.GetFieldName() == el.GetFieldName());
320+
if (item != null)
289321
{
290-
// 获取绑定模型所有属性
291-
// Get all properties of binding model
292-
var columns = Utility.GetTableColumns<TModel>(defaultOrderCallback: ColumnOrderCallback).ToList();
293-
294-
// 通过设定的 FieldItems 模板获取项进行渲染
295-
// Render items by setting FieldItems template
296-
foreach (var el in _editorItems)
322+
// 过滤掉不编辑与不可见的列
323+
// Filter out non-editable and invisible columns
324+
if (el.GetIgnore() || !el.IsVisible(ItemChangedType, IsSearch.Value) || string.IsNullOrEmpty(el.GetFieldName()))
297325
{
298-
var item = columns.FirstOrDefault(i => i.GetFieldName() == el.GetFieldName());
299-
if (item != null)
300-
{
301-
// 过滤掉不编辑与不可见的列
302-
// Filter out non-editable and invisible columns
303-
if (el.GetIgnore() || !el.IsVisible(ItemChangedType, IsSearch.Value) || string.IsNullOrEmpty(el.GetFieldName()))
304-
{
305-
columns.Remove(item);
306-
}
307-
else
308-
{
309-
// 设置只读属性与列模板
310-
// Set Readonly property and column template
311-
item.CopyValue(el);
312-
}
313-
}
326+
columns.Remove(item);
327+
}
328+
else
329+
{
330+
// 设置只读属性与列模板
331+
// Set Readonly property and column template
332+
item.CopyValue(el);
314333
}
315-
items.AddRange(columns);
316-
}
317-
else
318-
{
319-
items.AddRange(_editorItems.Where(i => !i.GetIgnore()
320-
&& !string.IsNullOrEmpty(i.GetFieldName())
321-
&& i.IsVisible(ItemChangedType, IsSearch.Value)));
322334
}
323335
}
324-
return items;
336+
337+
return columns;
325338
}
326339

327340
private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder =>

test/UnitTest/Components/EditorFormTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ public void AutoGenerateAllItem_True(bool autoGenerate)
8787
});
8888
}
8989

90+
[Fact]
91+
public void IgnoreItems_Ok()
92+
{
93+
var ignoreItems = new List<string> { nameof(Foo.Hobby) };
94+
var foo = new Foo();
95+
var cut = Context.Render<EditorForm<Foo>>(pb =>
96+
{
97+
pb.Add(a => a.AutoGenerateAllItem, true);
98+
pb.Add(a => a.Model, foo);
99+
pb.Add(a => a.IgnoreItems, ignoreItems);
100+
});
101+
cut.DoesNotContain("爱好");
102+
103+
cut.Render(pb =>
104+
{
105+
pb.Add(a => a.IgnoreItems, []);
106+
});
107+
cut.Contains("爱好");
108+
}
109+
90110
[Fact]
91111
public void IsDisplay_Ok()
92112
{

0 commit comments

Comments
 (0)