Skip to content

Commit 60fd3be

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 5e349d9 commit 60fd3be

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
403403
private _resetPanel() {
404404
this._resetActiveItem();
405405
this._positionStrategy.recalculateLastPosition();
406+
this._overlayRef.updatePosition();
406407
this.autocomplete._setVisibility();
407408
}
408409

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,37 @@ describe('MdAutocomplete', () => {
10001000
});
10011001
}));
10021002

1003+
it('should reposition the panel when the amount of options changes', () => {
1004+
input.style.top = '500px';
1005+
input.style.position = 'relative';
1006+
1007+
typeInElement('Cali', input);
1008+
fixture.detectChanges();
1009+
1010+
fixture.componentInstance.trigger.openPanel();
1011+
fixture.detectChanges();
1012+
1013+
const inputBottom = input.getBoundingClientRect().bottom;
1014+
const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel');
1015+
const panelTop = panel.getBoundingClientRect().top;
1016+
1017+
expect(Math.floor(inputBottom + 6)).toEqual(Math.floor(panelTop),
1018+
`Expected panel top to match input bottom when there is only one option.`);
1019+
expect(fixture.componentInstance.trigger.autocomplete.positionY).toEqual('below',
1020+
`Expected autocomplete to open below the trigger when there is only one option.`);
1021+
1022+
typeInElement('', input);
1023+
fixture.detectChanges();
1024+
1025+
const inputTop = input.getBoundingClientRect().top;
1026+
const panelBottom = panel.getBoundingClientRect().bottom;
1027+
1028+
expect(Math.floor(inputTop + 6)).toEqual(Math.floor(panelBottom),
1029+
`Expected panel switch to the above position if the options no longer fit.`);
1030+
expect(fixture.componentInstance.trigger.autocomplete.positionY).toEqual('above',
1031+
`Expected autocomplete positionY to be "above" if the options no longer fit.`);
1032+
});
1033+
10031034
});
10041035

10051036
describe('Option selection', () => {

0 commit comments

Comments
 (0)