Skip to content

Commit

Permalink
Add Autocomplete.KeepOpen (#2829)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoituron authored Oct 18, 2024
1 parent e303540 commit b53390a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5660,6 +5660,12 @@
Default is false.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentAutocomplete`1.KeepOpen">
<summary>
Gets or sets whether the drop-down panel stays open after selecting an item,
until the number of selected items reaches the maximum (only using the mouse).
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentAutocomplete`1.ListStyleValue">
<summary />
</member>
Expand Down Expand Up @@ -5714,6 +5720,9 @@
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentAutocomplete`1.RaiseValueTextChangedAsync(System.String)">
<summary />
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentAutocomplete`1.MustBeClosed">
<summary />
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentCombobox`1.LibraryConfiguration">
<summary />
</member>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Placeholder="search"
OnOptionsSearch="@OnSearch"
MaximumSelectedOptions="3"
KeepOpen="true"
OptionText="@(item => item.FirstName)"
OptionStyle="min-height: 40px;"
@bind-SelectedOptions="@SelectedItems">
Expand Down
45 changes: 42 additions & 3 deletions src/Core/Components/List/FluentAutocomplete.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ public override bool Multiple
[Parameter]
public bool SelectValueOnTab { get; set; } = false;

/// <summary>
/// Gets or sets whether the drop-down panel stays open after selecting an item,
/// until the number of selected items reaches the maximum (only using the mouse).
/// </summary>
[Parameter]
public bool KeepOpen { get; set; } = false;

/// <summary />
private string? ListStyleValue => new StyleBuilder()
.AddStyle("width", Width, when: !string.IsNullOrEmpty(Width))
Expand Down Expand Up @@ -287,6 +294,7 @@ protected override async Task InputHandlerAsync(ChangeEventArgs e)
if (MaximumSelectedOptions > 0 && SelectedOptions?.Count() >= MaximumSelectedOptions)
{
IsReachedMaxItems = true;
RenderComponent();
return;
}

Expand Down Expand Up @@ -319,8 +327,14 @@ protected override async Task InputHandlerAsync(ChangeEventArgs e)
await VirtualizationContainer.RefreshDataAsync();
}

_shouldRender = true;
StateHasChanged();
RenderComponent();

// Activate the rendering
void RenderComponent()
{
_shouldRender = true;
StateHasChanged();
}
}

private ValueTask<ItemsProviderResult<TOption>> LoadFilteredItemsAsync(ItemsProviderRequest request)
Expand Down Expand Up @@ -534,9 +548,13 @@ protected override async Task OnSelectedItemChangedHandlerAsync(TOption? item)
ValueText = string.Empty;
await RaiseValueTextChangedAsync(ValueText);

IsMultiSelectOpened = false;
await base.OnSelectedItemChangedHandlerAsync(item);
await DisplayLastSelectedItemAsync();

if (MustBeClosed())
{
IsMultiSelectOpened = false;
}
}

/// <summary />
Expand Down Expand Up @@ -619,4 +637,25 @@ private async Task RaiseValueTextChangedAsync(string value)
}

}

/// <summary />
private bool MustBeClosed()
{
if (KeepOpen == false)
{
return true;
}

if (MaximumSelectedOptions is null || MaximumSelectedOptions <= 1)
{
return true;
}

if (MaximumSelectedOptions > 0 && _selectedOptions.Count >= MaximumSelectedOptions)
{
return true;
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/Core/Components/List/ListComponentBase.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract partial class ListComponentBase<TOption> : FluentInputBase<strin

private bool _multiple = false;
private bool _hasInitializedParameters;
private List<TOption> _selectedOptions = [];
protected List<TOption> _selectedOptions = [];
protected TOption? _currentSelectedOption;
protected readonly RenderFragment _renderOptions;

Expand Down

0 comments on commit b53390a

Please sign in to comment.