Skip to content
Open
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
44 changes: 43 additions & 1 deletion src/Core/Components/List/FluentAutocomplete.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public partial class FluentAutocomplete<TOption> : ListComponentBase<TOption> wh
private readonly Debounce _debounce = new();
private bool _shouldRender = true;
private bool _inProgress;
private IEnumerable<TOption> _allItems;

/// <summary>
/// Initializes a new instance of the <see cref="FluentAutocomplete{TOption}"/> class.
Expand Down Expand Up @@ -279,6 +280,38 @@ private string ComponentWidth
/// <summary />
protected override bool ShouldRender() => _shouldRender;

public override async Task SetParametersAsync(ParameterView parameters)
{
if (!Multiple)
{
bool isSetSelectedOption = false;
TOption? newSelectedOption = default;

foreach (var parameter in parameters)
{
if (parameter.Name.Equals(nameof(SelectedOption)))
{
isSetSelectedOption = true;
newSelectedOption = (TOption?)parameter.Value;
break;
}
}

if (newSelectedOption is not null)
{
if (isSetSelectedOption && !Equals(_currentSelectedOption, newSelectedOption))
{
if (_allItems is not null && Items is not null
&& _allItems.Contains(newSelectedOption) && !Items.Contains(newSelectedOption))
{
Items = Items.Append(newSelectedOption)!;
}
}
}
}
await base.SetParametersAsync(parameters);
}

/// <summary>
/// Closes the multiselect dropdown.
/// </summary>
Expand Down Expand Up @@ -341,8 +374,17 @@ public async Task InvokeOptionsSearchAsync()
};

await OnOptionsSearch.InvokeAsync(args);
_allItems = args.Items;

Items = args.Items?.Take(MaximumOptionsSearch);
var topItems = _allItems.Take(MaximumOptionsSearch);
if (!Multiple && SelectedOption is not null && _allItems is not null && Items is not null)
{
if (_allItems.Contains(SelectedOption) && !topItems.Contains(SelectedOption))
{
topItems = _allItems.Take(MaximumOptionsSearch - 1).Append(SelectedOption);
}
}
Items = topItems;

SelectableItem = Items != null
? Items.FirstOrDefault(i => OptionDisabled is null ? true : OptionDisabled.Invoke(i) == false)
Expand Down