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>9.5.5</Version>
<Version>9.5.6-beta01</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 15 additions & 2 deletions src/BootstrapBlazor/Components/Select/Select.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public partial class Select<TValue> : ISelect, ILookup

private ItemsProviderResult<SelectedItem> _result;

private string _defaultVirtualizedItemText = "";

private SelectedItem? SelectedItem { get; set; }

private SelectedItem? SelectedRow
Expand All @@ -175,7 +177,7 @@ private SelectedItem? SelectedRow
return null;
}

var item = IsVirtualize ? GetItemByVirtulized() : GetItemByRows();
var item = IsVirtualize ? GetItemByVirtualized() : GetItemByRows();
if (item != null)
{
if (_init && DisableItemChangedWhenFirstRender)
Expand All @@ -193,7 +195,7 @@ private SelectedItem? SelectedRow

private SelectedItem? GetItemWithEnumValue() => ValueType.IsEnum ? Rows.Find(i => i.Value == Convert.ToInt32(Value).ToString()) : null;

private SelectedItem GetItemByVirtulized() => new(CurrentValueAsString, DefaultVirtualizeItemText ?? CurrentValueAsString);
private SelectedItem GetItemByVirtualized() => new(CurrentValueAsString, _defaultVirtualizedItemText);

private SelectedItem? GetItemByRows()
{
Expand All @@ -204,6 +206,16 @@ private SelectedItem? SelectedRow
return item;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();

_defaultVirtualizedItemText = DefaultVirtualizeItemText ?? CurrentValueAsString;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand Down Expand Up @@ -363,6 +375,7 @@ private async Task OnClickItem(SelectedItem item)
}
if (ret)
{
_defaultVirtualizedItemText = item.Text;
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Review the update of default virtualized text after an item click.

Updating _defaultVirtualizedItemText on item click changes what is considered the default text. Ensure that this behavior is intended or consider renaming the field to better reflect its dynamic role.

await SelectedItemChanged(item);
}
}
Expand Down
25 changes: 23 additions & 2 deletions test/UnitTest/Components/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ public void ItemClick_Ok()
}

[Fact]
public void DefaultVirtualizeItemText_Ok()
public void DefaultVirtualizeItemText_Null()
{
var cut = Context.RenderComponent<Select<string>>(pb =>
{
Expand All @@ -776,12 +776,33 @@ public void DefaultVirtualizeItemText_Ok()

var input = cut.Find(".form-select");
Assert.Contains("value=\"3\"", input.OuterHtml);
}

cut.SetParametersAndRender(pb =>
[Fact]
public async Task DefaultVirtualizeItemText_Ok()
{
var cut = Context.RenderComponent<Select<string>>(pb =>
{
pb.Add(a => a.Items, new SelectedItem[]
{
new("1", "Test1"),
new("2", "Test2")
});
pb.Add(a => a.Value, "3");
pb.Add(a => a.IsVirtualize, true);
pb.Add(a => a.DefaultVirtualizeItemText, "Test3");
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): Suggest adding a test case for null CurrentValueAsString with DefaultVirtualizeItemText set.

It would be beneficial to have a test case where CurrentValueAsString is null and DefaultVirtualizeItemText is set to a non-null value to ensure the component behaves as expected in this scenario. This would cover the edge case where the initial value is null but a default display text is provided for virtualized items.

Suggested implementation:

    [Fact]
    public void DefaultVirtualizeItemText_WithNullCurrentValueAsString()
    {
        var cut = Context.RenderComponent<Select<string>>(pb =>
        {
            pb.Add(a => a.Items, new SelectedItem[]
            {
                new("1", "Test1"),
                new("2", "Test2")
            });
            // set Value to null to simulate a null CurrentValueAsString
            pb.Add(a => a.Value, null);
            pb.Add(a => a.IsVirtualize, true);
            pb.Add(a => a.DefaultVirtualizeItemText, "Test3");
        });

        // Assert that the default virtualize text appears in the markup when Value is null.
        Assert.Contains("Test3", cut.Markup);
    }

Ensure that the CSS selectors used in the test (if any) are correct for your component.
Also, adjust the test case name or assertions if your component renders the default virtualize text differently.

});

var input = cut.Find(".form-select");
Assert.Contains("value=\"Test3\"", input.OuterHtml);

var items = cut.FindAll(".dropdown-item");
Assert.Equal(2, items.Count);

var item = items[1];
await cut.InvokeAsync(() => item.Click());
input = cut.Find(".form-select");
Assert.Contains("value=\"Test2\"", input.OuterHtml);
}

[Fact]
Expand Down