Skip to content

Commit

Permalink
16624 allowcustomvalue not fire onchange event (#16743)
Browse files Browse the repository at this point in the history
* fix: combobox not showing correct highlight

* fix: removed console

* fix: allowcustomvalue not fire onchange event

---------

Co-authored-by: Nikhil Tomar <[email protected]>
  • Loading branch information
preetibansalui and 2nikhiltom committed Jul 11, 2024
1 parent f7f4baa commit e8c6ae5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/react/src/components/ComboBox/ComboBox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ describe('ComboBox', () => {
expect(findInputNode()).toHaveDisplayValue('Apple');
});

it('should apply onChange value if custom value is entered and `allowCustomValue` is set', async () => {
render(<ComboBox {...mockProps} allowCustomValue />);

expect(findInputNode()).toHaveDisplayValue('');

await userEvent.type(findInputNode(), 'Apple');
await userEvent.keyboard('[Enter]');
assertMenuClosed();
expect(mockProps.onChange).toHaveBeenCalledWith({
inputValue: 'Apple',
selectedItem: null,
});
});

it('should respect slug prop', async () => {
const { container } = render(<ComboBox {...mockProps} slug={<Slug />} />);
await waitForPosition();
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ const ComboBox = forwardRef(
setInputValue(inputValue);
setHighlightedIndex(changes.selectedItem);
if (onChange) {
onChange({ selectedItem: changes.selectedItem });
onChange({ selectedItem: changes.selectedItem, inputValue });
}
return changes;
} else if (changes.selectedItem && !allowCustomValue) {
Expand Down Expand Up @@ -725,6 +725,13 @@ const ComboBox = forwardRef(
);
}

// Since `onChange` does not normally fire when the menu is closed, we should
// manually fire it when `allowCustomValue` is provided, the menu is closing,
// and there is a value.
if (allowCustomValue && isOpen && inputValue) {
onChange({ selectedItem, inputValue });
}

event.preventDownshiftDefault = true;
event?.persist?.();
}
Expand Down

0 comments on commit e8c6ae5

Please sign in to comment.