@@ -1387,6 +1387,71 @@ describe('Keyboard interactions', () => {
1387
1387
assertActiveElement ( getComboboxInput ( ) )
1388
1388
} )
1389
1389
)
1390
+
1391
+ it (
1392
+ 'Static options should allow escape to bubble' ,
1393
+ suppressConsoleLogs ( async ( ) => {
1394
+ render (
1395
+ < Combobox value = "test" onChange = { console . log } >
1396
+ < Combobox . Input onChange = { NOOP } />
1397
+ < Combobox . Button > Trigger</ Combobox . Button >
1398
+ < Combobox . Options static >
1399
+ < Combobox . Option value = "a" > Option A</ Combobox . Option >
1400
+ < Combobox . Option value = "b" > Option B</ Combobox . Option >
1401
+ < Combobox . Option value = "c" > Option C</ Combobox . Option >
1402
+ </ Combobox . Options >
1403
+ </ Combobox >
1404
+ )
1405
+
1406
+ let spy = jest . fn ( )
1407
+
1408
+ window . addEventListener (
1409
+ 'keydown' ,
1410
+ ( evt ) => {
1411
+ if ( evt . key === 'Escape' ) {
1412
+ spy ( )
1413
+ }
1414
+ } ,
1415
+ { capture : true }
1416
+ )
1417
+
1418
+ window . addEventListener ( 'keydown' , ( evt ) => {
1419
+ if ( evt . key === 'Escape' ) {
1420
+ spy ( )
1421
+ }
1422
+ } )
1423
+
1424
+ // Open combobox
1425
+ await click ( getComboboxButton ( ) )
1426
+
1427
+ // Verify it is visible
1428
+ assertComboboxButton ( { state : ComboboxState . Visible } )
1429
+ assertComboboxList ( {
1430
+ state : ComboboxState . Visible ,
1431
+ attributes : { id : 'headlessui-combobox-options-3' } ,
1432
+ } )
1433
+ assertActiveElement ( getComboboxInput ( ) )
1434
+ assertComboboxButtonLinkedWithCombobox ( )
1435
+
1436
+ // Re-focus the button
1437
+ getComboboxButton ( ) ?. focus ( )
1438
+ assertActiveElement ( getComboboxButton ( ) )
1439
+
1440
+ // Close combobox
1441
+ await press ( Keys . Escape )
1442
+
1443
+ // TODO: Verify it is rendered — with static it's not visible or invisible from an assert perspective
1444
+ // assertComboboxButton({ state: ComboboxState.InvisibleUnmounted })
1445
+ // assertComboboxList({ state: ComboboxState.InvisibleUnmounted })
1446
+
1447
+ // Verify the input is focused again
1448
+ assertActiveElement ( getComboboxInput ( ) )
1449
+
1450
+ // The external event handler should've been called twice
1451
+ // Once in the capture phase and once in the bubble phase
1452
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 )
1453
+ } )
1454
+ )
1390
1455
} )
1391
1456
1392
1457
describe ( '`ArrowDown` key' , ( ) => {
@@ -3778,6 +3843,36 @@ describe('Mouse interactions', () => {
3778
3843
} )
3779
3844
)
3780
3845
3846
+ it (
3847
+ 'should be possible to hover an option and make it active when using `static`' ,
3848
+ suppressConsoleLogs ( async ( ) => {
3849
+ render (
3850
+ < Combobox value = "test" onChange = { console . log } >
3851
+ < Combobox . Input onChange = { NOOP } />
3852
+ < Combobox . Button > Trigger</ Combobox . Button >
3853
+ < Combobox . Options static >
3854
+ < Combobox . Option value = "alice" > alice</ Combobox . Option >
3855
+ < Combobox . Option value = "bob" > bob</ Combobox . Option >
3856
+ < Combobox . Option value = "charlie" > charlie</ Combobox . Option >
3857
+ </ Combobox . Options >
3858
+ </ Combobox >
3859
+ )
3860
+
3861
+ let options = getComboboxOptions ( )
3862
+ // We should be able to go to the second option
3863
+ await mouseMove ( options [ 1 ] )
3864
+ assertActiveComboboxOption ( options [ 1 ] )
3865
+
3866
+ // We should be able to go to the first option
3867
+ await mouseMove ( options [ 0 ] )
3868
+ assertActiveComboboxOption ( options [ 0 ] )
3869
+
3870
+ // We should be able to go to the last option
3871
+ await mouseMove ( options [ 2 ] )
3872
+ assertActiveComboboxOption ( options [ 2 ] )
3873
+ } )
3874
+ )
3875
+
3781
3876
it (
3782
3877
'should make a combobox option active when you move the mouse over it' ,
3783
3878
suppressConsoleLogs ( async ( ) => {
@@ -4139,24 +4234,17 @@ describe('Mouse interactions', () => {
4139
4234
)
4140
4235
4141
4236
it (
4142
- 'Combobox preserves the latest known active option after an option becomes inactive ' ,
4237
+ 'should be possible to hold the last active option' ,
4143
4238
suppressConsoleLogs ( async ( ) => {
4144
4239
render (
4145
- < Combobox value = "test" onChange = { console . log } >
4146
- { ( { open, latestActiveOption } ) => (
4147
- < >
4148
- < Combobox . Input onChange = { NOOP } />
4149
- < Combobox . Button > Trigger</ Combobox . Button >
4150
- < div id = "latestActiveOption" > { latestActiveOption } </ div >
4151
- { open && (
4152
- < Combobox . Options >
4153
- < Combobox . Option value = "a" > Option A</ Combobox . Option >
4154
- < Combobox . Option value = "b" > Option B</ Combobox . Option >
4155
- < Combobox . Option value = "c" > Option C</ Combobox . Option >
4156
- </ Combobox . Options >
4157
- ) }
4158
- </ >
4159
- ) }
4240
+ < Combobox value = "test" onChange = { console . log } hold >
4241
+ < Combobox . Input onChange = { NOOP } />
4242
+ < Combobox . Button > Trigger</ Combobox . Button >
4243
+ < Combobox . Options >
4244
+ < Combobox . Option value = "a" > Option A</ Combobox . Option >
4245
+ < Combobox . Option value = "b" > Option B</ Combobox . Option >
4246
+ < Combobox . Option value = "c" > Option C</ Combobox . Option >
4247
+ </ Combobox . Options >
4160
4248
</ Combobox >
4161
4249
)
4162
4250
@@ -4181,24 +4269,19 @@ describe('Mouse interactions', () => {
4181
4269
4182
4270
// Verify that the first combobox option is active
4183
4271
assertActiveComboboxOption ( options [ 0 ] )
4184
- expect ( document . getElementById ( 'latestActiveOption' ) ! . textContent ) . toBe ( 'a' )
4185
4272
4186
4273
// Focus the second item
4187
4274
await mouseMove ( options [ 1 ] )
4188
4275
4189
4276
// Verify that the second combobox option is active
4190
4277
assertActiveComboboxOption ( options [ 1 ] )
4191
- expect ( document . getElementById ( 'latestActiveOption' ) ! . textContent ) . toBe ( 'b' )
4192
4278
4193
4279
// Move the mouse off of the second combobox option
4194
4280
await mouseLeave ( options [ 1 ] )
4195
4281
await mouseMove ( document . body )
4196
4282
4197
- // Verify that the second combobox option is NOT active
4198
- assertNoActiveComboboxOption ( )
4199
-
4200
- // But the last known active option is still recorded
4201
- expect ( document . getElementById ( 'latestActiveOption' ) ! . textContent ) . toBe ( 'b' )
4283
+ // Verify that the second combobox option is still active
4284
+ assertActiveComboboxOption ( options [ 1 ] )
4202
4285
} )
4203
4286
)
4204
4287
} )
0 commit comments