An open source design system that's fully customizable and agent ready
TypeScript
fix(Typeahead): empty the input when cleared while editing the selected value (#7165)
Ojas Mutreja801c4bc74bee
diff --git a/.changeset/typeahead-clear-in-edit-mode.md b/.changeset/typeahead-clear-in-edit-mode.mdnew file mode 100644index 0000000000..0b062f1fd0--- /dev/null+++ b/.changeset/typeahead-clear-in-edit-mode.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Typeahead: clearing the value while editing it now empties the input and reports `onChangeQuery('')`, instead of leaving the old label in a field with no value. Fixes #7038.++@HelloOjasMutrejadiff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsxindex 73a1c0a2f2..729a7a4a03 100644--- a/packages/core/src/Typeahead/Typeahead.test.tsx+++ b/packages/core/src/Typeahead/Typeahead.test.tsx@@ -1637,6 +1637,95 @@ describe('Typeahead edit mode', () => {expect(onChange).not.toHaveBeenCalled();});+ it('empties the input when cleared while editing the selected value (#7038)', async () => {+ const onChangeQuery = vi.fn();+ function Controlled() {+ const [value, setValue] = useState<SearchableItem | null>(fruits[0]);+ return (+ <Typeahead+ label="Fruit"+ searchSource={fruitSource}+ value={value}+ onChange={setValue}+ onChangeQuery={onChangeQuery}+ hasClear+ />+ );+ }+ render(<Controlled />);+ const input = screen.getByRole<HTMLInputElement>('combobox');++ // Enter edit mode: the token gives way to the label as editable text+ fireEvent.click(screen.getByText(fruits[0].label).closest('div')!);+ await waitFor(() => expect(input.value).toBe(fruits[0].label));++ fireEvent.click(screen.getByRole('button', {name: 'Clear selection'}));++ // The value and the visible text agree: nothing selected, nothing typed+ await waitFor(() => expect(input.value).toBe(''));+ expect(onChangeQuery).toHaveBeenLastCalledWith('');+ expect(input).toHaveFocus();+ });++ // Escape and blur restore the token but leave the edit-mode label in the+ // base's query, so a clear from the restored token faces the same stale text.+ it.each([+ [+ 'Escape',+ (input: HTMLElement) => fireEvent.keyDown(input, {key: 'Escape'}),+ ],+ ['blur', (input: HTMLElement) => fireEvent.blur(input)],+ ])(+ 'empties the input when cleared after %s restored the token (#7038)',+ async (_name, leaveEditMode) => {+ const onChangeQuery = vi.fn();+ function Controlled() {+ const [value, setValue] = useState<SearchableItem | null>(fruits[0]);+ return (+ <Typeahead+ label="Fruit"+ searchSource={fruitSource}+ value={value}+ onChange={setValue}+ onChangeQuery={onChangeQuery}+ hasClear+ />+ );+ }+ render(<Controlled />);+ const input = screen.getByRole<HTMLInputElement>('combobox');++ fireEvent.click(screen.getByText(fruits[0].label).closest('div')!);+ await waitFor(() => expect(input.value).toBe(fruits[0].label));++ leaveEditMode(input);+ await screen.findByRole('button', {name: 'Clear selection'});++ fireEvent.click(screen.getByRole('button', {name: 'Clear selection'}));++ await waitFor(() => expect(input.value).toBe(''));+ expect(onChangeQuery).toHaveBeenLastCalledWith('');+ },+ );++ it('does not report a query change when cleared outside edit mode', () => {+ const onChangeQuery = vi.fn();+ render(+ <Typeahead+ label="Fruit"+ searchSource={fruitSource}+ value={fruits[0]}+ onChange={vi.fn()}+ onChangeQuery={onChangeQuery}+ hasClear+ />,+ );++ fireEvent.click(screen.getByRole('button', {name: 'Clear selection'}));++ expect(onChangeQuery).not.toHaveBeenCalled();+ });+it('restores token on blur without action', async () => {const onChange = vi.fn();render(diff --git a/packages/core/src/Typeahead/Typeahead.tsx b/packages/core/src/Typeahead/Typeahead.tsxindex 131dda98fd..a33912c4b3 100644--- a/packages/core/src/Typeahead/Typeahead.tsx+++ b/packages/core/src/Typeahead/Typeahead.tsx@@ -519,12 +519,25 @@ export function Typeahead<T extends SearchableItem>({[onChange],);- // Handle clear (explicit X button on token)+ // Handle clear (explicit X button on token). Edit mode copied the label into+ // the base's query, and the base only resets that query on a selection, so+ // clearing the value has to empty it the way entering edit mode filled it+ // (#7038): a native set plus a synthetic input event, which reaches the+ // base's own change handler and its onChangeQuery.const handleClear = useCallback(() => {+ const input = inputRef.current;+ if (input && input.value !== '') {+ const nativeInputValueSetter = Object.getOwnPropertyDescriptor(+ window.HTMLInputElement.prototype,+ 'value',+ )?.set;+ nativeInputValueSetter?.call(input, '');+ input.dispatchEvent(new Event('input', {bubbles: true}));+ }setIsEditing(false);setEditingValue(null);onChange(null);- inputRef.current?.focus();+ input?.focus();}, [onChange]);// Handle Escape during edit mode — restore token