Skip to content

Commit 459ea17

Browse files
authored
doc(Dropdown): add sample code for ItemTemplate (#5374)
* refactor: 增强 ItemTemplate 模板 * style: 增加 tooltip 兼容样式 * doc: 增加 ItemTemplate 示例 * doc: 更新示例本地化资源文件 * chore: bump version 9.3.1-beta18 * refactor: 重构代码
1 parent 8a5e79c commit 459ea17

File tree

8 files changed

+36
-13
lines changed

8 files changed

+36
-13
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
</div>
3232
</DemoBlock>
3333

34+
<DemoBlock Title="@Localizer["DropdownItemTemplateTitle"]" Introduction="@Localizer["DropdownItemTemplateIntro"]" Name="ItemTemplate">
35+
<Dropdown TValue="string" Items="ItemTemplateList">
36+
<ItemTemplate>
37+
<Tooltip Title="just a tooltip text demo" Trigger="hover">
38+
<span class="fa-solid fa-flag"></span>
39+
<div class="ms-2 flex-fill">@context.Text</div>
40+
</Tooltip>
41+
</ItemTemplate>
42+
</Dropdown>
43+
</DemoBlock>
44+
3445
<DemoBlock Title="@Localizer["SplitTitle"]" Introduction='@Localizer["SplitIntro"]' Name="Split">
3546
<div class="row g-3">
3647
<div class="col-6 col-sm-4 col-md-3">

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public sealed partial class Dropdowns
1818

1919
private List<SelectedItem> Items { get; set; } = [];
2020

21+
private List<SelectedItem> ItemTemplateList { get; set; } = [];
22+
2123
/// <summary>
2224
/// ShowMessage
2325
/// </summary>
@@ -41,6 +43,13 @@ protected override void OnInitialized()
4143
new SelectedItem() { Text=Localizer["Item3"], Value="2"},
4244
];
4345

46+
ItemTemplateList =
47+
[
48+
new SelectedItem() { Text=Localizer["Item1"], Value="0"},
49+
new SelectedItem() { Text=Localizer["Item2"], Value="1"},
50+
new SelectedItem() { Text=Localizer["Item3"], Value="2"},
51+
];
52+
4453
Foos =
4554
[
4655
new SelectedItem{ Text=Localizer["Item1"], Value="0"},

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,9 @@
18131813
"DropdownCascadeItem5": "Chaoyang",
18141814
"DropdownCascadeItem6": "Haidian",
18151815
"DropdownCascadeItem7": "Jingan",
1816-
"DropdownCascadeItem8": "Huangpu"
1816+
"DropdownCascadeItem8": "Huangpu",
1817+
"DropdownItemTemplateTitle": "Item Template",
1818+
"DropdownItemTemplateIntro": "By setting <code>ItemTemplate</code>, you can customize the content displayed in the drop-down item. In this example, the <code>Tooltip</code> component is used to add a tooltip function when the mouse is hovered."
18171819
},
18181820
"BootstrapBlazor.Server.Components.Samples.GoTops": {
18191821
"Title": "GoTop",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,9 @@
18131813
"DropdownCascadeItem5": "朝阳区",
18141814
"DropdownCascadeItem6": "海淀区",
18151815
"DropdownCascadeItem7": "静安区",
1816-
"DropdownCascadeItem8": "黄浦区"
1816+
"DropdownCascadeItem8": "黄浦区",
1817+
"DropdownItemTemplateTitle": "下拉项模板",
1818+
"DropdownItemTemplateIntro": "通过设置 <code>ItemTemplate</code> 可以自定义下拉项显示内容,本例中通过自定义模板使用 <code>Tooltip</code> 组件增加鼠标悬浮是显示提示功能"
18171819
},
18181820
"BootstrapBlazor.Server.Components.Samples.GoTops": {
18191821
"Title": "GoTop 返回顶端组件",

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-beta17</Version>
4+
<Version>9.3.1-beta18</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Dropdown/Dropdown.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@
3838
{
3939
if (ItemTemplate != null)
4040
{
41-
@ItemTemplate.Invoke(item)
41+
<div class="@ActiveItem(item)">
42+
@ItemTemplate(item)
43+
</div>
4244
}
4345
else if (item.IsDisabled)
4446
{
45-
<div class="@DisableItem(item)">@item.Text</div>
47+
<div class="@ActiveItem(item)">@item.Text</div>
4648
}
4749
else
4850
{

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,6 @@ public partial class Dropdown<TValue>
6363
/// <returns></returns>
6464
protected string? ActiveItem(SelectedItem item) => CssBuilder.Default("dropdown-item")
6565
.AddClass("active", () => item.Value == CurrentValueAsString)
66-
.Build();
67-
68-
/// <summary>
69-
/// 获得/设置 设置当前项是否 Active 方法
70-
/// </summary>
71-
/// <param name="item"></param>
72-
/// <returns></returns>
73-
protected string? DisableItem(SelectedItem item) => CssBuilder.Default("dropdown-item")
7466
.AddClass("disabled", item.IsDisabled)
7567
.Build();
7668

src/BootstrapBlazor/Components/Tooltip/Tooltip.razor.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
display: inline-block;
33
}
44

5+
.dropdown-item > span[data-bs-toggle="tooltip"] {
6+
display: flex;
7+
align-items: center;
8+
}
9+
510
.tooltip.is-invalid {
611
--bs-tooltip-bg: var(--bs-danger);
712
}

0 commit comments

Comments
 (0)