Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Combobox): fix issue where autocomplete automatic does not fire selection event #1493

Merged
merged 2 commits into from
Jun 5, 2024
Merged
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
30 changes: 30 additions & 0 deletions packages/react/src/components/Combobox/Combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,36 @@ test('should handle selection with "enter" keydown event', () => {
fireEnterKeyPress();
});

test('should handle selection when autocomplete="automatic" and combobox input is blurred', () => {
const onSelectionChange = spy();
render(
<Combobox
label="label"
onSelectionChange={onSelectionChange}
value=""
autocomplete="automatic"
>
<ComboboxOption>Apple</ComboboxOption>
<ComboboxOption>Banana</ComboboxOption>
<ComboboxOption>Cantaloupe</ComboboxOption>
</Combobox>
);
const combobox = screen.getByRole('combobox');

// Note: Combobox forwards events to Listbox via dispatchEvent, but this doesn't
// work correctly within jsdom/enzyme so we fire the events directly on listbox
const fireArrowDownKeyPress = () =>
fireEvent.keyDown(screen.getByRole('listbox'), { key: 'ArrowDown' });

fireEvent.focus(combobox);
assertListboxIsOpen(true);
expect(onSelectionChange.notCalled).toBeTruthy();
fireArrowDownKeyPress();
fireEvent.blur(combobox);
expect(onSelectionChange.calledOnce).toBeTruthy();
expect(onSelectionChange.firstCall.firstArg.value).toEqual('Banana');
});

test('should always render all options when autocomplete="none"', () => {
render(
<Combobox label="label" autocomplete="none">
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/components/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ const Combobox = forwardRef<HTMLDivElement, ComboboxProps>(
/* istanbul ignore next: default value */ '';
setValue(stringValue);
setSelectedValue(stringValue);
onSelectionChange?.({
target: activeDescendant.element,
value: stringValue,
previousValue: value
});
}
},
[autocomplete, activeDescendant, onBlur]
Expand Down
Loading