Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 0 additions & 20 deletions src/BootstrapBlazor/Components/Button/Button.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,4 @@ protected virtual async Task OnClickButton()
/// </summary>
/// <returns></returns>
public ValueTask FocusAsync() => ButtonElement.FocusAsync();

/// <summary>
/// 处理点击方法
/// </summary>
/// <returns></returns>
protected virtual async Task HandlerClick()
{
if (OnClickWithoutRender != null)
{
if (!IsAsync)
{
IsNotRender = true;
}
await OnClickWithoutRender();
}
if (OnClick.HasDelegate)
{
await OnClick.InvokeAsync();
}
}
}
20 changes: 20 additions & 0 deletions src/BootstrapBlazor/Components/Button/ButtonBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,26 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
}
}

/// <summary>
/// 处理点击方法
/// </summary>
/// <returns></returns>
protected virtual async Task HandlerClick()
{
if (OnClickWithoutRender != null)
{
if (!IsAsync)
{
IsNotRender = true;
}
await OnClickWithoutRender();
}
if (OnClick.HasDelegate)
{
await OnClick.InvokeAsync();
}
}

/// <summary>
/// 设置按钮是否可用状态
/// </summary>
Expand Down
16 changes: 10 additions & 6 deletions src/BootstrapBlazor/Components/Button/LinkButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;
using System;

namespace BootstrapBlazor.Components;

/// <summary>
/// LinkButton 组件
/// </summary>
public sealed class LinkButton : ButtonBase
public class LinkButton : ButtonBase
{
/// <summary>
/// 获得/设置 Url 默认为 #
Expand Down Expand Up @@ -120,14 +119,19 @@ private RenderFragment AddImage() => builder =>

private async Task OnClickButton()
{
if (OnClickWithoutRender != null)
if (IsAsync)
{
await OnClickWithoutRender();
IsAsyncLoading = true;
IsDisabled = true;
}

if (OnClick.HasDelegate)
await HandlerClick();

// 恢复按钮
if (IsAsync)
{
await OnClick.InvokeAsync();
IsDisabled = IsKeepDisabled;
IsAsyncLoading = false;
}
}
}
18 changes: 12 additions & 6 deletions test/UnitTest/Components/LinkButtonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,22 @@ public void ChildContent_Ok()
}

[Fact]
public void OnClick_Ok()
public async Task OnClick_Ok()
{
var click = false;
var cut = Context.RenderComponent<LinkButton>(builder => builder.Add(s => s.OnClick, () => click = true));

cut.InvokeAsync(() =>
var cut = Context.RenderComponent<LinkButton>(pb =>
{
cut.Find("a").Click();
Assert.True(click);
pb.Add(a => a.IsAsync, true);
pb.Add(s => s.OnClick, async () =>
{
click = true;
await Task.Yield();
});
});

var link = cut.Find("a");
await cut.InvokeAsync(() => link.Click());
Assert.True(click);
}

[Fact]
Expand Down
Loading