Skip to content

Commit 2079ddb

Browse files
authored
feat(EditorForm): merge Items and FieldItems parameter (#7646)
* doc: 更新注释信息 * refactor: 增强过滤功能 * test: 增加单元测试 * refactor: 优化性能
1 parent 9cfd787 commit 2079ddb

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public partial class EditorForm<TModel> : IShowLabel, IDisposable
8585
public int? LabelWidth { get; set; }
8686

8787
/// <summary>
88-
/// <para lang="zh">获得/设置 列模板 设置 <see cref="Items"/> 时本参数不生效</para>
89-
/// <para lang="en">Gets or sets Field Items Template. Not effective when <see cref="Items"/> is set</para>
88+
/// <para lang="zh">获得/设置 绑定属性集合模板 设置 <see cref="Items"/> 时本参数优先级高</para>
89+
/// <para lang="en">Gets or sets the field items collection template.</para>
9090
/// </summary>
9191
[Parameter]
9292
public RenderFragment<TModel>? FieldItems { get; set; }
@@ -158,8 +158,8 @@ public partial class EditorForm<TModel> : IShowLabel, IDisposable
158158
public List<string>? IgnoreItems { get; set; }
159159

160160
/// <summary>
161-
/// <para lang="zh">获得/设置 级联上下文绑定字段信息集合 设置此参数后 <see cref="FieldItems"/> 模板不生效</para>
162-
/// <para lang="en">Gets or sets Context Field Items Collection. <see cref="FieldItems"/> template will not be effective if set</para>
161+
/// <para lang="zh">获得/设置 绑定字段信息集合 设置此参数后 <see cref="FieldItems"/> 模板优先级更高</para>
162+
/// <para lang="en">Gets or sets the items collection.</para>
163163
/// </summary>
164164
[Parameter]
165165
public IEnumerable<IEditorItem>? Items { get; set; }
@@ -294,7 +294,9 @@ private List<IEditorItem> GetRenderItems()
294294
var items = new List<IEditorItem>();
295295
if (Items != null)
296296
{
297-
items.AddRange(Items.Where(i => !i.GetIgnore() && !string.IsNullOrEmpty(i.GetFieldName()) && FilterIgnoreItem(i)));
297+
var editorFieldNames = new HashSet<string?>(_editorItems.Select(item => item.GetFieldName()));
298+
items.AddRange(Items.Where(i => !editorFieldNames.Contains(i.GetFieldName()) && FilterEditorItem(i)));
299+
items.AddRange(_editorItems.Where(FilterEditorItem));
298300
return items;
299301
}
300302

@@ -309,6 +311,8 @@ private List<IEditorItem> GetRenderItems()
309311
return items;
310312
}
311313

314+
private bool FilterEditorItem(IEditorItem i) => !i.GetIgnore() && !string.IsNullOrEmpty(i.GetFieldName()) && FilterIgnoreItem(i);
315+
312316
private bool FilterIgnoreItem(IEditorItem item)
313317
{
314318
return IgnoreItems?.Find(i => i.Equals(item.GetFieldName(), StringComparison.OrdinalIgnoreCase)) == null;

test/UnitTest/Components/EditorFormTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,29 @@ public void Items_Ok()
6868
pb.Add(a => a.Items, new List<IEditorItem>
6969
{
7070
new InternalTableColumn("Id", typeof(int)) { Ignore = true },
71+
new InternalTableColumn("Name", typeof(string)),
7172
new TableTemplateColumn<Foo>()
7273
});
74+
75+
pb.Add(a => a.FieldItems, f => builder =>
76+
{
77+
var index = 0;
78+
builder.OpenComponent<EditorItem<Foo, string>>(index++);
79+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.Field), f.Name);
80+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.FieldExpression), Utility.GenerateValueExpression(foo, nameof(Foo.Name), typeof(string)));
81+
82+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.Required), true);
83+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.RequiredErrorMessage), "Test");
84+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.Readonly), true);
85+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.SkipValidate), false);
86+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.Text), "Test-Text");
87+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.ComponentType), typeof(BootstrapInput<string>));
88+
builder.AddAttribute(index++, nameof(EditorItem<Foo, string>.ComponentParameters), new KeyValuePair<string, object>[]
89+
{
90+
new("type", "text")
91+
});
92+
builder.CloseComponent();
93+
});
7394
});
7495
}
7596

0 commit comments

Comments
 (0)