File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
@react-spectrum/combobox/test
@react-stately/combobox/src Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -1463,6 +1463,35 @@ describe('ComboBox', function () {
1463
1463
expect ( ( ) => getByRole ( 'listbox' ) ) . toThrow ( ) ;
1464
1464
} ) ;
1465
1465
1466
+ it ( 'retains selected key on blur if input value matches' , function ( ) {
1467
+ let { getByRole} = render (
1468
+ < Provider theme = { theme } >
1469
+ < ComboBox label = "Test" allowsCustomValue selectedKey = "2" onOpenChange = { onOpenChange } onSelectionChange = { onSelectionChange } >
1470
+ < Item key = "1" > Bulbasaur</ Item >
1471
+ < Item key = "2" > Squirtle</ Item >
1472
+ < Item key = "3" > Charmander</ Item >
1473
+ </ ComboBox >
1474
+ </ Provider >
1475
+ ) ;
1476
+
1477
+ let combobox = getByRole ( 'combobox' ) ;
1478
+ expect ( onSelectionChange ) . not . toHaveBeenCalled ( ) ;
1479
+
1480
+ act ( ( ) => {
1481
+ userEvent . click ( combobox ) ;
1482
+ jest . runAllTimers ( ) ;
1483
+ } ) ;
1484
+
1485
+ expect ( document . activeElement ) . toBe ( combobox ) ;
1486
+
1487
+ act ( ( ) => {
1488
+ combobox . blur ( ) ;
1489
+ jest . runAllTimers ( ) ;
1490
+ } ) ;
1491
+
1492
+ expect ( onSelectionChange ) . not . toHaveBeenCalled ( ) ;
1493
+ } ) ;
1494
+
1466
1495
it ( 'clears the input field if value doesn\'t match a combobox option and no item is focused (menuTrigger=manual case)' , function ( ) {
1467
1496
let { getByRole, getAllByRole} = render (
1468
1497
< Provider theme = { theme } >
Original file line number Diff line number Diff line change @@ -217,7 +217,8 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateProps<T>)
217
217
open ( ) ;
218
218
}
219
219
} else if ( shouldCloseOnBlur ) {
220
- if ( allowsCustomValue ) {
220
+ let itemText = collection . getItem ( selectedKey ) ?. textValue ?? '' ;
221
+ if ( allowsCustomValue && inputValue !== itemText ) {
221
222
commitCustomValue ( ) ;
222
223
} else {
223
224
resetInputValue ( ) ;
You can’t perform that action at this time.
0 commit comments