Skip to content

Commit

Permalink
fix(Combobox): fix issue where autocomplete automatic does not fire s…
Browse files Browse the repository at this point in the history
…election event (#1493)
  • Loading branch information
scurker authored Jun 5, 2024
1 parent 0408555 commit 0f061da
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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

0 comments on commit 0f061da

Please sign in to comment.