Skip to content

Commit 54ab1ab

Browse files
committed
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.
1 parent 7857b92 commit 54ab1ab

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,11 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
410410
switchMap(() => {
411411
this._resetActiveItem();
412412
this.autocomplete._setVisibility();
413+
414+
if (this.panelOpen) {
415+
this._overlayRef!.updatePosition();
416+
}
417+
413418
return this.panelClosingActions;
414419
}),
415420
// when the first closing event occurs...

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,39 @@ describe('MatAutocomplete', () => {
15381538
subscription!.unsubscribe();
15391539
}));
15401540

1541+
it('should reposition the panel when the amount of options changes', fakeAsync(() => {
1542+
let formField = fixture.debugElement.query(By.css('.mat-form-field')).nativeElement;
1543+
let inputReference = formField.querySelector('.mat-form-field-flex');
1544+
let input = inputReference.querySelector('input');
1545+
1546+
formField.style.top = '500px';
1547+
formField.style.position = 'fixed';
1548+
1549+
typeInElement('Cali', input);
1550+
fixture.detectChanges();
1551+
tick();
1552+
zone.simulateZoneExit();
1553+
fixture.detectChanges();
1554+
1555+
const inputBottom = inputReference.getBoundingClientRect().bottom;
1556+
const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!;
1557+
const panelTop = panel.getBoundingClientRect().top;
1558+
1559+
expect(Math.floor(inputBottom)).toBe(Math.floor(panelTop),
1560+
`Expected panel top to match input bottom when there is only one option.`);
1561+
1562+
typeInElement('', input);
1563+
fixture.detectChanges();
1564+
tick();
1565+
fixture.detectChanges();
1566+
1567+
const inputTop = inputReference.getBoundingClientRect().top;
1568+
const panelBottom = panel.getBoundingClientRect().bottom;
1569+
1570+
expect(Math.floor(inputTop)).toBe(Math.floor(panelBottom),
1571+
`Expected panel switch to the above position if the options no longer fit.`);
1572+
}));
1573+
15411574
});
15421575

15431576
describe('panel closing', () => {

0 commit comments

Comments
 (0)