Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function useSearchAutocomplete<T>(props: AriaSearchAutocompleteProps<T>,
autoComplete: 'off',
onClear: () => {
state.setInputValue('');
state.selectionManager.setSelectedKeys(new Set());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed that onInputChange is being called multiple times with different values when using the Esc key:
image

This seems to be because of useComboBox's Esc key handling which calls state.revert. Even if we clear the selected key here, that state update doesn't happen yet for state.revert which will cause the second onInputChange call. Ideally we'd drop the Esc key handling from useComboBox but keep the rest of it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, we should get a test for this as well

if (onClear) {
onClear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,31 @@ describe('SearchAutocomplete', function () {
expect(items).toHaveLength(1);
expect(searchAutocomplete).not.toHaveAttribute('aria-activedescendant');
});

it('should clear the selected item if searchfield is cleared', function () {
let {getByRole} = renderSearchAutocomplete();

let searchAutocomplete = getByRole('combobox');
typeText(searchAutocomplete, 'one');

let listbox = getByRole('listbox');
let items = within(listbox).getAllByRole('option');
expect(items).toHaveLength(1);
expect(searchAutocomplete).not.toHaveAttribute('aria-activedescendant');

fireEvent.keyDown(searchAutocomplete, {key: 'ArrowDown'});
fireEvent.keyUp(searchAutocomplete, {key: 'ArrowDown'});
fireEvent.keyDown(searchAutocomplete, {key: 'Enter'});
fireEvent.keyUp(searchAutocomplete, {key: 'Enter'});

expect(searchAutocomplete.value).toBe('One');

fireEvent.keyDown(searchAutocomplete, {key: 'Esc'});
fireEvent.keyUp(searchAutocomplete, {key: 'Esc'});

expect(searchAutocomplete.value).toBe('');
expect(onClear).toHaveBeenCalledTimes(1);
});
});

describe('blur', function () {
Expand Down