@@ -1617,6 +1617,36 @@ describe('MatAutocomplete', () => {
1617
1617
} ) ;
1618
1618
} ) ;
1619
1619
1620
+ describe ( 'form control with initial value' , ( ) => {
1621
+ let fixture : ComponentFixture < FormControlWithInitialValue > ;
1622
+ let input : HTMLInputElement ;
1623
+
1624
+ beforeEach ( waitForAsync ( async ( ) => {
1625
+ fixture = createComponent ( FormControlWithInitialValue ) ;
1626
+ fixture . detectChanges ( ) ;
1627
+
1628
+ input = fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement ;
1629
+
1630
+ fixture . componentInstance . trigger . openPanel ( ) ;
1631
+ fixture . detectChanges ( ) ;
1632
+ await new Promise ( r => setTimeout ( r ) ) ;
1633
+ } ) ) ;
1634
+
1635
+ it ( 'should mark the initial value as selected if its present' , fakeAsync ( ( ) => {
1636
+ const trigger = fixture . componentInstance . trigger ;
1637
+ trigger . openPanel ( ) ;
1638
+ fixture . detectChanges ( ) ;
1639
+
1640
+ const options = overlayContainerElement . querySelectorAll (
1641
+ 'mat-option' ,
1642
+ ) as NodeListOf < HTMLElement > ;
1643
+
1644
+ expect ( input . value ) . toBe ( 'Three' ) ;
1645
+ expect ( options . length ) . toBe ( 3 ) ;
1646
+ expect ( options [ 2 ] . classList ) . toContain ( 'mdc-list-item--selected' ) ;
1647
+ } ) ) ;
1648
+ } ) ;
1649
+
1620
1650
describe ( 'option groups' , ( ) => {
1621
1651
let DOWN_ARROW_EVENT : KeyboardEvent ;
1622
1652
let UP_ARROW_EVENT : KeyboardEvent ;
@@ -4375,6 +4405,31 @@ class PlainAutocompleteInputWithFormControl {
4375
4405
formControl = new FormControl ( '' ) ;
4376
4406
}
4377
4407
4408
+ @Component ( {
4409
+ template : `
4410
+ <mat-form-field>
4411
+ <input matInput placeholder="Choose" [matAutocomplete]="auto" [formControl]="optionCtrl">
4412
+ </mat-form-field>
4413
+ <mat-autocomplete #auto="matAutocomplete" >
4414
+ @for (option of options; track option) {
4415
+ <mat-option [value]="option">
4416
+ {{option}}
4417
+ </mat-option>
4418
+ }
4419
+ </mat-autocomplete>
4420
+ ` ,
4421
+ standalone : false ,
4422
+ } )
4423
+ class FormControlWithInitialValue {
4424
+ optionCtrl = new FormControl ( 'Three' ) ;
4425
+ filteredOptions : Observable < any > ;
4426
+ options = [ 'One' , 'Two' , 'Three' ] ;
4427
+
4428
+ @ViewChild ( MatAutocompleteTrigger ) trigger : MatAutocompleteTrigger ;
4429
+
4430
+ constructor ( ) { }
4431
+ }
4432
+
4378
4433
@Component ( {
4379
4434
template : `
4380
4435
<mat-form-field>
0 commit comments