From 497602d957ed218766c5b2a28c5f7878eaacd490 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Mon, 2 Apr 2018 18:28:20 +0200 Subject: [PATCH] fix(autocomplete): reposition the panel when the amount of options changes Fixes the autocomplete panel not being repositioned when the amount of options changes. This is necessary, because adding extra options could push it out of the viewport. --- src/lib/autocomplete/autocomplete-trigger.ts | 5 +++ src/lib/autocomplete/autocomplete.spec.ts | 33 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/lib/autocomplete/autocomplete-trigger.ts b/src/lib/autocomplete/autocomplete-trigger.ts index 8ccea2c91341..f02d210c8b1b 100644 --- a/src/lib/autocomplete/autocomplete-trigger.ts +++ b/src/lib/autocomplete/autocomplete-trigger.ts @@ -410,6 +410,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy { switchMap(() => { this._resetActiveItem(); this.autocomplete._setVisibility(); + + if (this.panelOpen) { + this._overlayRef!.updatePosition(); + } + return this.panelClosingActions; }), // when the first closing event occurs... diff --git a/src/lib/autocomplete/autocomplete.spec.ts b/src/lib/autocomplete/autocomplete.spec.ts index d98576933023..00a455bdde4e 100644 --- a/src/lib/autocomplete/autocomplete.spec.ts +++ b/src/lib/autocomplete/autocomplete.spec.ts @@ -1538,6 +1538,39 @@ describe('MatAutocomplete', () => { subscription!.unsubscribe(); })); + it('should reposition the panel when the amount of options changes', fakeAsync(() => { + let formField = fixture.debugElement.query(By.css('.mat-form-field')).nativeElement; + let inputReference = formField.querySelector('.mat-form-field-flex'); + let input = inputReference.querySelector('input'); + + formField.style.bottom = '100px'; + formField.style.position = 'fixed'; + + typeInElement('Cali', input); + fixture.detectChanges(); + tick(); + zone.simulateZoneExit(); + fixture.detectChanges(); + + const inputBottom = inputReference.getBoundingClientRect().bottom; + const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!; + const panelTop = panel.getBoundingClientRect().top; + + expect(Math.floor(inputBottom)).toBe(Math.floor(panelTop), + `Expected panel top to match input bottom when there is only one option.`); + + typeInElement('', input); + fixture.detectChanges(); + tick(); + fixture.detectChanges(); + + const inputTop = inputReference.getBoundingClientRect().top; + const panelBottom = panel.getBoundingClientRect().bottom; + + expect(Math.floor(inputTop)).toBe(Math.floor(panelBottom), + `Expected panel switch to the above position if the options no longer fit.`); + })); + }); describe('panel closing', () => {