Skip to content

Commit

Permalink
fix(ComboBox): handle custom value on input blur (#17396)
Browse files Browse the repository at this point in the history
* refactor(ComboBox): remove else-returns

* fix(ComboBox): handle custom value on input blue

---------

Co-authored-by: kennylam <909118+kennylam@users.noreply.github.com>
  • Loading branch information
emyarod and kennylam committed Sep 10, 2024
1 parent f01d544 commit 890d9b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 7 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,13 @@ describe('ComboBox', () => {
expect(findInputNode()).toHaveDisplayValue('Apple');
});

it('should handle InputBlur with allowCustomValue', async () => {
render(<ComboBox {...mockProps} allowCustomValue />);
await userEvent.type(findInputNode(), 'Apple');
fireEvent.blur(findInputNode());
expect(findInputNode()).toHaveDisplayValue('Apple');
});

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

Expand Down
17 changes: 13 additions & 4 deletions packages/react/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,15 @@ const ComboBox = forwardRef(
const { highlightedIndex } = changes;

switch (type) {
case InputBlur:
case InputBlur: {
if (allowCustomValue && highlightedIndex == '-1') {
const customValue = inputValue as ItemType;
changes.selectedItem = customValue;
if (onChange) {
onChange({ selectedItem: inputValue as ItemType, inputValue });
}
return changes;
}
if (
state.inputValue &&
highlightedIndex == '-1' &&
Expand All @@ -502,16 +510,17 @@ const ComboBox = forwardRef(
...changes,
inputValue: itemToString(changes.selectedItem),
};
} else if (
}
if (
state.inputValue &&
highlightedIndex == '-1' &&
!allowCustomValue &&
!changes.selectedItem
) {
return { ...changes, inputValue: '' };
} else {
return changes;
}
return changes;
}
case InputKeyDownEnter:
if (allowCustomValue) {
setInputValue(inputValue);
Expand Down

0 comments on commit 890d9b5

Please sign in to comment.