Skip to content

Commit c19f3ab

Browse files
authored
feat(Dropdown): add OnClick event callback (#5319)
* feat: 增加 OnClick 回调方法 * feat: 增加 OnClickWithoutRender 回调方法 * feat: 增加点击事件委托方法实现 * feat: 更改为 DynamicElement 组件 * doc: 更新文档示例 * doc: 更新文档
1 parent 49454b1 commit c19f3ab

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<Dropdown TValue="string" Items="Items" ShowSplit="true" Color="Color.Primary"></Dropdown>
3838
</div>
3939
<div class="col-6 col-sm-4 col-md-3">
40-
<Dropdown TValue="string" Items="Items" ShowSplit="false" Color="Color.Info"></Dropdown>
40+
<Dropdown TValue="string" Items="Items" ShowSplit="true" Color="Color.Info"></Dropdown>
4141
</div>
4242
<div class="col-6 col-sm-4 col-md-3">
4343
<Dropdown TValue="string" Items="Items" ShowSplit="true" Color="Color.Warning"></Dropdown>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ private AttributeItem[] GetAttributes() =>
251251
/// <returns></returns>
252252
private EventItem[] GetEvents() =>
253253
[
254+
new()
255+
{
256+
Name = "OnClick",
257+
Description = Localizer["EventDesc1"],
258+
Type ="EventCallback<MouseEventArgs>"
259+
},
260+
new()
261+
{
262+
Name = "OnClickWithoutRender",
263+
Description = Localizer["EventDesc2"],
264+
Type ="Func<Task>"
265+
},
254266
new EventItem()
255267
{
256268
Name = "OnSelectedItemChanged",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,8 @@
17931793
"AttributeButtonTemplate": "The template of button",
17941794
"AttributeItemTemplate": "The template of item",
17951795
"AttributeItemsTemplate": "The template of items",
1796+
"EventDesc1": "This event is triggered when the button is clicked",
1797+
"EventDesc2": "This event is triggered when the button is clicked and the current component is not refreshed for performance improvement",
17961798
"EventOnSelectedItemChanged": "Triggered when the value of the drop-down box changes",
17971799
"FixedButtonText": "The text of fixed button",
17981800
"Item1": "Melbourne",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,8 @@
17931793
"AttributeButtonTemplate": "按钮模板",
17941794
"AttributeItemTemplate": "菜单项模板",
17951795
"AttributeItemsTemplate": "下拉菜单模板",
1796+
"EventDesc1": "点击按钮时触发此事件",
1797+
"EventDesc2": "点击按钮时触发此事件并且不刷新当前组件,用于提高性能时使用",
17961798
"EventOnSelectedItemChanged": "下拉框值发生改变时触发",
17971799
"FixedButtonText": "固定按钮显示文字",
17981800
"Item1": "北京",

src/BootstrapBlazor/Components/Dropdown/Dropdown.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<BootstrapLabel for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
99
}
1010
<div @attributes="@AdditionalAttributes" id="@Id" class="@DirectionClassName">
11-
<button type="button" class="@ButtonClassName" data-bs-toggle="@DropdownToggle" disabled="@Disabled">
11+
<DynamicElement type="button" class="@ButtonClassName" data-bs-toggle="@DropdownToggle" disabled="@Disabled"
12+
TriggerClick="ShowSplit" OnClick="OnClickButton">
1213
@if (ButtonTemplate == null)
1314
{
1415
@ButtonText
@@ -17,7 +18,7 @@
1718
{
1819
@ButtonTemplate(SelectedItem)
1920
}
20-
</button>
21+
</DynamicElement>
2122
@if (ShowSplit)
2223
{
2324
<button type="button" class="@ClassName" data-bs-toggle="@ToggleString" disabled="@Disabled" aria-haspopup="true" aria-expanded="false"></button>

src/BootstrapBlazor/Components/Dropdown/Dropdown.razor.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
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+
68
namespace BootstrapBlazor.Components;
79

810
/// <summary>
@@ -102,6 +104,18 @@ public partial class Dropdown<TValue>
102104
[Parameter]
103105
public bool ShowSplit { get; set; }
104106

107+
/// <summary>
108+
/// 获得/设置 OnClick 事件
109+
/// </summary>
110+
[Parameter]
111+
public EventCallback<MouseEventArgs> OnClick { get; set; }
112+
113+
/// <summary>
114+
/// 获得/设置 OnClick 事件不刷新父组件
115+
/// </summary>
116+
[Parameter]
117+
public Func<Task>? OnClickWithoutRender { get; set; }
118+
105119
/// <summary>
106120
/// 获得/设置 获取菜单对齐方式 默认 none 未设置
107121
/// </summary>
@@ -203,4 +217,16 @@ protected async Task OnItemClick(SelectedItem item)
203217
}
204218

205219
private string? ButtonText => IsFixedButtonText ? FixedButtonText : SelectedItem?.Text;
220+
221+
private async Task OnClickButton()
222+
{
223+
if (OnClickWithoutRender != null)
224+
{
225+
await OnClickWithoutRender();
226+
}
227+
if (OnClick.HasDelegate)
228+
{
229+
await OnClick.InvokeAsync();
230+
}
231+
}
206232
}

0 commit comments

Comments
 (0)