Skip to content

Commit 83489f9

Browse files
authored
feat(EditorForm): add IsRenderWhenValueChanged parameter (#7138)
* feat: 增加 IEditorFormValueChanged 接口 * feat: 实现通知功能 * doc: 更新联动示例 * feat: 增加 IsRenderWhenValueChanged 参数 * chore: bump version 10.0.1 * refactor: 重新设计逻辑 * refactor: 精简代码
1 parent b7e3028 commit 83489f9

File tree

9 files changed

+63
-71
lines changed

9 files changed

+63
-71
lines changed

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/BootstrapBlazor.Server/Components/Samples/Table/TablesEdit.razor

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@page "/table/edit"
1+
@page "/table/edit"
22
@inject IStringLocalizer<NavMenu> NavMenuLocalizer
33
@inject IStringLocalizer<TablesEdit> Localizer
44
@inject IStringLocalizer<Foo> LocalizerFoo
@@ -57,13 +57,21 @@
5757
ShowToolbar="true" ShowExtendButtons="true" ShowSkeleton="true">
5858
<TableColumns>
5959
<TableColumn @bind-Field="@context.Complete" Align="Alignment.Center"></TableColumn>
60+
<TableColumn @bind-Field="@context.Education">
61+
<EditTemplate Context="v">
62+
<div class="col-12 col-sm-6">
63+
</div>
64+
<div class="col-12 col-sm-6">
65+
<Select @bind-Value="@v.Education"></Select>
66+
</div>
67+
<div class="col-12 col-sm-6">
68+
<Display Value="@GetEducationDesc(v.Education)" ShowLabel="true" DisplayText="@Localizer["TablesEditTemplateDisplayLabel"]"></Display>
69+
</div>
70+
</EditTemplate>
71+
</TableColumn>
6072
<TableColumn @bind-Field="@context.Count" Align="Alignment.Right"></TableColumn>
6173
<TableColumn @bind-Field="@context.Hobby"></TableColumn>
6274
</TableColumns>
63-
<EditTemplate>
64-
<!--本例中使用了联动功能,必须封装成组件-->
65-
<DemoTableEditTemplate Model="context" />
66-
</EditTemplate>
6775
<EditFooterTemplate>
6876
<Button Text="Popup" Color="Color.Danger" Icon="fa-regular fa-comment-dots" OnClick="() => OnClick(context)"></Button>
6977
<DialogCloseButton></DialogCloseButton>

src/BootstrapBlazor.Server/Components/Samples/Table/TablesEdit.razor.cs

Lines changed: 8 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([email protected]) Website: https://www.blazor.zone
@@ -40,6 +40,13 @@ public partial class TablesEdit
4040

4141
private bool _useGroup = false;
4242

43+
private string? GetEducationDesc(EnumEducation? item) => item switch
44+
{
45+
EnumEducation.Primary => Localizer["TablesEditTemplateDisplayDetail1"],
46+
EnumEducation.Middle => Localizer["TablesEditTemplateDisplayDetail2"],
47+
_ => ""
48+
};
49+
4350
/// <summary>
4451
/// <inheritdoc/>
4552
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,8 +5184,8 @@
51845184
"TablesEditItemsIntro": "设置 <code>Items</code> 作为数据源,无需设置 <code>OnSaveAsync</code> <code>OnDeleteAsync</code> 回调委托使用内置处理逻辑进行更新与删除功能",
51855185
"TablesEditItemsDescription": "设置 <code>Items</code> 作为数据源,必须使用双向绑定 <code>@bind-Items</code>, 同时 <code>TItem</code> 泛型约束的类实例,如本例中的 <code>Foo</code> ,需要正确配置 <code>[Key]</code> 标签,否则内置编辑功能无法正常工作;通过设置 <code>IsGroupExtendButtons</code> 参数控制行内功能按钮是否组合显示 默认 <code>true</code>",
51865186
"TablesEditTemplateTitle": "具有单表维护功能的表格",
5187-
"TablesEditTemplateIntro": "通过设置 <code>EditTemplate</code> 自定义编辑弹窗,如果属性需要联动时必须像本例这样封装成一个独立的组件再放置到模板中",
5188-
"TablesEditTemplateDescription": "本例中设置 <code>Count</code> 右侧对齐,<code>Complete</code> 列设置为居中对齐,布尔类型列自动渲染成 <code>Switch</code> 组件,点击 <code>学历</code> 下拉框时右侧只读组件描述信息联动更新,<b>此功能必须封装成独立组件</b>",
5187+
"TablesEditTemplateIntro": "通过设置 <code>EditTemplate</code> 自定义编辑弹窗,<code>10.0.1</code> 版本后支持联动功能",
5188+
"TablesEditTemplateDescription": "本例中设置 <code>Count</code> 右侧对齐,<code>Complete</code> 列设置为居中对齐,布尔类型列自动渲染成 <code>Switch</code> 组件,点击 <code>学历</code> 下拉框时右侧只读组件描述信息联动更新",
51895189
"TablesEditTemplateDisplayLabel": "详细信息",
51905190
"TablesEditTemplateDisplayDetail1": "小学六年",
51915191
"TablesEditTemplateDisplayDetail2": "初中三年",

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.0.0</Version>
4+
<Version>10.0.1</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Dialog/EditDialog.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@namespace BootstrapBlazor.Components
1+
@namespace BootstrapBlazor.Components
22
@typeparam TModel
33
@inherits DialogBase<TModel>
44
@attribute [BootstrapModuleAutoLoader("Dialog/EditDialog.razor.js", AutoInvokeInit = false, AutoInvokeDispose = false)]
@@ -16,7 +16,9 @@
1616
}
1717
else
1818
{
19-
<EditorForm TModel="TModel" Items="Items" ItemChangedType="ItemChangedType" ItemsPerRow="ItemsPerRow" RowType="RowType" LabelAlign="LabelAlign" ShowLabel="ShowLabel" ShowUnsetGroupItemsOnTop="ShowUnsetGroupItemsOnTop">
19+
<EditorForm TModel="TModel" Items="Items" ItemChangedType="ItemChangedType" ItemsPerRow="ItemsPerRow" RowType="RowType"
20+
LabelAlign="LabelAlign" ShowLabel="ShowLabel" ShowUnsetGroupItemsOnTop="ShowUnsetGroupItemsOnTop"
21+
IsRenderWhenValueChanged="true">
2022
<Buttons>
2123
@RenderFooter
2224
</Buttons>

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

Lines changed: 34 additions & 2 deletions
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([email protected]) Website: https://www.blazor.zone
@@ -12,7 +12,7 @@ namespace BootstrapBlazor.Components;
1212
/// 编辑表单基类
1313
/// </summary>
1414
[CascadingTypeParameter(nameof(TModel))]
15-
public partial class EditorForm<TModel> : IShowLabel
15+
public partial class EditorForm<TModel> : IShowLabel, IDisposable
1616
{
1717
private string? ClassString => CssBuilder.Default("bb-editor")
1818
.AddClassFromAttributes(AdditionalAttributes)
@@ -159,6 +159,12 @@ public partial class EditorForm<TModel> : IShowLabel
159159
[NotNull]
160160
public string? PlaceHolderText { get; set; }
161161

162+
/// <summary>
163+
/// 获得/设置 当值变化时是否重新渲染组件 默认 false
164+
/// </summary>
165+
[Parameter]
166+
public bool IsRenderWhenValueChanged { get; set; }
167+
162168
/// <summary>
163169
/// 获得/设置 级联上下文 EditContext 实例 内置于 EditForm 或者 ValidateForm 时有值
164170
/// </summary>
@@ -209,6 +215,11 @@ protected override void OnInitialized()
209215
{
210216
base.OnInitialized();
211217

218+
if (CascadedEditContext != null)
219+
{
220+
CascadedEditContext.OnFieldChanged += NotifyValueChanged;
221+
}
222+
212223
if (CascadedEditContext != null && IsSearch is not true)
213224
{
214225
var message = Localizer["ModelInvalidOperationExceptionMessage", nameof(EditorForm<TModel>)];
@@ -298,4 +309,25 @@ private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder =>
298309
private RenderFragment<object>? GetRenderTemplate(IEditorItem item) => IsSearch.Value && item is ITableColumn col
299310
? col.SearchTemplate
300311
: item.EditTemplate;
312+
313+
private void NotifyValueChanged(object? sender, FieldChangedEventArgs args)
314+
{
315+
// perf: 判断是否在编辑状态避免不必要的重绘
316+
if (IsRenderWhenValueChanged)
317+
{
318+
StateHasChanged();
319+
}
320+
}
321+
322+
/// <summary>
323+
/// <inheritdoc/>
324+
/// </summary>
325+
public void Dispose()
326+
{
327+
if (CascadedEditContext != null)
328+
{
329+
CascadedEditContext.OnFieldChanged -= NotifyValueChanged;
330+
}
331+
GC.SuppressFinalize(this);
332+
}
301333
}

src/BootstrapBlazor/Components/Validate/ValidateBase.cs

Lines changed: 1 addition & 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([email protected]) Website: https://www.blazor.zone

0 commit comments

Comments
 (0)