File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
packages/react/src/components/ComboBox Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments