Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Typeahead): omit aria-activedescendant when search results are empty (#4059) (#4312)

Ojas Mutreja67539432fa20
diff --git a/.changeset/typeahead-omit-aria-activedescendant-when-search-tjsk.md b/.changeset/typeahead-omit-aria-activedescendant-when-search-tjsk.md
new file mode 100644
index 0000000000..f4d06dfa2f
--- /dev/null
+++ b/.changeset/typeahead-omit-aria-activedescendant-when-search-tjsk.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Typeahead: omit aria-activedescendant when search results are empty or index is out of bounds (#4059)
+@HelloOjasMutreja
diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsx
index 6f25b1aea3..000f8a6fb9 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx
@@ -431,7 +431,7 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
resultsGenRef.current = gen;
const shown = searchResults.slice(0, maxMenuItems);
setResults(shown);
- setHighlightedIndex(searchResults.length > 0 ? 0 : -1);
+ setHighlightedIndex(shown.length > 0 ? 0 : -1);
if (searchResults.length > 0 || searchQuery.length > 0) {
showLayer();
}
@@ -469,8 +469,9 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
return;
}
resultsGenRef.current = gen;
- setResults(bootstrapResults.slice(0, maxMenuItems));
- setHighlightedIndex(bootstrapResults.length > 0 ? 0 : -1);
+ const shown = bootstrapResults.slice(0, maxMenuItems);
+ setResults(shown);
+ setHighlightedIndex(shown.length > 0 ? 0 : -1);
if (bootstrapResults.length > 0) {
showLayer();
}
@@ -637,15 +638,19 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
switch (e.key) {
case 'ArrowDown':
e.preventDefault();
- setHighlightedIndex(prev =>
- prev < results.length - 1 ? prev + 1 : 0,
- );
+ if (results.length > 0) {
+ setHighlightedIndex(prev =>
+ prev < results.length - 1 ? prev + 1 : 0,
+ );
+ }
break;
case 'ArrowUp':
e.preventDefault();
- setHighlightedIndex(prev =>
- prev > 0 ? prev - 1 : results.length - 1,
- );
+ if (results.length > 0) {
+ setHighlightedIndex(prev =>
+ prev > 0 ? prev - 1 : results.length - 1,
+ );
+ }
break;
case 'Enter':
e.preventDefault();
@@ -660,13 +665,17 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
case 'Home':
if (popover.isOpen) {
e.preventDefault();
- setHighlightedIndex(0);
+ if (results.length > 0) {
+ setHighlightedIndex(0);
+ }
}
break;
case 'End':
if (popover.isOpen) {
e.preventDefault();
- setHighlightedIndex(results.length - 1);
+ if (results.length > 0) {
+ setHighlightedIndex(results.length - 1);
+ }
}
break;
}
@@ -694,13 +703,17 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
// cursor walks off-screen once navigation passes the visible window. Mirrors
// CommandPaletteItem's scrollIntoView({block: 'nearest'}) behavior.
useEffect(() => {
- if (!popover.isOpen || highlightedIndex < 0) {
+ if (
+ !popover.isOpen ||
+ highlightedIndex < 0 ||
+ highlightedIndex >= results.length
+ ) {
return;
}
document
.getElementById(getItemId(highlightedIndex))
?.scrollIntoView?.({block: 'nearest'});
- }, [popover.isOpen, highlightedIndex, getItemId]);
+ }, [popover.isOpen, highlightedIndex, getItemId, results.length]);
const selectedKey =
value == null ? null : getKey(value.id, () => results.indexOf(value));
@@ -725,7 +738,9 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
aria-expanded={popover.isOpen}
aria-controls={listboxId}
aria-activedescendant={
- popover.isOpen && highlightedIndex >= 0
+ popover.isOpen &&
+ highlightedIndex >= 0 &&
+ highlightedIndex < results.length
? getItemId(highlightedIndex)
: undefined
}
diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsx
index 67d9275f8c..463a785721 100644
--- a/packages/core/src/Typeahead/Typeahead.test.tsx
+++ b/packages/core/src/Typeahead/Typeahead.test.tsx
@@ -171,6 +171,33 @@ describe('BaseTypeahead', () => {
});
});
+ describe('empty results active descendant (#4059)', () => {
+ it('does not set aria-activedescendant when search has 0 results', async () => {
+ render(
+ <BaseTypeahead
+ searchSource={fruitSource}
+ value={null}
+ onChange={() => {}}
+ debounceMs={0}
+ />,
+ );
+ const input = screen.getByRole('combobox');
+ fireEvent.change(input, {target: {value: 'zzzzz'}});
+
+ await waitFor(() => {
+ expect(input).not.toHaveAttribute('aria-activedescendant');
+ });
+
+ // Press ArrowDown — should NOT set aria-activedescendant to option-0
+ fireEvent.keyDown(input, {key: 'ArrowDown'});
+ expect(input).not.toHaveAttribute('aria-activedescendant');
+
+ // Press Home — should NOT set aria-activedescendant
+ fireEvent.keyDown(input, {key: 'Home'});
+ expect(input).not.toHaveAttribute('aria-activedescendant');
+ });
+ });
+
it('disables input when isDisabled', () => {
render(
<BaseTypeahead