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
5 changes: 2 additions & 3 deletions src/BootstrapBlazor/Components/Table/Table.razor
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,12 @@
{
@if (Items != null)
{
<Virtualize @ref="VirtualizeElement"
ItemSize="RowHeight" OverscanCount="10" Items="@Items.ToList()" ChildContent="RenderRow">
<Virtualize ItemSize="RowHeight" OverscanCount="10" Items="@Items.ToList()" ChildContent="RenderRow">
</Virtualize>
}
else
{
<Virtualize @ref="VirtualizeElement"
<Virtualize @ref="_virtualizeElement"
ItemSize="RowHeight" OverscanCount="10" Placeholder="RenderPlaceholderRow"
ItemsProvider="LoadItems" ItemContent="RenderRow">
</Virtualize>
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ protected void OnClickCardView()

private async Task QueryAsync(bool shouldRender, int? pageIndex = null, bool triggerByPagination = false)
{
if (ScrollMode == ScrollMode.Virtual && VirtualizeElement != null)
if (ScrollMode == ScrollMode.Virtual && _virtualizeElement != null)
{
await VirtualizeElement.RefreshDataAsync();
await _virtualizeElement.RefreshDataAsync();
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public partial class Table<TItem> : ITable, IModelEqualityComparer<TItem> where
/// <summary>
/// 获得/设置 内置虚拟化组件实例
/// </summary>
protected Virtualize<TItem>? VirtualizeElement { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): Using [NotNull] with conditional assignment may be misleading.

The _virtualizeElement field is marked with [NotNull] yet may remain null depending on the rendering branch. Please verify that this attribute accurately reflects its lifecycle or consider handling the nullability to avoid potential warnings or runtime issues.

[NotNull]
private Virtualize<TItem>? _virtualizeElement = null;

/// <summary>
/// 获得 Table 组件样式表
Expand Down
5 changes: 3 additions & 2 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8911,9 +8911,10 @@ public async Task TestLoopQueryAsync()

public RenderFragment RenderVirtualPlaceHolder() => new(builder =>
{
if (ScrollMode == ScrollMode.Virtual && VirtualizeElement != null)
var fieldInfo = GetType().BaseType!.GetField("_virtualizeElement", BindingFlags.NonPublic | BindingFlags.Instance)!;
if (ScrollMode == ScrollMode.Virtual && fieldInfo.GetValue(this) is Virtualize<Foo> element)
Comment on lines +8914 to +8915
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Avoid testing private fields with reflection.

Testing private fields using reflection makes tests brittle and tightly coupled to implementation details. Consider exposing the necessary information through a protected property or method if it's essential for testing. Alternatively, refactor the test to interact with the component's public API and verify the expected behavior without relying on internal state.

Suggested implementation:

            if (ScrollMode == ScrollMode.Virtual && VirtualizeElement is Virtualize<Foo> element)

Ensure that the component exposes a protected property VirtualizeElement (if not already present) that returns the value of the private _virtualizeElement field. For example, in the component's base class you may add:

protected Virtualize? VirtualizeElement => _virtualizeElement;

This change avoids testing private fields via reflection and makes the test less brittle while still exposing the necessary state for verifying the component behavior.

{
builder.AddContent(0, VirtualizeElement.Placeholder?.Invoke(new Microsoft.AspNetCore.Components.Web.Virtualization.PlaceholderContext()));
builder.AddContent(0, element.Placeholder?.Invoke(new PlaceholderContext()));
}
});

Expand Down