diff --git a/src/lib/autocomplete/autocomplete-trigger.ts b/src/lib/autocomplete/autocomplete-trigger.ts index 1b773673e843..34f3ee091403 100644 --- a/src/lib/autocomplete/autocomplete-trigger.ts +++ b/src/lib/autocomplete/autocomplete-trigger.ts @@ -121,6 +121,9 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy { openPanel(): void { if (!this._overlayRef) { this._createOverlay(); + } else { + /** Update the panel width, in case the host width has changed */ + this._overlayRef.getState().width = this._getHostWidth(); } if (!this._overlayRef.hasAttached()) { diff --git a/src/lib/autocomplete/autocomplete.spec.ts b/src/lib/autocomplete/autocomplete.spec.ts index 78eb4a4b118c..0456a71acbd8 100644 --- a/src/lib/autocomplete/autocomplete.spec.ts +++ b/src/lib/autocomplete/autocomplete.spec.ts @@ -938,11 +938,37 @@ describe('MdAutocomplete', () => { })); }); + + it('should have correct width when opened', () => { + const widthFixture = TestBed.createComponent(SimpleAutocomplete); + widthFixture.componentInstance.width = 300; + widthFixture.detectChanges(); + + widthFixture.componentInstance.trigger.openPanel(); + widthFixture.detectChanges(); + + const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement; + // Firefox, edge return a decimal value for width, so we need to parse and round it to verify + expect(Math.ceil(parseFloat(overlayPane.style.width))).toEqual(300); + + widthFixture.componentInstance.trigger.closePanel(); + widthFixture.detectChanges(); + + widthFixture.componentInstance.width = 500; + widthFixture.detectChanges(); + + widthFixture.componentInstance.trigger.openPanel(); + widthFixture.detectChanges(); + + // Firefox, edge return a decimal value for width, so we need to parse and round it to verify + expect(Math.ceil(parseFloat(overlayPane.style.width))).toEqual(500); + + }); }); @Component({ template: ` - + @@ -958,6 +984,7 @@ class SimpleAutocomplete implements OnDestroy { filteredStates: any[]; valueSub: Subscription; placeholder = 'auto'; + width: number; @ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger; @ViewChild(MdAutocomplete) panel: MdAutocomplete;