Skip to content

Commit 72c7d17

Browse files
committed
refactor: 增加 DropdownMenu 组件
1 parent 1ac25d9 commit 72c7d17

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@namespace BootstrapBlazor.Components
2+
3+
<ul class="dropdown-menu">
4+
@foreach (var item in Rows)
5+
{
6+
<li @key="item" class="dropdown-item" @onclick="() => OnClick(item)">
7+
@if (ItemTemplate == null)
8+
{
9+
<div>@item</div>
10+
}
11+
else
12+
{
13+
@ItemTemplate(item)
14+
}
15+
</li>
16+
}
17+
@if (ShowNoDataTip && Rows.Count == 0)
18+
{
19+
<li class="dropdown-item">@NoDataTip</li>
20+
}
21+
</ul>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
/// DropdownMenu component
10+
/// </summary>
11+
public partial class DropdownMenu
12+
{
13+
/// <summary>
14+
/// Get or set the dropdown menu items
15+
/// </summary>
16+
[Parameter, NotNull, EditorRequired]
17+
public List<string>? Rows { get; set; }
18+
19+
/// <summary>
20+
/// Get or set the dropdown menu item template default value is null.
21+
/// </summary>
22+
[Parameter]
23+
public RenderFragment<string>? ItemTemplate { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets whether to show the no matching data option, default is true
27+
/// </summary>
28+
[Parameter]
29+
public bool ShowNoDataTip { get; set; } = true;
30+
31+
/// <summary>
32+
/// Gets or sets Display prompt message when there is no matching data. The default prompt is "No Data"
33+
/// </summary>
34+
[Parameter]
35+
[NotNull]
36+
public string? NoDataTip { get; set; }
37+
38+
/// <summary>
39+
/// Gets or sets Callback method when a candidate item is clicked
40+
/// </summary>
41+
[Parameter]
42+
public Func<string, Task>? OnItemClick { get; set; }
43+
44+
private Task OnClick(string val)
45+
{
46+
if (OnItemClick != null)
47+
{
48+
return OnItemClick.Invoke(val);
49+
}
50+
return Task.CompletedTask;
51+
}
52+
53+
/// <summary>
54+
/// Render method
55+
/// </summary>
56+
public void Render(List<string> items)
57+
{
58+
Rows = items;
59+
StateHasChanged();
60+
}
61+
}

0 commit comments

Comments
 (0)