@@ -1551,7 +1551,7 @@ test.each([
1551
1551
} ,
1552
1552
)
1553
1553
1554
- test . each ( [ true , false , `desktop ` ] as const ) (
1554
+ test . each ( [ true , false , `if-mobile ` ] as const ) (
1555
1555
`closeDropdownOnSelect=%s controls input focus and dropdown closing` ,
1556
1556
async ( closeDropdownOnSelect ) => {
1557
1557
window . innerWidth = 600 // simulate mobile
@@ -1568,7 +1568,7 @@ test.each([true, false, `desktop`] as const)(
1568
1568
const is_desktop = window . innerWidth > select . breakpoint
1569
1569
const should_be_closed =
1570
1570
closeDropdownOnSelect === true ||
1571
- ( closeDropdownOnSelect === `desktop ` && ! is_desktop )
1571
+ ( closeDropdownOnSelect === `if-mobile ` && ! is_desktop )
1572
1572
1573
1573
// count number of selected items
1574
1574
const selected_items = document . querySelectorAll ( `ul.selected > li` )
@@ -1593,7 +1593,7 @@ test.each([true, false, `desktop`] as const)(
1593
1593
expect ( document . activeElement === input_el ) . toBe ( ! should_be_closed )
1594
1594
}
1595
1595
1596
- if ( closeDropdownOnSelect === `desktop ` ) {
1596
+ if ( closeDropdownOnSelect === `if-mobile ` ) {
1597
1597
// reduce window width to simulate mobile
1598
1598
window . innerWidth = 400
1599
1599
window . dispatchEvent ( new Event ( `resize` ) )
@@ -1605,7 +1605,7 @@ test.each([true, false, `desktop`] as const)(
1605
1605
) as HTMLLIElement
1606
1606
if ( another_option ) {
1607
1607
another_option . click ( )
1608
- // On mobile (when closeDropdownOnSelect = 'desktop '), dropdown should close, input should lose focus
1608
+ // On mobile (when closeDropdownOnSelect = 'if-mobile '), dropdown should close, input should lose focus
1609
1609
expect ( dropdown . classList ) . toContain ( `hidden` ) // Now it should be closed
1610
1610
expect ( document . activeElement === input_el ) . toBe ( false )
1611
1611
} else {
@@ -1616,3 +1616,78 @@ test.each([true, false, `desktop`] as const)(
1616
1616
}
1617
1617
} ,
1618
1618
)
1619
+
1620
+ test ( `closeDropdownOnSelect='retain-focus' retains input focus when dropdown closes after option selection` , async ( ) => {
1621
+ mount ( MultiSelect , {
1622
+ target : document . body ,
1623
+ props : {
1624
+ options : [ 1 , 2 , 3 ] ,
1625
+ closeDropdownOnSelect : `retain-focus` ,
1626
+ open : true ,
1627
+ } ,
1628
+ } )
1629
+
1630
+ const input_el = doc_query < HTMLInputElement > ( `input[autocomplete]` )
1631
+ input_el . focus ( )
1632
+ await tick ( )
1633
+
1634
+ // select an option - should close dropdown but retain focus
1635
+ doc_query ( `ul.options > li` ) . click ( )
1636
+ await tick ( )
1637
+
1638
+ expect ( document . activeElement ) . toBe ( input_el )
1639
+ expect ( document . querySelectorAll ( `ul.selected > li` ) ) . toHaveLength ( 1 )
1640
+ } )
1641
+
1642
+ test ( `closeDropdownOnSelect='retain-focus' works correctly with maxSelect` , async ( ) => {
1643
+ mount ( MultiSelect , {
1644
+ target : document . body ,
1645
+ props : {
1646
+ options : [ 1 , 2 , 3 ] ,
1647
+ closeDropdownOnSelect : `retain-focus` ,
1648
+ maxSelect : 2 ,
1649
+ open : true ,
1650
+ } ,
1651
+ } )
1652
+
1653
+ const input_el = doc_query < HTMLInputElement > ( `input[autocomplete]` )
1654
+ input_el . focus ( )
1655
+ await tick ( )
1656
+
1657
+ // select first option
1658
+ doc_query ( `ul.options > li` ) . click ( )
1659
+ await tick ( )
1660
+ expect ( document . activeElement ) . toBe ( input_el )
1661
+
1662
+ // select second option (reaching maxSelect)
1663
+ input_el . dispatchEvent ( new MouseEvent ( `mouseup` , { bubbles : true } ) )
1664
+ await tick ( )
1665
+ doc_query ( `ul.options > li` ) . click ( )
1666
+ await tick ( )
1667
+
1668
+ expect ( document . activeElement ) . toBe ( input_el )
1669
+ expect ( document . querySelectorAll ( `ul.selected > li` ) ) . toHaveLength ( 2 )
1670
+ } )
1671
+
1672
+ test ( `Escape and Tab still blur input even with closeDropdownOnSelect='retain-focus'` , async ( ) => {
1673
+ mount ( MultiSelect , {
1674
+ target : document . body ,
1675
+ props : {
1676
+ options : [ 1 , 2 , 3 ] ,
1677
+ closeDropdownOnSelect : `retain-focus` ,
1678
+ open : true ,
1679
+ } ,
1680
+ } )
1681
+
1682
+ const input_el = doc_query < HTMLInputElement > ( `input[autocomplete]` )
1683
+ input_el . focus ( )
1684
+ await tick ( )
1685
+
1686
+ // Escape should blur input (retain-focus only applies to selection, not keyboard closing)
1687
+ input_el . dispatchEvent (
1688
+ new KeyboardEvent ( `keydown` , { key : `Escape` , bubbles : true } ) ,
1689
+ )
1690
+ await tick ( )
1691
+
1692
+ expect ( document . activeElement ) . not . toBe ( input_el )
1693
+ } )
0 commit comments