Skip to content

Commit 1c4bc58

Browse files
ArgoZhangice6
andauthored
feat(Dropdown): add IsAsync parameter (#5324)
* refactor: update html element tag name * test: 更新单元测试 * doc: 更新参数注释 * feat(Dropdown): add IsAsync parameter * refactor: 精简代码 提高代码可读性 * refactor: 代码重构 * feat: 实现 IsAsync 逻辑 * doc: 增加 IsAsync 文档 * feat: 分离按钮支持默认颜色 * feat: 异步图标显示逻辑 * refactor: 增加 span 包裹元素 * doc: 增加文档 * feat: 增加图标赋值逻辑 * refactor: 使用父类服务 * test: 增加单元测试 * chore: bump version 9.3.1-beta10 Co-Authored-By: ice6 <[email protected]>
1 parent 0f7be01 commit 1c4bc58

File tree

11 files changed

+175
-67
lines changed

11 files changed

+175
-67
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
</div>
4949
</DemoBlock>
5050

51+
<DemoBlock Title="@Localizer["IsAsyncTitle"]" Introduction='@Localizer["IsAsyncIntro"]' Name="IsAsync">
52+
<Dropdown TValue="string" Items="Items" ShowSplit="true" IsAsync="true" OnClickWithoutRender="OnIsAsyncClick"></Dropdown>
53+
</DemoBlock>
54+
5155
<DemoBlock Title="@Localizer["SizeTitle"]" Introduction="@Localizer["SizeIntro"]" Name="Size">
5256
<div class="row g-3">
5357
<div class="col-6 col-sm-4 col-md-3 col-xl-auto">

src/BootstrapBlazor.Server/Components/Samples/Dropdowns.razor.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace BootstrapBlazor.Server.Components.Samples;
1010
/// </summary>
1111
public sealed partial class Dropdowns
1212
{
13+
[Inject, NotNull]
14+
private ToastService? ToastService { get; set; }
15+
1316
[NotNull]
1417
private ConsoleLogger? Logger { get; set; }
1518

@@ -142,6 +145,15 @@ private async Task OnCascadeBindSelectClick(SelectedItem item)
142145
StateHasChanged();
143146
}
144147

148+
private async Task OnIsAsyncClick()
149+
{
150+
// 模拟异步延时
151+
await Task.Delay(1000);
152+
153+
// 提示任务完成
154+
await ToastService.Success("Dropdown IsAsync", "Job done!");
155+
}
156+
145157
/// <summary>
146158
/// GetAttributes
147159
/// </summary>
@@ -220,6 +232,14 @@ private AttributeItem[] GetAttributes() =>
220232
DefaultValue = " false "
221233
},
222234
new()
235+
{
236+
Name = "IsAsync",
237+
Description = Localizer["AttributeIsAsync"],
238+
Type = "boolean",
239+
ValueList = " — ",
240+
DefaultValue = "false"
241+
},
242+
new()
223243
{
224244
Name = "Size",
225245
Description = Localizer["AttributeSize"],

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,8 @@
17631763
"SplitIntro": "You can create a split drop-down menu with tags similar to a single button drop-down menu, and add ShowSplit='true' when you use a split component. Insert this symbol as a drop-down The options are handled at appropriate intervals (distance).",
17641764
"SizeTitle": "Size definition",
17651765
"SizeIntro": "The drop-down menu has a variety of size specifications to choose from <code>Size</code> attributes, including preset and split button drop-down menus.",
1766+
"IsAsyncTitle": "Asynchronous request button",
1767+
"IsAsyncIntro": "By setting whether the <code>isAsync</code> property button is <b>asynchronous request button by setting whether the is</b> <code>false</code> by default",
17661768
"DirectionTitle": "Expanding direction",
17671769
"DirectionIntro": "Add the style of <code>Direction='Direction.Dropup'</code> to make the drop-down menu expand upward.",
17681770
"AlignmentTitle": "Menu alignment",
@@ -1788,6 +1790,7 @@
17881790
"AttributeMenuItem": "Menu item rendering label",
17891791
"AttributeResponsive": "Menu alignment",
17901792
"AttributeShowSplit": "Split button drop-down menu",
1793+
"AttributeIsAsync": "whether it is an asynchronous button",
17911794
"AttributeSize": "Size",
17921795
"AttributeTagName": "Label",
17931796
"AttributeButtonTemplate": "The template of button",
@@ -2226,7 +2229,7 @@
22262229
"Block8Title": "Secondary encapsulation button",
22272230
"Block8Intro": "The button display text is set by setting the <code>Text</code> property of the <code>winButton</code> component, and the click button is the text on the right that shows the clicked button",
22282231
"Block9Title": "Asynchronous request button",
2229-
"Block9Intro": "By setting whether the <code>isAsync</code> property button is <b>asynchronous request button by setting whether the is</b> <code>false</code> by default",
2232+
"Block9Intro": "By setting whether the <code>isAsync</code> property button is <b>asynchronous request button by setting whether the is</b> <code>false</code> by default. <b>Note</b> This will only take effect when <code>ShowSplit=\"true\"</code> is set",
22302233
"ButtonAsyncDescription": "When the button is an asynchronous request button, the button is changed to disabled, and the <code>loading</code> small icon is displayed, returning to normal after the asynchronous request ends, in this case, after clicking the <b>asynchronous button</b>, the request load animation is displayed and returns to normal after 5 seconds",
22312234
"EventDesc1": "This event is triggered when the button is clicked",
22322235
"EventDesc2": "This event is triggered when the button is clicked and the current component is not refreshed for performance improvement",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,8 @@
17611761
"ColorIntro": "提供各种颜色的警告信息框 引用 <code>Color='Color.Primary'</code> 等颜色及样式类来定义下拉菜单的外在表现",
17621762
"SplitTitle": "分裂式按钮下拉菜单",
17631763
"SplitIntro": "可用与单个按钮下拉菜单近似的标记创建分裂式下拉菜单,添加 <code>ShowSplit='true'</code> 插入此符号为下拉选项作适当的间隔(距)处理。",
1764+
"IsAsyncTitle": "异步按钮",
1765+
"IsAsyncIntro": "通过设置 <code>IsAsync</code> 属性按钮是否为 <b>异步请求按钮</b>,默认为 <code>false</code>,<b>注意</b> 需要设置 <code>ShowSplit=\"true\"</code> 时才生效",
17641766
"SizeTitle": "尺寸大小定义",
17651767
"SizeIntro": "下拉菜单有各种大小规格可以选用 <code>Size</code> 属性,包括预设及分裂式按钮下拉菜单。",
17661768
"DirectionTitle": "展开方向",
@@ -1788,6 +1790,7 @@
17881790
"AttributeMenuItem": "菜单项渲染标签",
17891791
"AttributeResponsive": "菜单对齐",
17901792
"AttributeShowSplit": "分裂式按钮下拉菜单",
1793+
"AttributeIsAsync": "是否为异步按钮",
17911794
"AttributeSize": "尺寸",
17921795
"AttributeTagName": "标签",
17931796
"AttributeButtonTemplate": "按钮模板",

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.3.1-beta09</Version>
4+
<Version>9.3.1-beta10</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Button/Button.razor.cs

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,11 @@ public partial class Button : ButtonBase
1818
[Parameter]
1919
public bool IsAutoFocus { get; set; }
2020

21-
/// <summary>
22-
/// 按钮点击回调方法,内置支持 IsAsync 开关
23-
/// </summary>
24-
protected EventCallback<MouseEventArgs> OnClickButton { get; set; }
25-
2621
/// <summary>
2722
/// 获得/设置 html button 实例
2823
/// </summary>
2924
protected ElementReference ButtonElement { get; set; }
3025

31-
/// <summary>
32-
/// OnInitialized 方法
33-
/// </summary>
34-
protected override void OnInitialized()
35-
{
36-
base.OnInitialized();
37-
38-
SetClickHandler();
39-
}
40-
4126
/// <summary>
4227
/// OnAfterRenderAsync 方法
4328
/// </summary>
@@ -57,36 +42,33 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5742
}
5843

