@@ -1551,7 +1551,7 @@ test.each([
15511551 } ,
15521552)
15531553
1554- test . each ( [ true , false , `desktop ` ] as const ) (
1554+ test . each ( [ true , false , `if-mobile ` ] as const ) (
15551555 `closeDropdownOnSelect=%s controls input focus and dropdown closing` ,
15561556 async ( closeDropdownOnSelect ) => {
15571557 window . innerWidth = 600 // simulate mobile
@@ -1568,7 +1568,7 @@ test.each([true, false, `desktop`] as const)(
15681568 const is_desktop = window . innerWidth > select . breakpoint
15691569 const should_be_closed =
15701570 closeDropdownOnSelect === true ||
1571- ( closeDropdownOnSelect === `desktop ` && ! is_desktop )
1571+ ( closeDropdownOnSelect === `if-mobile ` && ! is_desktop )
15721572
15731573 // count number of selected items
15741574 const selected_items = document . querySelectorAll ( `ul.selected > li` )
@@ -1593,7 +1593,7 @@ test.each([true, false, `desktop`] as const)(
15931593 expect ( document . activeElement === input_el ) . toBe ( ! should_be_closed )
15941594 }
15951595
1596- if ( closeDropdownOnSelect === `desktop ` ) {
1596+ if ( closeDropdownOnSelect === `if-mobile ` ) {
15971597 // reduce window width to simulate mobile
15981598 window . innerWidth = 400
15991599 window . dispatchEvent ( new Event ( `resize` ) )
@@ -1605,7 +1605,7 @@ test.each([true, false, `desktop`] as const)(
16051605 ) as HTMLLIElement
16061606 if ( another_option ) {
16071607 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
16091609 expect ( dropdown . classList ) . toContain ( `hidden` ) // Now it should be closed
16101610 expect ( document . activeElement === input_el ) . toBe ( false )
16111611 } else {
@@ -1616,3 +1616,78 @@ test.each([true, false, `desktop`] as const)(
16161616 }
16171617 } ,
16181618)
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