Skip to content

Commit a65e246

Browse files
authored
feat(ToggleButton): add ToggleButton component (dotnetcore#6545)
* feat: 增加 ToggleButton 组件 * doc: 增加示例 * chore: 更新 Sortable 依赖 * test: 更新 Table 单元测试 * test: 增加单元测试提高覆盖率 * chore: 更新项目依赖 * refactor: 重构代码 * test: 增加单元测试 * doc: 更新示例 * chore: bump version 9.9.1-beta05
1 parent 69ce39e commit a65e246

File tree

8 files changed

+177
-3
lines changed

8 files changed

+177
-3
lines changed

src/BootstrapBlazor.Server/Components/Samples/Buttons.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@
152152
</div>
153153
</DemoBlock>
154154

155+
<DemoBlock Title="@Localizer["ToggleButton"]" Introduction="@Localizer["ToggleIntroduction"]" Name="Toggle">
156+
<ToggleButton Text="Toggle" Color="Color.Danger"></ToggleButton>
157+
</DemoBlock>
158+
155159
<AttributeTable Items="@GetAttributes()" />
156160

157161
<EventTable Items="@GetEvents()" />

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,9 @@
22112211
"TooltipText": "Button",
22122212
"TooltipTitle": "Tooltip",
22132213
"TooltipIntro": "Set <code>TooltipText</code> <code>TooltipPlacement</code> <code>TooltipTrigger</code> shortcut button prompt bar information, position, and triggering method. For more functions, please use the <code>Tooltip</code> component to implement. In this example, the second button is in the <b>disabled</b> state, and the prompt bar is still available",
2214-
"TooltipDisabledText": "Disabled"
2214+
"TooltipDisabledText": "Disabled",
2215+
"ToggleButton": "Toggle",
2216+
"ToggleIntroduction": "Get the current button <code>Toggle</code> state by setting the <code>IsActiveChanged</code> or <code>OnToggleAsync</code> callback method"
22152217
},
22162218
"BootstrapBlazor.Server.Components.Samples.PulseButtons": {
22172219
"Block1Title": "Basic usage",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,9 @@
22112211
"TooltipText": "按钮",
22122212
"TooltipTitle": "按钮提示栏",
22132213
"TooltipIntro": "通过设置 <code>TooltipText</code> <code>TooltipPlacement</code> <code>TooltipTrigger</code> 快捷设置按钮提示栏信息、位置、触发方式,更多功能请使用 <code>Tooltip</code> 组件实现,本例中第二个按钮是 <b>禁用</b> 状态,提示栏仍然可用",
2214-
"TooltipDisabledText": "禁用按钮"
2214+
"TooltipDisabledText": "禁用按钮",
2215+
"ToggleButton": "状态切换按钮",
2216+
"ToggleIntroduction": "通过设置 <code>IsActiveChanged</code> 或者 <code>OnToggleAsync</code> 回调方法获得当前按钮 <code>Toggle</code> 状态"
22152217
},
22162218
"BootstrapBlazor.Server.Components.Samples.PulseButtons": {
22172219
"Block1Title": "基础用法",

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>9.9.1-beta04</Version>
4+
<Version>9.9.1-beta05</Version>
55
</PropertyGroup>
66

77
<ItemGroup>
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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
IsActive = !IsActive;
56+
if (OnClickWithoutRender != null)
57+
{
58+
if (!IsAsync)
59+
{
60+
IsNotRender = true;
61+
}
62+
63+
await OnClickWithoutRender();
64+
}
65+
66+
if (OnClick.HasDelegate)
67+
{
68+
await OnClick.InvokeAsync();
69+
}
70+
71+
if (IsActiveChanged.HasDelegate)
72+
{
73+
await IsActiveChanged.InvokeAsync(IsActive);
74+
}
75+
76+
if (OnToggleAsync != null)
77+
{
78+
await OnToggleAsync(IsActive);
79+
}
80+
}
81+
}

test/UnitTest/Components/ButtonTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,45 @@ public void ShareButton_Ok()
452452
Assert.Equal("test-share-title", cut.Instance.ShareContext?.Title);
453453
Assert.Equal("www.blazor.zone", cut.Instance.ShareContext?.Url);
454454
}
455+
456+
[Fact]
457+
public async Task ToogleButton_Ok()
458+
{
459+
var active = false;
460+
var bindActive = false;
461+
var clickWithoutRender = false;
462+
var clicked = false;
463+
var tcs = new TaskCompletionSource();
464+
var cut = Context.RenderComponent<ToggleButton>(pb =>
465+
{
466+
pb.Add(a => a.IsActive, false);
467+
pb.Add(a => a.IsActiveChanged, EventCallback.Factory.Create<bool>(this, b =>
468+
{
469+
active = b;
470+
bindActive = true;
471+
}));
472+
pb.Add(a => a.OnClickWithoutRender, () =>
473+
{
474+
clickWithoutRender = true;
475+
return Task.CompletedTask;
476+
});
477+
pb.Add(a => a.OnClick, () =>
478+
{
479+
clicked = true;
480+
return Task.CompletedTask;
481+
});
482+
pb.Add(a => a.OnToggleAsync, async isActive =>
483+
{
484+
await Task.Delay(10);
485+
active = isActive;
486+
tcs.TrySetResult();
487+
});
488+
});
489+
var button = cut.Find("button");
490+
await cut.InvokeAsync(() => button.Click());
491+
await tcs.Task;
492+
Assert.True(active);
493+
Assert.True(clickWithoutRender);
494+
Assert.True(clicked);
495+
}
455496
}

test/UnitTest/Components/TableTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8720,6 +8720,27 @@ public void Modify_Ok()
87208720
Assert.True(ProhibitDelete(cut.Instance));
87218721
}
87228722

8723+
[Fact]
8724+
public void Table_Sortable()
8725+
{
8726+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
8727+
var cut = Context.RenderComponent<Table<Foo>>(pb =>
8728+
{
8729+
pb.AddCascadingValue<ISortableList>(new SortableList());
8730+
pb.Add(a => a.TableColumns, foo => builder =>
8731+
{
8732+
builder.OpenComponent<TableColumn<Foo, string>>(0);
8733+
builder.AddAttribute(1, "Field", "Name");
8734+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
8735+
builder.CloseComponent();
8736+
});
8737+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
8738+
pb.Add(a => a.Items, Foo.GenerateFoo(localizer));
8739+
});
8740+
}
8741+
8742+
class SortableList : ISortableList { }
8743+
87238744
static bool ProhibitEdit(Table<Foo> @this)
87248745
{
87258746
var ret = false;

0 commit comments

Comments
 (0)