Skip to content

Commit 2bd87ca

Browse files
committed
feat: 增加 ToggleButton 组件
1 parent 175fd46 commit 2bd87ca

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@namespace BootstrapBlazor.Components
2+
@inherits ButtonBase
3+
4+
<button @attributes="@AdditionalAttributes" @onclick="@OnClickButton" id="@Id" class="@ToggleClassName" disabled="@Disabled"
5+
role="button" data-bs-placement="@PlacementString" data-bs-trigger="@TriggerString"
6+
aria-disabled="@DisabledString" tabindex="@Tab" data-bs-toggle="button"
7+
@onclick:stopPropagation="@StopPropagation">
8+
@if(IsAsyncLoading)
9+
{
10+
<i class="@LoadingIcon"></i>
11+
}
12+
else if (!string.IsNullOrEmpty(Icon))
13+
{
14+
<i class="@Icon"></i>
15+
}
16+
@if (!string.IsNullOrEmpty(Text))
17+
{
18+
<span>@Text</span>
19+
}
20+
<CascadingValue Value="this" IsFixed="true">
21+
@ChildContent
22+
</CascadingValue>
23+
</button>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// Toggle Button 按钮组件
10+
/// </summary>
11+
public partial class ToggleButton
12+
{
13+
/// <summary>
14+
/// 获得/设置 状态切换回调方法
15+
/// </summary>
16+
[Parameter]
17+
public Func<bool, Task>? OnToggleAsync { get; set; }
18+
19+
/// <summary>
20+
/// 获得/设置 当前状态是否为激活状态 默认 false
21+
/// </summary>
22+
[Parameter]
23+
public bool IsActive { get; set; }
24+
25+
/// <summary>
26+
/// 获得/设置 激活状态回调方法
27+
/// </summary>
28+
[Parameter]
29+
public EventCallback<bool> IsActiveChanged { get; set; }
30+
31+
private string? ToggleClassName => CssBuilder.Default(ClassName)
32+
.AddClass("active", IsActive)
33+
.Build();
34+
35+
private async Task OnClickButton()
36+
{
37+
if (IsAsync)
38+
{
39+
IsAsyncLoading = true;
40+
IsDisabled = true;
41+
}
42+
43+
await HandlerClick();
44+
45+
// 恢复按钮
46+
if (IsAsync)
47+
{
48+
IsDisabled = IsKeepDisabled;
49+
IsAsyncLoading = false;
50+
}
51+
}
52+
53+
private async Task HandlerClick()
54+
{
55+
if (OnClickWithoutRender != null)
56+
{
57+
if (!IsAsync)
58+
{
59+
IsNotRender = true;
60+
}
61+
62+
await OnClickWithoutRender();
63+
}
64+
65+
if (OnClick.HasDelegate)
66+
{
67+
await OnClick.InvokeAsync();
68+
}
69+
70+
IsActive = !IsActive;
71+
if (IsActiveChanged.HasDelegate)
72+
{
73+
await IsActiveChanged.InvokeAsync(IsActive);
74+
}
75+
if (OnToggleAsync != null)
76+
{
77+
await OnToggleAsync(IsActive);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)