Skip to content

Commit 8b69ead

Browse files
authored
fix: added condition to clear input when closed (#18544)
1 parent ec82b57 commit 8b69ead

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/react/src/components/ComboBox/ComboBox-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,25 @@ describe('ComboBox', () => {
622622
);
623623
});
624624

625+
it('should clear input when closing with chevron if input does not match any item and allowCustomValue is false', async () => {
626+
render(<ComboBox {...mockProps} allowCustomValue={false} />);
627+
628+
// First type something that doesn't match any item
629+
await userEvent.type(findInputNode(), 'xyz');
630+
631+
// Menu should be open at this point
632+
assertMenuOpen(mockProps);
633+
634+
// Click the chevron/toggle button to close
635+
await userEvent.click(screen.getByRole('button', { name: 'Close' }));
636+
637+
// Menu should be closed
638+
assertMenuClosed();
639+
640+
// Input should be cleared
641+
expect(findInputNode()).toHaveDisplayValue('');
642+
});
643+
625644
it('should pass defined selectedItem to onChange when item is selected', async () => {
626645
render(<ComboBox {...mockProps} />);
627646

packages/react/src/components/ComboBox/ComboBox.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,17 @@ const ComboBox = forwardRef(
625625
}
626626
case FunctionToggleMenu:
627627
case ToggleButtonClick:
628+
if (
629+
!changes.isOpen &&
630+
state.inputValue &&
631+
highlightedIndex === -1 &&
632+
!allowCustomValue
633+
) {
634+
return {
635+
...changes,
636+
inputValue: '', // Clear the input
637+
};
638+
}
628639
if (changes.isOpen && !changes.selectedItem) {
629640
return { ...changes };
630641
}

0 commit comments

Comments
 (0)