Skip to content
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
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>10.2.3</Version>
<Version>10.2.4-beta01</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
[Parameter]
public bool EnableKeyboard { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 是否将 active 选中节点自动滚动到可视状态</para>
/// <para lang="en">Gets or sets whether to automatically scroll the active selected node into the visible state.</para>
/// </summary>
[Parameter]
public bool IsAutoScrollIntoView { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 键盘导航时的滚动至视图选项,默认为 null,使用 { behavior: "smooth", block: "nearest", inline: "start" }</para>
/// <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>
Expand Down Expand Up @@ -350,6 +357,7 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
private string? _searchText;
private bool _shouldRender = true;
private bool _init;
private bool _scrollIntoView = false;

/// <summary>
/// <inheritdoc/>
Expand Down Expand Up @@ -410,6 +418,7 @@ protected override async Task OnParametersSetAsync()
_activeItem ??= Items.FirstOrDefaultActiveItem();
_activeItem?.SetParentExpand<TreeViewItem<TItem>, TItem>(true);
_init = true;
_scrollIntoView = true;
}
}
}
Expand All @@ -424,6 +433,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

if (_keyboardArrowUpDownTrigger)
{
_scrollIntoView = false;
_keyboardArrowUpDownTrigger = false;
await InvokeVoidAsync("scroll", Id, ScrollIntoViewOptions);
}
Expand All @@ -432,6 +442,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
await InvokeVoidAsync("resetTreeViewRow", Id);
}

if (IsAutoScrollIntoView && _scrollIntoView)
{
_scrollIntoView = false;
await InvokeVoidAsync("scroll", Id, ScrollIntoViewOptions);
}
}

/// <summary>
Expand Down Expand Up @@ -703,6 +719,7 @@ public void SetActiveItem(TreeViewItem<TItem>? item)
{
_activeItem = item;
_activeItem?.SetParentExpand<TreeViewItem<TItem>, TItem>(true);
_scrollIntoView = true;
StateHasChanged();
}

Expand Down
30 changes: 30 additions & 0 deletions test/UnitTest/Components/TreeViewTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,36 @@ public async Task AllowDrag_Ok()
Assert.False(treeDragContext.IsChildren);
}

[Fact]
public async Task IsAutoScrollIntoView_Ok()
{
var items = TreeFoo.GetTreeItems();
ScrollIntoViewOptions? options = null;
Context.JSInterop.SetupVoid("scroll", invocationMatcher =>
{
options = invocationMatcher.Arguments[1] as ScrollIntoViewOptions;
return true;
}).SetVoidResult();
var cut = Context.Render<TreeView<TreeFoo>>(pb =>
{
pb.Add(a => a.Items, items);
pb.Add(a => a.IsAutoScrollIntoView, true);
});
Assert.Null(options);

cut.Render(pb =>
{
pb.Add(a => a.ScrollIntoViewOptions, new ScrollIntoViewOptions()
{
Behavior = ScrollIntoViewBehavior.Smooth,
Inline = ScrollIntoViewInline.Center,
Block = ScrollIntoViewBlock.Center
});
});
await cut.InvokeAsync(() => cut.Instance.SetActiveItem(items.First()));
Assert.NotNull(options);
}

class MockTree<TItem> : TreeView<TItem> where TItem : class
{
public bool TestComparerItem(TItem? a, TItem? b) => base.Equals(a, b);
Expand Down