Skip to content

Commit 1314f4b

Browse files
authored
[Autocomplete] Fix keyboard usage in Single mode (#3930)
1 parent 47b370c commit 1314f4b

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/Core/Components/List/FluentAutocomplete.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,14 @@
216216

217217
if (Multiple == false)
218218
{
219-
<FluentLabel tabindex="0" aria-label="@GetOptionText(item)" role="checkbox" aria-checked="true">@text</FluentLabel>
219+
<FluentLabel Id="@($"{Id}-single")"
220+
tabindex="0"
221+
aria-label="@GetOptionText(item)"
222+
role="checkbox"
223+
aria-checked="true"
224+
@onkeydown="@(e => e.Code == "Delete" || e.Code == "Backspace" ? RemoveSelectedItemAsync(item) : Task.CompletedTask)">
225+
@text
226+
</FluentLabel>
220227
}
221228
else if (ReadOnly || Disabled)
222229
{

src/Core/Components/List/FluentAutocomplete.razor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public async Task InvokeOptionsSearchAsync()
327327
Items = args.Items?.Take(MaximumOptionsSearch);
328328

329329
SelectableItem = Items != null
330-
? Items.FirstOrDefault()
330+
? Items.FirstOrDefault(i => OptionDisabled is null ? true : OptionDisabled.Invoke(i) == false)
331331
: default;
332332

333333
if (VirtualizationContainer != null)
@@ -558,6 +558,13 @@ protected override async Task OnSelectedItemChangedHandlerAsync(TOption? item)
558558
await RaiseValueTextChangedAsync(ValueText);
559559

560560
await base.OnSelectedItemChangedHandlerAsync(item);
561+
562+
// In Single mode, set the focus on the input field
563+
if (!Multiple && Module != null)
564+
{
565+
await Module.InvokeVoidAsync("focusOn", $"{Id}-single");
566+
}
567+
561568
await DisplayLastSelectedItemAsync();
562569

563570
if (MustBeClosed())

src/Core/Components/List/ListComponentBase.razor.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,14 @@ protected virtual async Task OnSelectedItemChangedHandlerAsync(TOption? item)
568568
InternalValue = GetOptionValue(item);
569569
await RaiseChangedEventsAsync();
570570
}
571+
572+
// For Autocomplete, allow to unselect the item if it is already selected
573+
else if (this is FluentAutocomplete<TOption>)
574+
{
575+
SelectedOption = default;
576+
InternalValue = GetOptionValue(default);
577+
await RaiseChangedEventsAsync();
578+
}
571579
}
572580
}
573581

@@ -632,6 +640,12 @@ protected virtual bool RemoveSelectedItem(TOption? item)
632640
return false;
633641
}
634642

643+
if (this is FluentAutocomplete<TOption> && SelectedOption is not null)
644+
{
645+
SelectedOption = default;
646+
InternalValue = GetOptionValue(default);
647+
}
648+
635649
return _selectedOptions.Remove(item);
636650
}
637651

0 commit comments

Comments
 (0)