Skip to content

Commit 6915e8a

Browse files
emoralesb05mmalerba
authored andcommitted
fix(autocomplete): update overlay ref width on menu trigger (#3573)
* fix(autocomplete): update overlay ref width on menu trigger when changing the width of the `host` (or using flex box) the `div.cdk-overlay-pane` kept the width of the host when it was first created * chore(autocomplete): add pane width unit test * chore(): parse and round width since firefox and edge return a decimal width * chore(): update comment to make it more understandable
1 parent 5346353 commit 6915e8a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
121121
openPanel(): void {
122122
if (!this._overlayRef) {
123123
this._createOverlay();
124+
} else {
125+
/** Update the panel width, in case the host width has changed */
126+
this._overlayRef.getState().width = this._getHostWidth();
124127
}
125128

126129
if (!this._overlayRef.hasAttached()) {

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,11 +967,37 @@ describe('MdAutocomplete', () => {
967967
}));
968968

969969
});
970+
971+
it('should have correct width when opened', () => {
972+
const widthFixture = TestBed.createComponent(SimpleAutocomplete);
973+
widthFixture.componentInstance.width = 300;
974+
widthFixture.detectChanges();
975+
976+
widthFixture.componentInstance.trigger.openPanel();
977+
widthFixture.detectChanges();
978+
979+
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
980+
// Firefox, edge return a decimal value for width, so we need to parse and round it to verify
981+
expect(Math.ceil(parseFloat(overlayPane.style.width))).toEqual(300);
982+
983+
widthFixture.componentInstance.trigger.closePanel();
984+
widthFixture.detectChanges();
985+
986+
widthFixture.componentInstance.width = 500;
987+
widthFixture.detectChanges();
988+
989+
widthFixture.componentInstance.trigger.openPanel();
990+
widthFixture.detectChanges();
991+
992+
// Firefox, edge return a decimal value for width, so we need to parse and round it to verify
993+
expect(Math.ceil(parseFloat(overlayPane.style.width))).toEqual(500);
994+
995+
});
970996
});
971997

972998
@Component({
973999
template: `
974-
<md-input-container [floatPlaceholder]="placeholder">
1000+
<md-input-container [floatPlaceholder]="placeholder" [style.width.px]="width">
9751001
<input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
9761002
</md-input-container>
9771003
@@ -987,6 +1013,7 @@ class SimpleAutocomplete implements OnDestroy {
9871013
filteredStates: any[];
9881014
valueSub: Subscription;
9891015
placeholder = 'auto';
1016+
width: number;
9901017

9911018
@ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger;
9921019
@ViewChild(MdAutocomplete) panel: MdAutocomplete;

0 commit comments

Comments
 (0)