Skip to content

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

TypeScript

fix(PowerSearch): reopen the menu on an already-focused input (#6850)

zakelstorm5f76965890ac
diff --git a/.changeset/powersearch-reopen-focused-input.md b/.changeset/powersearch-reopen-focused-input.md
new file mode 100644
index 0000000000..d69398d2c2
--- /dev/null
+++ b/.changeset/powersearch-reopen-focused-input.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Refresh stale cached field entries when `PowerSearch` reopens an already-focused input after its search source changes (#6845).
+
+@korkt-kim
diff --git a/packages/core/src/Typeahead/BaseTypeahead.test.tsx b/packages/core/src/Typeahead/BaseTypeahead.test.tsx
index 0a094a1337..b2ee856f9e 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.test.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.test.tsx
@@ -3,12 +3,13 @@
/**
* @file BaseTypeahead.test.tsx
* @input BaseTypeahead public props and a synchronous SearchSource
- * @output Focused contract tests for the public combobox engine
+ * @output Combobox contract tests, including stale source refresh on reopen
* @position Colocated verification for BaseTypeahead
*/
import {afterAll, beforeAll, describe, expect, it, vi} from 'vitest';
import {fireEvent, render, screen, waitFor} from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
import * as stylex from '@stylexjs/stylex';
import {BaseTypeahead} from './BaseTypeahead';
import type {SearchSource, SearchableItem} from './types';
@@ -151,6 +152,63 @@ describe('BaseTypeahead', () => {
expect(input).toHaveAttribute('tabindex', '-1');
});
+ it('refreshes nonempty cached results after source replacement on a focused-input click', async () => {
+ const user = userEvent.setup();
+ const originalSource: SearchSource<SearchableItem> = {
+ search: () => [],
+ bootstrap: () => [resultItem],
+ };
+ const updatedSource: SearchSource<SearchableItem> = {
+ search: () => [],
+ bootstrap: () => [{id: '2', label: 'Updated result'}],
+ };
+ const {rerender} = render(
+ <BaseTypeahead
+ searchSource={originalSource}
+ value={null}
+ onChange={() => {}}
+ hasEntriesOnFocus
+ />,
+ );
+ const input = screen.getByRole('combobox');
+
+ await user.click(input);
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ });
+ expect(
+ screen.getByRole('option', {name: 'Result', hidden: true}),
+ ).toBeInTheDocument();
+
+ await user.keyboard('{Escape}');
+ rerender(
+ <BaseTypeahead
+ searchSource={updatedSource}
+ value={null}
+ onChange={() => {}}
+ hasEntriesOnFocus
+ />,
+ );
+ expect(input).toHaveFocus();
+ expect(input).toHaveValue('');
+ expect(input).toHaveAttribute('aria-expanded', 'false');
+ expect(
+ screen.getByRole('option', {name: 'Result', hidden: true}),
+ ).toBeInTheDocument();
+
+ await user.click(input);
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ expect(
+ screen.getByRole('option', {name: 'Updated result', hidden: true}),
+ ).toBeInTheDocument();
+ });
+ expect(
+ screen.queryByRole('option', {name: 'Result', hidden: true}),
+ ).not.toBeInTheDocument();
+ expect(input).toHaveFocus();
+ });
+
it('counts grapheme clusters when enforcing minQueryLength', async () => {
const search = vi.fn(() => [resultItem]);
render(
diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsx
index 5170fc5811..3515872ee2 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx
@@ -5,7 +5,7 @@
/**
* @file BaseTypeahead.tsx
* @input Uses React, StyleX, usePopover, TypeaheadItem
- * @output Exports BaseTypeahead combobox engine component
+ * @output Exports BaseTypeahead with focused-input menu reactivation
* @position Core implementation; used by Typeahead and Tokenizer
*
* Pure combobox engine: input, search, keyboard navigation, dropdown.
@@ -931,8 +931,22 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
if (!wasAlreadyFocusedRef.current || popover.isOpen) {
return;
}
+ if (
+ hasEntriesOnFocus &&
+ query.length === 0 &&
+ resultsGenRef.current !== searchGenRef.current
+ ) {
+ void performBootstrap();
+ return;
+ }
openIfEligible();
- }, [popover.isOpen, openIfEligible]);
+ }, [
+ popover.isOpen,
+ hasEntriesOnFocus,
+ query.length,
+ performBootstrap,
+ openIfEligible,
+ ]);
// Handle blur — close the dropdown when focus leaves the input for an
// element that is neither inside the field wrapper (anchor) nor inside the