Skip to content

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

TypeScript

fix(Typeahead): dismiss the result list on the Tab keydown, not on the blur (#5400)

Cindy Zhanga7bedb2ac53a
diff --git a/.changeset/typeahead-tab-dismiss-on-keydown.md b/.changeset/typeahead-tab-dismiss-on-keydown.md
new file mode 100644
index 0000000000..ed42ec5d28
--- /dev/null
+++ b/.changeset/typeahead-tab-dismiss-on-keydown.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Typeahead: Tab out of the field now moves focus to the next control. The result list is dismissed on the Tab keydown rather than from the blur that press produces — hiding a top-layer popover during the focusout makes Chrome abandon the in-flight focus move and drop focus to `<body>`, so the press appeared to do nothing. Selector and MultiSelector already dismissed on the keydown.
+@cixzhang
diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsx
index fe98c70d62..7155722138 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx
@@ -692,6 +692,14 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
e.preventDefault();
popover.hide();
break;
+ case 'Tab':
+ // Dismiss here rather than from the blur this press produces:
+ // hiding a top-layer popover during the focusout makes Chrome
+ // abandon the in-flight focus move and drop focus to <body>, so the
+ // user's Tab appears to do nothing. Selector and MultiSelector
+ // already dismiss on this keydown.
+ popover.hide();
+ break;
case 'Home':
if (popover.isOpen) {
e.preventDefault();
diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsx
index 0f2ad5a69c..b12de11a91 100644
--- a/packages/core/src/Typeahead/Typeahead.test.tsx
+++ b/packages/core/src/Typeahead/Typeahead.test.tsx
@@ -420,6 +420,31 @@ describe('BaseTypeahead focus-out', () => {
});
});
+ it('closes the list on the Tab keydown, before the blur it produces', async () => {
+ render(
+ <BaseTypeahead
+ searchSource={fruitSource}
+ value={null}
+ onChange={() => {}}
+ debounceMs={0}
+ />,
+ );
+ const input = screen.getByRole('combobox');
+ input.focus();
+ fireEvent.change(input, {target: {value: 'App'}});
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ });
+
+ // No blur is fired here on purpose: dismissing from the blur instead lets
+ // the popover close mid-focus-move, which Chrome answers by dropping
+ // focus to <body>.
+ fireEvent.keyDown(input, {key: 'Tab'});
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'false');
+ });
+ });
+
it('Tab from the input with the list open moves focus to the next control', async () => {
const user = userEvent.setup();
render(