Skip to content

Commit

Permalink
refactor: refine EditDialog reuse DialogCloseButton (#575)
Browse files Browse the repository at this point in the history
* refactor: 增加 EditorRequired 标签

* refactor: 移除 OnCloseAsync 级联参数

* refactor: 使用 DialogCloseButton 精简代码

* refactor: 精简代码

* fix: 修复 OnCloseAsync 回调丢失问题
  • Loading branch information
ArgoZhang authored Feb 26, 2023
1 parent b7726ae commit 6a4aa70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
23 changes: 7 additions & 16 deletions src/BootstrapBlazor/Components/Dialog/EditDialog.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public partial class EditDialog<TModel>
/// 获得/设置 保存回调委托
/// </summary>
[Parameter]
[NotNull]
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
public Func<EditContext, Task>? OnSaveAsync { get; set; }

/// <summary>
Expand Down Expand Up @@ -85,11 +87,6 @@ protected override void OnParametersSet()
SaveButtonText ??= Localizer[nameof(SaveButtonText)];
}

private async Task OnClickClose()
{
if (OnCloseAsync != null) await OnCloseAsync();
}

private async Task OnValidSubmitAsync(EditContext context)
{
if (OnSaveAsync != null)
Expand Down Expand Up @@ -117,21 +114,15 @@ public async ValueTask ToggleLoading(bool state)
{
if (FooterTemplate != null)
{
builder.OpenComponent<CascadingValue<Func<Task>?>>(0);
builder.AddAttribute(1, nameof(CascadingValue<Func<Task>?>.Value), OnCloseAsync);
builder.AddAttribute(2, nameof(CascadingValue<Func<Task>?>.IsFixed), true);
builder.AddAttribute(3, nameof(CascadingValue<Func<Task>?>.ChildContent), FooterTemplate(Model));
builder.CloseComponent();
builder.AddContent(1, FooterTemplate(Model));
}
else
{
if (!IsTracking)
{
builder.OpenComponent<Button>(20);
builder.AddAttribute(21, nameof(Button.Color), Color.Secondary);
builder.AddAttribute(22, nameof(Button.Icon), "fa-solid fa-xmark");
builder.AddAttribute(23, nameof(Button.Text), CloseButtonText);
builder.AddAttribute(24, nameof(Button.OnClickWithoutRender), OnClickClose);
builder.OpenComponent<DialogCloseButton>(20);
builder.AddAttribute(21, nameof(Button.Text), CloseButtonText);
builder.AddAttribute(22, nameof(Button.OnClickWithoutRender), OnCloseAsync);
builder.CloseComponent();
}
builder.OpenComponent<Button>(30);
Expand Down
5 changes: 1 addition & 4 deletions src/BootstrapBlazor/Extensions/DialogServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ public static async Task ShowEditDialog<TModel>(this DialogService service, Edit
[nameof(EditDialog<TModel>.ShowLoading)] = option.ShowLoading,
[nameof(EditDialog<TModel>.ShowLabel)] = option.ShowLabel,
[nameof(EditDialog<TModel>.Items)] = option.Items ?? Utility.GenerateColumns<TModel>(item => item.Editable),
[nameof(EditDialog<TModel>.OnCloseAsync)] = new Func<Task>(async () =>
{
await option.Dialog.Close();
}),
[nameof(EditDialog<TModel>.OnCloseAsync)] = option.OnCloseAsync,
[nameof(EditDialog<TModel>.OnSaveAsync)] = new Func<EditContext, Task>(async context =>
{
if (option.OnEditAsync != null)
Expand Down

0 comments on commit 6a4aa70

Please sign in to comment.