Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Dialog/Dialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@inherits BootstrapComponentBase

<Modal @ref="_modal" IsBackdrop="_isBackdrop" IsKeyboard="@_isKeyboard" IsFade="_isFade"
OnShownAsync="_onShownAsync" OnCloseAsync="_onCloseAsync"
OnShownAsync="_onShownAsync" OnClosingAsync="_onClosingAsync" OnCloseAsync="_onCloseAsync"
class="@ClassString">
@for (var index = 0; index < DialogParameters.Keys.Count; index++)
{
Expand Down
3 changes: 3 additions & 0 deletions src/BootstrapBlazor/Components/Dialog/Dialog.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public partial class Dialog : IDisposable
private Modal? _modal = null;
private Func<Task>? _onShownAsync = null;
private Func<Task>? _onCloseAsync = null;
private Func<Task<bool>>? _onClosingAsync = null;

private readonly Dictionary<Dictionary<string, object>, (bool IsKeyboard, bool IsBackdrop, Func<Task>? OnCloseCallback)> DialogParameters = [];
private Dictionary<string, object>? _currentParameter;
Expand Down Expand Up @@ -87,6 +88,8 @@ private async Task Show(DialogOption option)
}
};

_onClosingAsync = option.OnClosingAsync;

Comment thread
ArgoZhang marked this conversation as resolved.
_isKeyboard = option.IsKeyboard;
_isBackdrop = option.IsBackdrop;
_isFade = option.IsFade;
Expand Down
10 changes: 7 additions & 3 deletions src/BootstrapBlazor/Components/Dialog/DialogOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BootstrapBlazor.Components;
/// <para lang="zh">Dialog 对话框组件</para>
/// <para lang="en">Dialog component</para>
/// </summary>
public class DialogOption
public class DialogOption : ICloseable
{
/// <summary>
/// <para lang="zh">获得/设置 关联的 Modal 实例</para>
Expand Down Expand Up @@ -192,11 +192,15 @@ public class DialogOption
public string? CloseButtonText { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 关闭对话框回调方法</para>
/// <para lang="en">Gets or sets the callback method for closing the dialog</para>
/// <inheritdoc cref="ICloseable.OnCloseAsync"/>
/// </summary>
public Func<Task>? OnCloseAsync { get; set; }

/// <summary>
/// <inheritdoc cref="ICloseable.OnClosingAsync"/>
/// </summary>
public Func<Task<bool>>? OnClosingAsync { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 是否自动关闭对话框(保存成功后) 默认值为 true</para>
/// <para lang="en">Gets or sets whether to automatically close the dialog after saving successfully, default is true</para>
Expand Down
25 changes: 25 additions & 0 deletions src/BootstrapBlazor/Components/Modal/IClosable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone

namespace BootstrapBlazor.Components;

/// <summary>
/// <para lang="zh">可关闭接口</para>
/// <para lang="en">the closable interface</para>
/// </summary>
public interface ICloseable
{
Comment thread
ArgoZhang marked this conversation as resolved.
/// <summary>
/// <para lang="zh">获得/设置 弹出窗口关闭时的回调委托</para>
/// <para lang="en">Gets or sets the callback delegate when the popup is closed</para>
/// </summary>
Func<Task>? OnCloseAsync { get; set; }

/// <summary>
/// <para lang="zh">关闭之前回调方法 返回 true 时关闭弹窗 返回 false 时阻止关闭弹窗</para>
/// <para lang="en">Callback Method Before Closing. Return true to close, false to prevent closing</para>
/// </summary>
Func<Task<bool>>? OnClosingAsync { get; set; }
}
8 changes: 3 additions & 5 deletions src/BootstrapBlazor/Components/Modal/Modal.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BootstrapBlazor.Components;
/// <para lang="zh">Modal 组件</para>
/// <para lang="en">Modal component</para>
/// </summary>
public partial class Modal
public partial class Modal : ICloseable
{
[Inject]
[NotNull]
Expand Down Expand Up @@ -77,15 +77,13 @@ public partial class Modal
public Func<Task>? OnShownAsync { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 弹出窗口关闭时的回调委托</para>
/// <para lang="en">Gets or sets the callback delegate when the popup is closed</para>
/// <inheritdoc cref="ICloseable.OnCloseAsync"/>
/// </summary>
[Parameter]
public Func<Task>? OnCloseAsync { get; set; }

/// <summary>
/// <para lang="zh">关闭之前回调方法 返回 true 时关闭弹窗 返回 false 时阻止关闭弹窗</para>
/// <para lang="en">Callback Method Before Closing. Return true to close, false to prevent closing</para>
/// <inheritdoc cref="ICloseable.OnClosingAsync"/>
/// </summary>
[Parameter]
public Func<Task<bool>>? OnClosingAsync { get; set; }
Expand Down
16 changes: 16 additions & 0 deletions test/UnitTest/Components/DialogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,22 @@ await cut.InvokeAsync(() => dialog.ShowCloseDialog<MockValidateFormDialog>("Clos
await cut.InvokeAsync(() => dialog.ShowExceptionDialog(null, new Exception("Test")));
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
#endregion

// OnClosing
var closing = false;
await cut.InvokeAsync(() => dialog.Show(new DialogOption()
{
OnClosingAsync = () =>
{
closing = true;
return Task.FromResult(false);
}
}));

// 由于返回 false 所以关窗方法被阻止
await cut.InvokeAsync(() => modal.Instance.Close());
Assert.True(closing);
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
Comment thread
ArgoZhang marked this conversation as resolved.
}

private class MockValidateFormDialog : ComponentBase
Expand Down
6 changes: 1 addition & 5 deletions test/UnitTest/Core/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ public TestBase()
{
Context = new BunitContext();
Context.JSInterop.Mode = JSRuntimeMode.Loose;

Context.Services.AddMockEnvironment();
}

public void Dispose()
{
#pragma warning disable CA2012
// 由于 bUnit 2.0 继承了 IAsyncDisposable 接口,因此此处调用 DisposeAsync 方法
Context.DisposeAsync();
#pragma warning restore CA2012
Context.DisposeAsync().AsTask().Wait();
Comment thread
ArgoZhang marked this conversation as resolved.
GC.SuppressFinalize(this);
}
}
19 changes: 0 additions & 19 deletions test/UnitTest/Extensions/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Microsoft.Extensions.DependencyInjection;
Expand All @@ -29,12 +27,6 @@ public static IServiceCollection AddConfiguration(this IServiceCollection servic
return services;
}

public static IServiceCollection AddMockEnvironment(this IServiceCollection services)
{
services.AddSingleton<IHostEnvironment, MockEnvironment>();
return services;
}

public static ILoggingBuilder AddMockLoggerProvider(this ILoggingBuilder builder)
{
return builder.AddProvider(new TestLoggerProvider());
Expand All @@ -61,15 +53,4 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

}
}

class MockEnvironment : IHostEnvironment
{
public string EnvironmentName { get; set; } = "Development";

public string ApplicationName { get; set; } = "Test";

public string ContentRootPath { get; set; } = "UnitTest";

public IFileProvider ContentRootFileProvider { get; set; } = null!;
}
}
Loading