5944
/// <summary>
60-
/// 设置 OnClickButton 方法
45+
/// OnClickButton 方法
6146
/// </summary>
62-
protected virtual void SetClickHandler()
47+
protected virtual async Task OnClickButton()
6348
{
64-
OnClickButton = EventCallback.Factory.Create<MouseEventArgs>(this, async () =>
49+
if (IsAsync && ButtonType == ButtonType.Button)
6550
{
66-
if (IsAsync && ButtonType == ButtonType.Button)
67-
{
68-
IsAsyncLoading = true;
69-
ButtonIcon = LoadingIcon;
70-
IsDisabled = true;
71-
}
51+
IsAsyncLoading = true;
52+
ButtonIcon = LoadingIcon;
53+
IsDisabled = true;
54+
}
7255

73-
if (IsAsync)
74-
{
75-
await Task.Run(() => InvokeAsync(HandlerClick));
76-
}
77-
else
78-
{
79-
await HandlerClick();
80-
}
56+
if (IsAsync)
57+
{
58+
await Task.Run(() => InvokeAsync(HandlerClick));
59+
}
60+
else
61+
{
62+
await HandlerClick();
63+
}
8164

82-
// 恢复按钮
83-
if (IsAsync && ButtonType == ButtonType.Button)
84-
{
85-
ButtonIcon = Icon;
86-
IsDisabled = IsKeepDisabled;
87-
IsAsyncLoading = false;
88-
}
89-
});
65+
// 恢复按钮
66+
if (IsAsync && ButtonType == ButtonType.Button)
67+
{
68+
ButtonIcon = Icon;
69+
IsDisabled = IsKeepDisabled;
70+
IsAsyncLoading = false;
71+
}
9072
}
9173

