Skip to content

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

TypeScript

fix(Typeahead, Tokenizer): ArrowDown on the closed field loads a replaced search source instead of showing its old list (#9166)

Cindy Zhangf89931310d6a
diff --git a/.changeset/typeahead-arrowdown-replaced-source.md b/.changeset/typeahead-arrowdown-replaced-source.md
new file mode 100644
index 0000000000..533674d1ec
--- /dev/null
+++ b/.changeset/typeahead-arrowdown-replaced-source.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Typeahead` and `Tokenizer`: after the search source is replaced, pressing ArrowDown on the closed field loads the new source (its focus entries, or the typed query against it) instead of opening the old source's list.
+
+@cixzhang
diff --git a/packages/core/src/Typeahead/BaseTypeahead.test.tsx b/packages/core/src/Typeahead/BaseTypeahead.test.tsx
index 68090c454b..cfca32d6bf 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.test.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.test.tsx
@@ -293,6 +293,95 @@ describe('BaseTypeahead', () => {
},
);
+ // spec:AST-092 FR4: a replaced source's list is no longer current, so
+ // ArrowDown on the closed field loads the new source instead of showing it.
+ it('loads the replacement source when ArrowDown opens the closed field', async () => {
+ const user = userEvent.setup();
+ const originalSource: SearchSource<SearchableItem> = {
+ search: () => [],
+ bootstrap: () => [resultItem],
+ };
+ const updatedBootstrap = vi.fn(() => [{id: '2', label: 'Updated result'}]);
+ const updatedSource: SearchSource<SearchableItem> = {
+ search: () => [],
+ bootstrap: updatedBootstrap,
+ };
+ const renderField = (searchSource: SearchSource<SearchableItem>) => (
+ <BaseTypeahead
+ searchSource={searchSource}
+ value={null}
+ onChange={() => {}}
+ hasEntriesOnFocus
+ />
+ );
+ const {rerender} = render(renderField(originalSource));
+ const input = screen.getByRole('combobox');
+
+ await user.click(input);
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ });
+ await user.keyboard('{Escape}');
+ rerender(renderField(updatedSource));
+ expect(input).toHaveAttribute('aria-expanded', 'false');
+
+ await user.keyboard('{ArrowDown}');
+ 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(updatedBootstrap).toHaveBeenCalledTimes(1);
+ });
+
+ it('searches the replacement source for the typed query when ArrowDown opens the closed field', async () => {
+ const user = userEvent.setup();
+ const originalSource: SearchSource<SearchableItem> = {
+ search: () => [resultItem],
+ bootstrap: () => [],
+ };
+ const updatedSearch = vi.fn(() => [{id: '2', label: 'Updated result'}]);
+ const updatedSource: SearchSource<SearchableItem> = {
+ search: updatedSearch,
+ bootstrap: () => [],
+ };
+ const renderField = (searchSource: SearchSource<SearchableItem>) => (
+ <BaseTypeahead
+ searchSource={searchSource}
+ value={null}
+ onChange={() => {}}
+ />
+ );
+ const {rerender} = render(renderField(originalSource));
+ const input = screen.getByRole('combobox');
+
+ await user.type(input, 're');
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ expect(
+ screen.getByRole('option', {name: 'Result', hidden: true}),
+ ).toBeInTheDocument();
+ });
+ await user.keyboard('{Escape}');
+ rerender(renderField(updatedSource));
+
+ await user.keyboard('{ArrowDown}');
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ expect(
+ screen.getByRole('option', {name: 'Updated result', hidden: true}),
+ ).toBeInTheDocument();
+ });
+ expect(updatedSearch).toHaveBeenCalledWith('re');
+ expect(
+ screen.queryByRole('option', {name: 'Result', hidden: true}),
+ ).not.toBeInTheDocument();
+ });
+
it('re-shows cached results without bootstrapping again when the source is unchanged', async () => {
const user = userEvent.setup();
const bootstrap = vi.fn(() => [resultItem]);
diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsx
index 84991463f0..e7b2a77e7f 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx
@@ -1091,15 +1091,30 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
if (!popover.isOpen) {
if (e.key === 'ArrowDown' && (hasEntriesOnFocus || query.length > 0)) {
e.preventDefault();
- if (results.length > 0) {
+ // Results held from a replaced search source are not current
+ // (spec:AST-092 FR4): they count as none, so the new source loads.
+ const hasResultsFromCurrentSource =
+ results.length > 0 && resultsGenRef.current >= sourceGenRef.current;
+ const isQueryBelowMinLength = isBelowMinQueryLength(
+ query,
+ minQueryLength,
+ );
+ if (hasResultsFromCurrentSource) {
popover.show();
setHighlightedIndex(0);
+ } else if (
+ results.length > 0 &&
+ query.length > 0 &&
+ !isQueryBelowMinLength
+ ) {
+ // The typed query runs again against the new source.
+ void performSearch(query);
} else if (
hasEntriesOnFocus &&
// A below-threshold query was never searched; falling back to the
// bootstrap entries here would open a menu of suggestions that
// ignore what the user has already typed.
- !isBelowMinQueryLength(query, minQueryLength)
+ !isQueryBelowMinLength
) {
void performBootstrap();
}
@@ -1170,6 +1185,7 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
query,
minQueryLength,
performBootstrap,
+ performSearch,
externalOnKeyDown,
],
);