Skip to content

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

TypeScript

fix(Typeahead, Tokenizer): picking a result reports the emptied query to onChangeQuery (#8929) (#9993)

Ojas Mutreja97873156b98b
diff --git a/.changeset/typeahead-pick-reports-empty-query.md b/.changeset/typeahead-pick-reports-empty-query.md
new file mode 100644
index 0000000000..4c0a9e96db
--- /dev/null
+++ b/.changeset/typeahead-pick-reports-empty-query.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Typeahead, Tokenizer: picking a result now calls `onChangeQuery('')`. The pick empties the input but never reported it, so an app that mirrored the query kept the text typed before the pick. A pick made while the query was already empty reports nothing, as nothing changed. (#8929)
+
+@HelloOjasMutreja
diff --git a/packages/core/src/Typeahead/BaseTypeahead.test.tsx b/packages/core/src/Typeahead/BaseTypeahead.test.tsx
index 6e8a9653e6..7c21d0d20f 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.test.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.test.tsx
@@ -88,6 +88,87 @@ const testStyles = stylex.create({
});
describe('BaseTypeahead', () => {
+ describe('picking a result', () => {
+ const source: SearchSource<SearchableItem> = {
+ search: () => [
+ {id: 'a', label: 'Apple'},
+ {id: 'b', label: 'Apricot'},
+ ],
+ bootstrap: () => [],
+ };
+
+ it('reports the emptied query to onChangeQuery in a Typeahead', async () => {
+ const user = userEvent.setup();
+ const onChangeQuery = vi.fn();
+ render(
+ <Typeahead
+ label="Fruit"
+ searchSource={source}
+ value={null}
+ onChange={() => {}}
+ onChangeQuery={onChangeQuery}
+ />,
+ );
+
+ await user.type(screen.getByRole('combobox'), 'Ap');
+ expect(onChangeQuery).toHaveBeenLastCalledWith('Ap');
+ await user.click(
+ await screen.findByRole('option', {name: 'Apple', hidden: true}),
+ );
+
+ expect(screen.getByRole('combobox')).toHaveValue('');
+ expect(onChangeQuery).toHaveBeenLastCalledWith('');
+ });
+
+ it('reports the emptied query to onChangeQuery in a Tokenizer', async () => {
+ const user = userEvent.setup();
+ const onChangeQuery = vi.fn();
+ render(
+ <Tokenizer
+ label="Fruits"
+ searchSource={source}
+ value={[]}
+ onChange={() => {}}
+ onChangeQuery={onChangeQuery}
+ />,
+ );
+
+ await user.type(screen.getByRole('combobox'), 'Ap');
+ expect(onChangeQuery).toHaveBeenLastCalledWith('Ap');
+ await user.click(
+ await screen.findByRole('option', {name: 'Apple', hidden: true}),
+ );
+
+ expect(screen.getByRole('combobox')).toHaveValue('');
+ expect(onChangeQuery).toHaveBeenLastCalledWith('');
+ });
+
+ it('does not report a change when the query was already empty', async () => {
+ const user = userEvent.setup();
+ const onChangeQuery = vi.fn();
+ render(
+ <Typeahead
+ label="Fruit"
+ hasEntriesOnFocus
+ searchSource={{
+ search: () => [],
+ bootstrap: () => [{id: 'a', label: 'Apple'}],
+ }}
+ value={null}
+ onChange={() => {}}
+ onChangeQuery={onChangeQuery}
+ />,
+ );
+
+ await user.click(screen.getByRole('combobox'));
+ await user.click(
+ await screen.findByRole('option', {name: 'Apple', hidden: true}),
+ );
+
+ expect(onChangeQuery).not.toHaveBeenCalled();
+ });
+ });
+
it('asks a touch keyboard for its Search key and no autocorrection', () => {
render(
<BaseTypeahead
diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsx
index 25f60e2ea2..509eaf0c1e 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx
@@ -1375,6 +1375,12 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
searchSource.cancel?.();
onChange(item);
setQuery('');
+ // The pick empties the input, which is a change to the query like any
+ // typed one. A query that was already empty (a pick from the focus list)
+ // did not change.
+ if (query !== '') {
+ onChangeQuery?.('');
+ }
replaceResults([]);
setHeldHeight(null);
setHasSearched(false);
@@ -1385,7 +1391,16 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
popover.hide();
inputRef.current?.focus();
},
- [isDisabled, onChange, popover, searchSource, setLoading, replaceResults],
+ [
+ isDisabled,
+ onChange,
+ onChangeQuery,
+ query,
+ popover,
+ searchSource,
+ setLoading,
+ replaceResults,
+ ],
);
// Shared by handleFocus and handleClick: bootstrap entries if none are