Skip to content

fix(autocomplete): reposition the panel when the amount of options changes #4469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
33 changes: 33 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down