Skip to content

Commit 7fa251c

Browse files
feat(SelectTree): add ShowSearch parameter (#5037)
* feat: 增加 ShowSearch 参数 * refactor: 重构固定 Search 栏样式兼容 Select-Tree * test: 更新 TreeView 单元测试 * test: 更新 SelectTree 单元测试 Co-Authored-By: Alex chow <[email protected]>
1 parent 8b7cc2a commit 7fa251c

File tree

8 files changed

+72
-13
lines changed

8 files changed

+72
-13
lines changed

src/BootstrapBlazor/Components/Select/SelectTree.razor

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<BootstrapLabel required="@Required" for="@InputId" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
99
}
1010
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassName">
11-
<div class="dropdown-toggle" data-bs-toggle="@ToggleString" data-bs-auto-close="outside" data-bs-offset="@OffsetString" data-bs-placement="@PlacementString" data-bs-custom-class="@CustomClassString">
11+
<div class="dropdown-toggle" data-bs-toggle="@ToggleString" data-bs-auto-close="outside" data-bs-offset="@OffsetString" data-bs-placement="@PlacementString" data-bs-custom-class="@SelectTreeCustomClassString">
1212
@if (IsEditable)
1313
{
1414
<input type="text" id="@InputId" disabled="@Disabled" placeholder="@PlaceHolder" class="@InputClassName" value="@CurrentValueAsString" @onchange="@OnChange" />
@@ -20,7 +20,11 @@
2020
<span class="@AppendClassName"><i class="@DropdownIcon"></i></span>
2121
</div>
2222
<div class="dropdown-menu">
23-
<TreeView TItem="TValue" Items="@Items" ShowIcon="ShowIcon" OnTreeItemClick="OnItemClick" ModelEqualityComparer="@ModelEqualityComparer" CustomKeyAttribute="@CustomKeyAttribute" OnExpandNodeAsync="@OnExpandNodeAsync" />
23+
<TreeView TItem="TValue" Items="@Items" ShowIcon="ShowIcon"
24+
OnTreeItemClick="OnItemClick" ModelEqualityComparer="@ModelEqualityComparer"
25+
ShowSearch="ShowSearch" ShowResetSearchButton="ShowResetSearchButton"
26+
IsFixedSearch="IsFixedSearch"
27+
CustomKeyAttribute="@CustomKeyAttribute" OnExpandNodeAsync="@OnExpandNodeAsync" />
2428
</div>
2529
@if (!IsPopover)
2630
{

src/BootstrapBlazor/Components/Select/SelectTree.razor.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ public partial class SelectTree<TValue> : IModelEqualityComparer<TValue>
120120
[Parameter]
121121
public bool IsEditable { get; set; }
122122

123+
/// <summary>
124+
/// 获得/设置 是否显示搜索栏 默认 false 不显示
125+
/// </summary>
126+
[Parameter]
127+
public bool ShowSearch { get; set; }
128+
129+
/// <summary>
130+
/// 获得/设置 是否固定搜索栏 默认 false 不固定
131+
/// </summary>
132+
[Parameter]
133+
public bool IsFixedSearch { get; set; }
134+
135+
/// <summary>
136+
/// 获得/设置 是否显示重置搜索栏按钮 默认 true 显示
137+
/// </summary>
138+
[Parameter]
139+
public bool ShowResetSearchButton { get; set; } = true;
140+
123141
[Inject]
124142
[NotNull]
125143
private IStringLocalizer<SelectTree<TValue>>? Localizer { get; set; }
@@ -149,6 +167,10 @@ public partial class SelectTree<TValue> : IModelEqualityComparer<TValue>
149167
[NotNull]
150168
private IIconTheme? IconTheme { get; set; }
151169

170+
private string? SelectTreeCustomClassString => CssBuilder.Default(CustomClassString)
171+
.AddClass("select-tree", IsPopover)
172+
.Build();
173+
152174
/// <summary>
153175
/// <inheritdoc/>
154176
/// </summary>
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
.select-tree {
1+
.select-tree {
22
--bb-select-tree-dropdown-max-height: var(--bb-dropdown-max-height);
3-
}
43

5-
.select-tree .dropdown-menu {
4+
.dropdown-menu {
65
max-height: var(--bb-select-tree-dropdown-max-height);
6+
7+
.tree-view {
8+
max-height: calc(var(--bb-select-tree-dropdown-max-height) - var(--bs-dropdown-padding-y) * 2 - 2px);
9+
}
710
}
11+
}

src/BootstrapBlazor/Components/TreeView/TreeView.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ else
4444
}
4545
@if (IsVirtualize)
4646
{
47-
<div class="tree-root is-virtual scroll" tabindex="0">
47+
<div class="tree-root is-virtual" tabindex="0">
4848
<Virtualize ItemSize="RowHeight" OverscanCount="10" Items="@Rows">
4949
<ItemContent>
5050
@RenderTreeRow(context)
@@ -54,7 +54,7 @@ else
5454
}
5555
else
5656
{
57-
<div class="tree-root scroll" tabindex="0">
57+
<div class="tree-root" tabindex="0">
5858
@foreach (var item in Rows)
5959
{
6060
@RenderTreeRow(item)

src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
1717
/// <summary>
1818
/// 获得 按钮样式集合
1919
/// </summary>
20-
private string? ClassString => CssBuilder.Default("tree-view")
20+
private string? ClassString => CssBuilder.Default("tree-view scroll")
2121
.AddClass("is-fixed-search", ShowSearch && IsFixedSearch)
2222
.AddClassFromAttributes(AdditionalAttributes)
2323
.Build();

src/BootstrapBlazor/Components/TreeView/TreeView.razor.scss

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
height: 100%;
1919

2020
.tree-search {
21-
margin-block-end: .5rem;
21+
padding: var(--bb-tree-padding);
22+
padding-bottom: .5rem;
23+
background-color: var(--bs-body-bg);
2224
}
2325

24-
&.is-fixed-search .tree-root {
25-
height: calc(100% - var(--bb-tree-search-height));
26+
&.is-fixed-search .tree-search {
27+
position: sticky;
28+
top: 0;
29+
z-index: 5;
2630
}
2731

2832
.tree-root {

test/UnitTest/Components/SelectTreeTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,31 @@ public void IsActive_Ok()
204204
Assert.Contains("active", nodes[1].ClassName);
205205
}
206206

207+
[Fact]
208+
public void ShowSearch_Ok()
209+
{
210+
var items = TreeFoo.GetTreeItems();
211+
var cut = Context.RenderComponent<SelectTree<TreeFoo>>(builder =>
212+
{
213+
builder.Add(p => p.Items, items);
214+
builder.Add(p => p.Value, new TreeFoo() { Id = "1020", Text = "Navigation Two" });
215+
builder.Add(p => p.ShowSearch, true);
216+
});
217+
cut.Contains("tree-search");
218+
219+
cut.SetParametersAndRender(pb =>
220+
{
221+
pb.Add(a => a.ShowResetSearchButton, true);
222+
});
223+
cut.Contains("tree-search-reset");
224+
225+
cut.SetParametersAndRender(pb =>
226+
{
227+
pb.Add(a => a.IsFixedSearch, true);
228+
});
229+
cut.Contains("is-fixed-search");
230+
}
231+
207232
private List<TreeViewItem<string>> BindItems { get; } =
208233
[
209234
new TreeViewItem<string>("Test1")

test/UnitTest/Components/TreeViewTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public async Task IsVirtualize_Ok()
516516
pb.Add(a => a.IsVirtualize, false);
517517
pb.Add(a => a.Items, items);
518518
});
519-
cut.Contains("tree-root scroll");
519+
cut.Contains("tree-root");
520520

521521
cut.SetParametersAndRender(pb =>
522522
{
@@ -537,7 +537,7 @@ public async Task IsVirtualize_Ok()
537537
return [node1, node2];
538538
});
539539
});
540-
cut.Contains("tree-root is-virtual scroll");
540+
cut.Contains("tree-root is-virtual");
541541

542542
// 触发第一个节点展开
543543
await cut.InvokeAsync(() => cut.Find(".node-icon.visible").Click());

0 commit comments

Comments
 (0)