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
Changes from 1 commit
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);
}
return item ?? new SelectedItem(value, DefaultVirtualizeItemText ?? value);
}

/// <summary>
Expand Down
Loading