66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { Combobox , ComboboxPopup , ComboboxWidget } from '@angular/aria/simple-combobox' ;
9+ import {
10+ Combobox ,
11+ ComboboxInput ,
12+ ComboboxPopup ,
13+ ComboboxPopupContainer ,
14+ } from '@angular/aria/combobox' ;
1015import { Listbox , Option } from '@angular/aria/listbox' ;
1116import {
1217 afterRenderEffect ,
1318 ChangeDetectionStrategy ,
1419 Component ,
1520 computed ,
16- signal ,
1721 viewChild ,
22+ viewChildren ,
1823} from '@angular/core' ;
1924import { COUNTRIES } from '../countries' ;
2025import { OverlayModule } from '@angular/cdk/overlay' ;
@@ -25,52 +30,60 @@ import {FormsModule} from '@angular/forms';
2530 selector : 'autocomplete-auto-select-example' ,
2631 templateUrl : 'autocomplete-auto-select-example.html' ,
2732 styleUrl : '../autocomplete.css' ,
28- imports : [ Combobox , ComboboxPopup , ComboboxWidget , Listbox , Option , OverlayModule , FormsModule ] ,
33+ imports : [
34+ Combobox ,
35+ ComboboxInput ,
36+ ComboboxPopup ,
37+ ComboboxPopupContainer ,
38+ Listbox ,
39+ Option ,
40+ OverlayModule ,
41+ FormsModule ,
42+ ] ,
2943 changeDetection : ChangeDetectionStrategy . OnPush ,
3044} )
3145export class AutocompleteAutoSelectExample {
3246 /** The selected value of the combobox. */
33- readonly listbox = viewChild ( Listbox ) ;
34- readonly combobox = viewChild ( Combobox ) ;
47+ listbox = viewChild < Listbox < string > > ( Listbox ) ;
48+
49+ /** The options available in the listbox. */
50+ options = viewChildren < Option < string > > ( Option ) ;
51+
52+ /** A reference to the ng aria combobox. */
53+ combobox = viewChild < Combobox < string > > ( Combobox ) ;
3554
36- popupExpanded = signal ( false ) ;
37- searchString = signal ( '' ) ;
38- selectedOption = signal < string [ ] > ( [ ] ) ;
55+ /** A reference to the ng aria combobox input. */
56+ comboboxInput = viewChild < ComboboxInput > ( ComboboxInput ) ;
3957
4058 /** The query string used to filter the list of countries. */
41- query = computed ( ( ) => this . searchString ( ) ) ;
59+ query = computed ( ( ) => this . comboboxInput ( ) ?. value ( ) || '' ) ;
4260
4361 /** The list of countries filtered by the query. */
4462 countries = computed ( ( ) =>
4563 COUNTRIES . filter ( country => country . toLowerCase ( ) . startsWith ( this . query ( ) . toLowerCase ( ) ) ) ,
4664 ) ;
4765
4866 constructor ( ) {
67+ // Scrolls to the active item when the active option changes.
4968 afterRenderEffect ( ( ) => {
50- this . listbox ( ) ?. scrollActiveItemIntoView ( ) ;
69+ if ( this . combobox ( ) ?. expanded ( ) ) {
70+ const option = this . options ( ) . find ( opt => opt . active ( ) ) ;
71+ option ?. element . scrollIntoView ( { block : 'nearest' } ) ;
72+ }
5173 } ) ;
5274 }
5375
5476 /** Clears the query and the listbox value. */
5577 clear ( ) : void {
56- this . searchString . set ( '' ) ;
57- this . selectedOption . set ( [ ] ) ;
58- }
59-
60- onCommit ( ) {
61- const selectedOption = this . selectedOption ( ) ;
62- if ( selectedOption . length > 0 ) {
63- this . searchString . set ( selectedOption [ 0 ] ) ;
64- }
65- this . popupExpanded . set ( false ) ;
66- this . combobox ( ) ?. element . focus ( ) ;
78+ this . comboboxInput ( ) ?. value . set ( '' ) ;
79+ this . listbox ?.( ) ?. value . set ( [ ] ) ;
6780 }
6881
6982 /** Handles keydown events on the clear button. */
7083 onKeydown ( event : KeyboardEvent ) : void {
7184 if ( event . key === 'Enter' ) {
7285 this . clear ( ) ;
73- this . popupExpanded . set ( false ) ;
86+ this . combobox ?. ( ) ?. close ( ) ;
7487 event . stopPropagation ( ) ;
7588 }
7689 }
0 commit comments