Skip to content

Commit

Permalink
fix(autocomplete): update overlay ref width on menu trigger (#3573)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
emoralesb05 authored and mmalerba committed Mar 14, 2017
1 parent 5346353 commit 6915e8a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
openPanel(): void {
if (!this._overlayRef) {
this._createOverlay();
} else {
/** Update the panel width, in case the host width has changed */
this._overlayRef.getState().width = this._getHostWidth();
}

if (!this._overlayRef.hasAttached()) {
Expand Down
29 changes: 28 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,11 +967,37 @@ describe('MdAutocomplete', () => {
}));

});

it('should have correct width when opened', () => {
const widthFixture = TestBed.createComponent(SimpleAutocomplete);
widthFixture.componentInstance.width = 300;
widthFixture.detectChanges();

widthFixture.componentInstance.trigger.openPanel();
widthFixture.detectChanges();

const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
// Firefox, edge return a decimal value for width, so we need to parse and round it to verify
expect(Math.ceil(parseFloat(overlayPane.style.width))).toEqual(300);

widthFixture.componentInstance.trigger.closePanel();
widthFixture.detectChanges();

widthFixture.componentInstance.width = 500;
widthFixture.detectChanges();

widthFixture.componentInstance.trigger.openPanel();
widthFixture.detectChanges();

// Firefox, edge return a decimal value for width, so we need to parse and round it to verify
expect(Math.ceil(parseFloat(overlayPane.style.width))).toEqual(500);

});
});

@Component({
template: `
<md-input-container [floatPlaceholder]="placeholder">
<md-input-container [floatPlaceholder]="placeholder" [style.width.px]="width">
<input mdInput placeholder="State" [mdAutocomplete]="auto" [formControl]="stateCtrl">
</md-input-container>
Expand All @@ -987,6 +1013,7 @@ class SimpleAutocomplete implements OnDestroy {
filteredStates: any[];
valueSub: Subscription;
placeholder = 'auto';
width: number;

@ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger;
@ViewChild(MdAutocomplete) panel: MdAutocomplete;
Expand Down

0 comments on commit 6915e8a

Please sign in to comment.