66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import {
10- Combobox ,
11- ComboboxInput ,
12- ComboboxPopup ,
13- ComboboxPopupContainer ,
14- } from '@angular/aria/combobox' ;
9+ import { Combobox , ComboboxPopup , ComboboxWidget } from '@angular/aria/simple-combobox' ;
1510import { Listbox , Option } from '@angular/aria/listbox' ;
1611import {
1712 afterRenderEffect ,
1813 ChangeDetectionStrategy ,
1914 Component ,
2015 computed ,
16+ signal ,
2117 viewChild ,
22- viewChildren ,
2318} from '@angular/core' ;
2419import { COUNTRIES } from '../countries' ;
2520import { OverlayModule } from '@angular/cdk/overlay' ;
@@ -30,60 +25,52 @@ import {FormsModule} from '@angular/forms';
3025 selector : 'autocomplete-auto-select-example' ,
3126 templateUrl : 'autocomplete-auto-select-example.html' ,
3227 styleUrl : '../autocomplete.css' ,
33- imports : [
34- Combobox ,
35- ComboboxInput ,
36- ComboboxPopup ,
37- ComboboxPopupContainer ,
38- Listbox ,
39- Option ,
40- OverlayModule ,
41- FormsModule ,
42- ] ,
28+ imports : [ Combobox , ComboboxPopup , ComboboxWidget , Listbox , Option , OverlayModule , FormsModule ] ,
4329 changeDetection : ChangeDetectionStrategy . OnPush ,
4430} )
4531export class AutocompleteAutoSelectExample {
4632 /** The selected value of the 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 ) ;
33+ readonly listbox = viewChild ( Listbox ) ;
34+ readonly combobox = viewChild ( Combobox ) ;
5435
55- /** A reference to the ng aria combobox input. */
56- comboboxInput = viewChild < ComboboxInput > ( ComboboxInput ) ;
36+ popupExpanded = signal ( false ) ;
37+ searchString = signal ( '' ) ;
38+ selectedOption = signal < string [ ] > ( [ ] ) ;
5739
5840 /** The query string used to filter the list of countries. */
59- query = computed ( ( ) => this . comboboxInput ( ) ?. value ( ) || '' ) ;
41+ query = computed ( ( ) => this . searchString ( ) ) ;
6042
6143 /** The list of countries filtered by the query. */
6244 countries = computed ( ( ) =>
6345 COUNTRIES . filter ( country => country . toLowerCase ( ) . startsWith ( this . query ( ) . toLowerCase ( ) ) ) ,
6446 ) ;
6547
6648 constructor ( ) {
67- // Scrolls to the active item when the active option changes.
6849 afterRenderEffect ( ( ) => {
69- if ( this . combobox ( ) ?. expanded ( ) ) {
70- const option = this . options ( ) . find ( opt => opt . active ( ) ) ;
71- option ?. element . scrollIntoView ( { block : 'nearest' } ) ;
72- }
50+ this . listbox ( ) ?. scrollActiveItemIntoView ( ) ;
7351 } ) ;
7452 }
7553
7654 /** Clears the query and the listbox value. */
7755 clear ( ) : void {
78- this . comboboxInput ( ) ?. value . set ( '' ) ;
79- this . listbox ?.( ) ?. value . set ( [ ] ) ;
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 ( ) ;
8067 }
8168
8269 /** Handles keydown events on the clear button. */
8370 onKeydown ( event : KeyboardEvent ) : void {
8471 if ( event . key === 'Enter' ) {
8572 this . clear ( ) ;
86- this . combobox ?. ( ) ?. close ( ) ;
73+ this . popupExpanded . set ( false ) ;
8774 event . stopPropagation ( ) ;
8875 }
8976 }
0 commit comments