Skip to content

Commit de7fe2c

Browse files
committed
refactor: 复用代码
1 parent 02c2186 commit de7fe2c

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/BootstrapBlazor/Components/ListView/ListView.razor

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
{
1717
if (GroupName == null)
1818
{
19-
foreach (var item in Rows)
20-
{
21-
<div class="listview-item" @onclick="@(e => OnClick(item))">
22-
@BodyTemplate(item)
23-
</div>
24-
}
19+
@RenderGroup(Rows)
2520
}
2621
else if (Collapsible)
2722
{
@@ -38,12 +33,7 @@
3833
<div @key="@key.GroupName" class="accordion-item">
3934
<div class="accordion-header">@key.GroupName</div>
4035
<div class="accordion-body">
41-
@foreach (var item in key.Items)
42-
{
43-
<div class="listview-item" @onclick="@(e => OnClick(item))">
44-
@BodyTemplate(item)
45-
</div>
46-
}
36+
@RenderGroup(key.Items)
4737
</div>
4838
</div>
4939
}
@@ -77,14 +67,19 @@
7767
@code {
7868
RenderFragment RenderItem((object? GroupName, IOrderedEnumerable<TItem> Items) key, int index) =>
7969
@<CollapseItem Text="@GetGroupName(key.GroupName)" IsCollapsed="IsCollapsed(index, key.GroupName)">
80-
@foreach (var item in key.Items)
70+
@RenderGroup(key.Items)
71+
</CollapseItem>;
72+
73+
RenderFragment<IEnumerable<TItem>> RenderGroup => items =>
74+
@<div class="listview-item-group">
75+
@foreach (var item in items)
8176
{
82-
<div class="listview-item" @onclick="@(e => OnClick(item))">
77+
<div @key="item" class="listview-item" @onclick="@(e => OnClick(item))">
8378
@if(BodyTemplate != null)
8479
{
8580
@BodyTemplate(item)
8681
}
8782
</div>
8883
}
89-
</CollapseItem>;
84+
</div>;
9085
}

0 commit comments

Comments
 (0)