9274
/// <summary>
@@ -107,7 +89,7 @@ protected virtual async Task HandlerClick()
10789
{
10890
IsNotRender = true;
10991
}
110-
await OnClickWithoutRender.Invoke();
92+
await OnClickWithoutRender();
11193
}
11294
if (OnClick.HasDelegate)
11395
{

src/BootstrapBlazor/Components/Button/CountButton.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6-
using Microsoft.AspNetCore.Components.Web;
7-
86
namespace BootstrapBlazor.Components;
97

108
/// <summary>
@@ -33,21 +31,18 @@ public class CountButton : Button
3331
/// <summary>
3432
/// <inheritdoc/>
3533
/// </summary>
36-
protected override void SetClickHandler()
34+
protected override async Task OnClickButton()
3735
{
38-
OnClickButton = EventCallback.Factory.Create<MouseEventArgs>(this, async () =>
39-
{
40-
IsAsyncLoading = true;
41-
ButtonIcon = LoadingIcon;
42-
IsDisabled = true;
36+
IsAsyncLoading = true;
37+
ButtonIcon = LoadingIcon;
38+
IsDisabled = true;
4339

44-
await Task.Run(() => InvokeAsync(HandlerClick));
45-
await UpdateCount();
40+
await Task.Run(() => InvokeAsync(HandlerClick));
41+
await UpdateCount();
4642

47-
IsDisabled = false;
48-
ButtonIcon = Icon;
49-
IsAsyncLoading = false;
50-
});
43+
IsDisabled = false;
44+
ButtonIcon = Icon;
45+
IsAsyncLoading = false;
5146
}
5247

5348
/// <summary>

src/BootstrapBlazor/Components/Dropdown/Dropdown.razor

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55

66
@if (IsShowLabel)
77
{
8-
<BootstrapLabel for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
8+
<BootstrapLabel for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText"></BootstrapLabel>
99
}
1010
<div @attributes="@AdditionalAttributes" id="@Id" class="@DirectionClassName">
1111
<DynamicElement TagName="button" type="button" class="@ButtonClassName" data-bs-toggle="@DropdownToggle" disabled="@Disabled"
1212
TriggerClick="ShowSplit" OnClick="OnClickButton">
1313
@if (ButtonTemplate == null)
1414
{
15-
@ButtonText
15+
@if (!string.IsNullOrEmpty(_buttonIcon))
16+
{
17+
<i class="@_buttonIcon"></i>
18+
}
19+
<span>@ButtonText</span>
1620
}
1721
else
1822
{

0 commit comments

Comments
 (0)