Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Select): add GroupItemTemplate parameter #2264

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/BootstrapBlazor/Components/Divider/Divider.razor.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*divider*/
.divider {
--bb-divider-bg: #{$divider-bg};
--bb-divider-margin: #{$divider-margin};
Expand Down Expand Up @@ -68,8 +67,3 @@
}
}
}


.dropdown-menu .divider {
--bb-divider-margin: .25rem 0;
}
9 changes: 8 additions & 1 deletion src/BootstrapBlazor/Components/Select/MultiSelect.razor
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@
{
if (!string.IsNullOrEmpty(itemGroup.Key))
{
<Divider Text="@itemGroup.Key" />
if (GroupItemTemplate != null)
{
@GroupItemTemplate(itemGroup.Key)
}
else
{
<Divider Text="@itemGroup.Key" />
}
}
@foreach (var item in itemGroup)
{
Expand Down
9 changes: 8 additions & 1 deletion src/BootstrapBlazor/Components/Select/Select.razor
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@
{
if (!string.IsNullOrEmpty(itemGroup.Key))
{
<Divider Text="@itemGroup.Key" />
if (GroupItemTemplate != null)
{
@GroupItemTemplate(itemGroup.Key)
}
else
{
<Divider Text="@itemGroup.Key" />
}
}
@foreach (var item in itemGroup)
{
Expand Down
6 changes: 6 additions & 0 deletions src/BootstrapBlazor/Components/Select/SelectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public abstract class SelectBase<TValue> : PopoverSelectBase<TValue>
[Parameter]
public RenderFragment<SelectedItem>? ItemTemplate { get; set; }

/// <summary>
/// 获得/设置 分组项模板
/// </summary>
[Parameter]
public RenderFragment<string>? GroupItemTemplate { get; set; }

/// <summary>
/// 获得/设置 IIconTheme 服务实例
/// </summary>
Expand Down

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions src/BootstrapBlazor/wwwroot/scss/bootstrap.blazor.css
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,6 @@ section > h2 {
padding-left: 10px;
}

/*divider*/
.divider {
--bb-divider-bg: #dcdfe6;
--bb-divider-margin: 1rem 0;
Expand Down Expand Up @@ -1952,10 +1951,6 @@ section > h2 {
transform: translateX(50%);
}

.dropdown-menu .divider {
--bb-divider-margin: .25rem 0;
}

/*add this to avoid flickering*/
.bb-dd-inprogess > * {
pointer-events: none;
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/wwwroot/scss/bootstrap.blazor.min.css

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions test/UnitTest/Components/MultiSelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,30 @@ public void ItemTemplate_Ok()
Assert.Contains("test-Test2-test", cut.Markup);
}

[Fact]
public void GroupItemTemplate_Ok()
{
var cut = Context.RenderComponent<MultiSelect<string>>(pb =>
{
pb.Add(a => a.Value, "1");
pb.Add(a => a.ShowCloseButton, false);
pb.Add(a => a.Items, new List<SelectedItem>
{
new SelectedItem("1", "Test1") { GroupName = "Test1" },
new SelectedItem("2", "Test2") { GroupName = "Test2" }
});
pb.Add(a => a.GroupItemTemplate, title => builder =>
{
builder.OpenElement(0, "div");
builder.AddAttribute(1, "class", "group-key");
builder.AddContent(2, title);
builder.CloseElement();
});
});
cut.Contains("<div class=\"group-key\">Test1</div>");
cut.Contains("<div class=\"group-key\">Test2</div>");
}

[Fact]
public void SearchIcon_Ok()
{
Expand Down
23 changes: 23 additions & 0 deletions test/UnitTest/Components/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,29 @@ public void ItemTemplate_Ok()
cut.Find(".dropdown-item").Click();
}

[Fact]
public void GroupItemTemplate_Ok()
{
var cut = Context.RenderComponent<Select<string>>(pb =>
{
pb.Add(a => a.Items, new SelectedItem[]
{
new SelectedItem("1", "Test1") { GroupName = "Test1" },
new SelectedItem("2", "Test2") { GroupName = "Test2" }
});
pb.Add(a => a.Value, "2");
pb.Add(a => a.GroupItemTemplate, title => builder =>
{
builder.OpenElement(0, "div");
builder.AddAttribute(1, "class", "group-key");
builder.AddContent(2, title);
builder.CloseComponent();
});
});
cut.Contains("<div class=\"group-key\">Test1</div>");
cut.Contains("<div class=\"group-key\">Test2</div>");
}

[Fact]
public void NullItems_Ok()
{
Expand Down