Skip to content

Commit

Permalink
lib: Improve key navigation in TypeaheadSelect and MultiTypeaheadSelect
Browse files Browse the repository at this point in the history
In the TypeaheadSelect component, the focusedItemIndex is for the
filteredSelections array, not for the selectOptions one. Thus without
this fix, we would get the aria stuff wrong. We might even end up
accessing non-existing items, when focusedItemIndex refers to a "No
results found" fake entry in filteredSelections with index 0 while
selectOptions is empty.

The fake entries are now properly disabled and will be skipped by key
navigation in both TypeaheadSelect and MultiTypeaheadSelect.

Also, key navigation relies on there being at least one option that
will not be skipped. Thus, we need to us isEnabledMenu instead of
isMenu when deciding whether to return early. This got broken in
8d9f97b.
  • Loading branch information
mvollmer authored and martinpitt committed Jan 5, 2025
1 parent d4eee9f commit 225e9c2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/lib/cockpit-components-multi-typeahead-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
newSelectOptions = [
{
isAriaDisabled: true,
isDisabled: true,
content:
typeof noOptionsFoundMessage === 'string' ? noOptionsFoundMessage : noOptionsFoundMessage(inputValue),
value: NO_RESULTS
Expand Down
6 changes: 4 additions & 2 deletions pkg/lib/cockpit-components-typeahead-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
newSelectOptions = [
{
isAriaDisabled: true,
isDisabled: true,
content:
typeof noOptionsFoundMessage === 'string' ? noOptionsFoundMessage : noOptionsFoundMessage(filterValue),
value: NO_RESULTS
Expand All @@ -287,6 +288,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
newSelectOptions = [
{
isAriaDisabled: true,
isDisabled: true,
content: noOptionsAvailableMessage,
value: NO_RESULTS
}
Expand Down Expand Up @@ -316,7 +318,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>

const setActiveAndFocusedItem = (itemIndex: number) => {
setFocusedItemIndex(itemIndex);
const focusedItem = selectOptions[itemIndex] as TypeaheadSelectMenuOption;
const focusedItem = filteredSelections[itemIndex] as TypeaheadSelectMenuOption;
setActiveItemId(String(focusedItem.value));
};

Expand Down Expand Up @@ -384,7 +386,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>

openMenu();

if (filteredSelections.every(o => !isMenu(o))) {
if (filteredSelections.every(o => !isEnabledMenu(o))) {
return;
}

Expand Down

0 comments on commit 225e9c2

Please sign in to comment.