@@ -529,6 +529,96 @@ describe('Combobox', () => {
529529 expect ( screen . getByRole ( 'option' , { name : 'Banana' } ) ) . toHaveAttribute ( 'data-selected' ) ;
530530 } ) ;
531531
532+ describe ( 'active option reference while filtering a selection' , ( ) => {
533+ function FilteredPicker ( ) {
534+ const [ open , setOpen ] = useState ( false ) ;
535+ const [ value , setValue ] = useState < string | null > ( 'apple' ) ;
536+ return (
537+ < Combobox . Root
538+ inline
539+ open = { open }
540+ onOpenChange = { setOpen }
541+ value = { value }
542+ onValueChange = { setValue }
543+ >
544+ < button
545+ type = 'button'
546+ onClick = { ( ) => setOpen ( ! open ) }
547+ >
548+ { value }
549+ </ button >
550+ { open && (
551+ < >
552+ < Combobox . Input aria-label = 'Search fruit' />
553+ < Combobox . List >
554+ < Combobox . Collection
555+ items = { fruits }
556+ itemToStringLabel = { fruit => fruit . label }
557+ >
558+ { fruit => (
559+ < Combobox . Option
560+ key = { fruit . value }
561+ value = { fruit . value }
562+ label = { fruit . label }
563+ >
564+ { fruit . label }
565+ </ Combobox . Option >
566+ ) }
567+ </ Combobox . Collection >
568+ </ Combobox . List >
569+ </ >
570+ ) }
571+ </ Combobox . Root >
572+ ) ;
573+ }
574+
575+ function expectActiveOption ( name : string ) {
576+ const reference = screen . getByRole ( 'combobox' ) . getAttribute ( 'aria-activedescendant' ) ;
577+ expect ( reference === null ? null : document . getElementById ( reference ) ) . toBe (
578+ screen . getByRole ( 'option' , { name } ) ,
579+ ) ;
580+ }
581+
582+ it ( 'references the filtered option the input highlights, then selects it' , async ( ) => {
583+ const user = userEvent . setup ( ) ;
584+ render ( < FilteredPicker /> ) ;
585+ await user . click ( screen . getByRole ( 'button' , { name : 'apple' } ) ) ;
586+ expectActiveOption ( 'Apple' ) ;
587+
588+ await user . type ( screen . getByRole ( 'combobox' ) , 'Ban' ) ;
589+ expect ( screen . getAllByRole ( 'option' ) ) . toHaveLength ( 1 ) ;
590+ expectActiveOption ( 'Banana' ) ;
591+
592+ await user . keyboard ( '{ArrowDown}' ) ;
593+ expectActiveOption ( 'Banana' ) ;
594+ await user . keyboard ( '{ArrowDown}' ) ;
595+ expectActiveOption ( 'Banana' ) ;
596+
597+ await user . keyboard ( '{Enter}' ) ;
598+ expect ( screen . getByRole ( 'button' , { name : 'banana' } ) ) . toBeInTheDocument ( ) ;
599+ expect ( screen . queryByRole ( 'combobox' ) ) . not . toBeInTheDocument ( ) ;
600+ } ) ;
601+
602+ it ( 'drops the reference when filtering leaves no options, and references the selection on reopen' , async ( ) => {
603+ const user = userEvent . setup ( ) ;
604+ render ( < FilteredPicker /> ) ;
605+ await user . click ( screen . getByRole ( 'button' , { name : 'apple' } ) ) ;
606+ await user . type ( screen . getByRole ( 'combobox' ) , 'Ban' ) ;
607+ expectActiveOption ( 'Banana' ) ;
608+
609+ await user . type ( screen . getByRole ( 'combobox' ) , 'x' ) ;
610+ expect ( screen . queryAllByRole ( 'option' ) ) . toHaveLength ( 0 ) ;
611+ expect ( screen . getByRole ( 'combobox' ) ) . not . toHaveAttribute ( 'aria-activedescendant' ) ;
612+ await user . keyboard ( '{ArrowDown}' ) ;
613+ expect ( screen . getByRole ( 'combobox' ) ) . not . toHaveAttribute ( 'aria-activedescendant' ) ;
614+
615+ await user . click ( screen . getByRole ( 'button' , { name : 'apple' } ) ) ;
616+ await user . click ( screen . getByRole ( 'button' , { name : 'apple' } ) ) ;
617+ expect ( screen . getByRole ( 'combobox' ) ) . toHaveValue ( '' ) ;
618+ expectActiveOption ( 'Apple' ) ;
619+ } ) ;
620+ } ) ;
621+
532622 it . each ( [ true , false ] ) ( 'respects a controlled clearing request when accepted=%s' , async accept => {
533623 const user = userEvent . setup ( ) ;
534624 const onValueChange = vi . fn ( ) ;
0 commit comments