Skip to content

Commit 186d9ea

Browse files
authored
feat(TreeView): add IsAutoScrollIntoView parameter (#7585)
* feat(TreeView): add IsAutoScrollIntoView parameter * feat: 增加 _scrollIntoView 控制内部逻辑 * chore: bump version 10.2.4-beta01 * test: 更新单元测试 * test: 更新单元测试
1 parent 81ea9d1 commit 186d9ea

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

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>10.2.3</Version>
4+
<Version>10.2.4-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
245245
[Parameter]
246246
public bool EnableKeyboard { get; set; }
247247

248+
/// <summary>
249+
/// <para lang="zh">获得/设置 是否将 active 选中节点自动滚动到可视状态</para>
250+
/// <para lang="en">Gets or sets whether to automatically scroll the active selected node into the visible state.</para>
251+
/// </summary>
252+
[Parameter]
253+
public bool IsAutoScrollIntoView { get; set; }
254+
248255
/// <summary>
249256
/// <para lang="zh">获得/设置 键盘导航时的滚动至视图选项,默认为 null,使用 { behavior: "smooth", block: "nearest", inline: "start" }</para>
250257
/// <para lang="en">Gets or sets the scroll into view options for keyboard navigation. Default is null, using { behavior: "smooth", block: "nearest", inline: "start" }</para>
@@ -350,6 +357,7 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
350357
private string? _searchText;
351358
private bool _shouldRender = true;
352359
private bool _init;
360+
private bool _scrollIntoView = false;
353361

354362
/// <summary>
355363
/// <inheritdoc/>
@@ -410,6 +418,7 @@ protected override async Task OnParametersSetAsync()
410418
_activeItem ??= Items.FirstOrDefaultActiveItem();
411419
_activeItem?.SetParentExpand<TreeViewItem<TItem>, TItem>(true);
412420
_init = true;
421+
_scrollIntoView = true;
413422
}
414423
}
415424
}
@@ -424,6 +433,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
424433

425434
if (_keyboardArrowUpDownTrigger)
426435
{
436+
_scrollIntoView = false;
427437
_keyboardArrowUpDownTrigger = false;
428438
await InvokeVoidAsync("scroll", Id, ScrollIntoViewOptions);
429439
}
@@ -432,6 +442,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
432442
{
433443
await InvokeVoidAsync("resetTreeViewRow", Id);
434444
}
445+
446+
if (IsAutoScrollIntoView && _scrollIntoView)
447+
{
448+
_scrollIntoView = false;
449+
await InvokeVoidAsync("scroll", Id, ScrollIntoViewOptions);
450+
}
435451
}
436452

437453
/// <summary>
@@ -703,6 +719,7 @@ public void SetActiveItem(TreeViewItem<TItem>? item)
703719
{
704720
_activeItem = item;
705721
_activeItem?.SetParentExpand<TreeViewItem<TItem>, TItem>(true);
722+
_scrollIntoView = true;
706723
StateHasChanged();
707724
}
708725

test/UnitTest/Components/TreeViewTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,36 @@ public async Task AllowDrag_Ok()
12691269
Assert.False(treeDragContext.IsChildren);
12701270
}
12711271

1272+
[Fact]
1273+
public async Task IsAutoScrollIntoView_Ok()
1274+
{
1275+
var items = TreeFoo.GetTreeItems();
1276+
ScrollIntoViewOptions? options = null;
1277+
Context.JSInterop.SetupVoid("scroll", invocationMatcher =>
1278+
{
1279+
options = invocationMatcher.Arguments[1] as ScrollIntoViewOptions;
1280+
return true;
1281+
}).SetVoidResult();
1282+
var cut = Context.Render<TreeView<TreeFoo>>(pb =>
1283+
{
1284+
pb.Add(a => a.Items, items);
1285+
pb.Add(a => a.IsAutoScrollIntoView, true);
1286+
});
1287+
Assert.Null(options);
1288+
1289+
cut.Render(pb =>
1290+
{
1291+
pb.Add(a => a.ScrollIntoViewOptions, new ScrollIntoViewOptions()
1292+
{
1293+
Behavior = ScrollIntoViewBehavior.Smooth,
1294+
Inline = ScrollIntoViewInline.Center,
1295+
Block = ScrollIntoViewBlock.Center
1296+
});
1297+
});
1298+
await cut.InvokeAsync(() => cut.Instance.SetActiveItem(items.First()));
1299+
Assert.NotNull(options);
1300+
}
1301+
12721302
class MockTree<TItem> : TreeView<TItem> where TItem : class
12731303
{
12741304
public bool TestComparerItem(TItem? a, TItem? b) => base.Equals(a, b);

0 commit comments

Comments
 (0)