Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Select): can not switch Value when data type is SelectedItem on Virtual mode #4850

Merged
merged 6 commits into from
Dec 14, 2024
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
19 changes: 10 additions & 9 deletions src/BootstrapBlazor/Components/Select/Select.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private SelectedItem? SelectedRow
var item = Rows.Find(i => i.Value == CurrentValueAsString)
?? Rows.Find(i => i.Active)
?? Rows.Where(i => !i.IsDisabled).FirstOrDefault()
?? GetVirtualizeItem();
?? GetVirtualizeItem(CurrentValueAsString);

if (item != null)
{
Expand Down Expand Up @@ -376,22 +376,23 @@ protected override bool TryParseValueFromString(string value, [MaybeNullWhen(fal

private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue result, out string? validationErrorMessage)
{
SelectedItem = Items.FirstOrDefault(i => i.Value == value)
?? GetVirtualizeItem();
SelectedItem = Rows.FirstOrDefault(i => i.Value == value)
?? GetVirtualizeItem(value);

// support SelectedItem? type
result = SelectedItem != null ? (TValue)(object)SelectedItem : default;
validationErrorMessage = "";
return SelectedItem != null;
}

private SelectedItem? GetVirtualizeItem()
private SelectedItem? GetVirtualizeItem(string value)
{
return OnQueryAsync == null ? null : GetSelectedItem();

SelectedItem? GetSelectedItem() => ValueType == typeof(SelectedItem)
? (SelectedItem)(object)Value
: new SelectedItem(CurrentValueAsString, DefaultVirtualizeItemText ?? CurrentValueAsString);
SelectedItem? item = null;
if (_result.Items != null)
{
item = _result.Items.FirstOrDefault(i => i.Value == value) ?? new SelectedItem(value, DefaultVirtualizeItemText ?? value);
}
return item;
}

/// <summary>
Expand Down
23 changes: 10 additions & 13 deletions test/UnitTest/Components/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ public async Task IsVirtualize_OnQueryAsync_Clearable_Ok()
}

[Fact]
public void IsVirtualize_BindValue()
public async Task IsVirtualize_BindValue()
{
var value = new SelectedItem("3", "Test 3");
var cut = Context.RenderComponent<Select<SelectedItem>>(pb =>
Expand All @@ -772,24 +772,21 @@ public void IsVirtualize_BindValue()
});
});

cut.InvokeAsync(() =>
{
var input = cut.Find(".form-select");
Assert.Equal("Test 3", input.GetAttribute("value"));
});
cut.Contains("Test 3");
var input = cut.Find(".form-select");
Assert.Null(input.GetAttribute("value"));

var select = cut.Instance;
Assert.Equal("3", select.Value?.Value);

cut.InvokeAsync(() =>
var item = cut.Find(".dropdown-item");
await cut.InvokeAsync(() =>
{
var item = cut.Find(".dropdown-item");
item.Click();
Assert.Equal("1", value.Value);

var input = cut.Find(".form-select");
Assert.Equal("Test1", input.GetAttribute("value"));
});
Assert.Equal("1", value.Value);

input = cut.Find(".form-select");
Assert.Equal("Test1", input.GetAttribute("value"));
}

[Fact]
Expand Down
Loading