Skip to content

Commit 6169356

Browse files
committed
fix(select): incorrect menu width when there is no placeholder
The select pane's width wasn't being set properly, when it doesn't have a placeholder, because the width usually gets set within the placeholder's setter. Fixes #3244.
1 parent aa3360a commit 6169356

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/lib/select/select.spec.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ describe('MdSelect', () => {
4343
ThrowsErrorOnInit,
4444
BasicSelectOnPush,
4545
BasicSelectOnPushPreselected,
46-
SelectWithPlainTabindex
46+
SelectWithPlainTabindex,
47+
BasicSelectNoPlaceholder
4748
],
4849
providers: [
4950
{provide: OverlayContainer, useFactory: () => {
@@ -145,13 +146,27 @@ describe('MdSelect', () => {
145146
}));
146147

147148
it('should set the width of the overlay based on the trigger', async(() => {
148-
trigger.style.width = '200px';
149-
150149
fixture.whenStable().then(() => {
151150
trigger.click();
152151
fixture.detectChanges();
152+
153+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
154+
expect(pane.style.minWidth).toBe(trigger.getBoundingClientRect().width + 'px');
155+
});
156+
}));
157+
158+
it('should set the width of the overlay if there is no placeholder', async(() => {
159+
let noPlaceholder = TestBed.createComponent(BasicSelectNoPlaceholder);
160+
161+
noPlaceholder.detectChanges();
162+
trigger = noPlaceholder.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
163+
164+
noPlaceholder.whenStable().then(() => {
165+
trigger.click();
166+
noPlaceholder.detectChanges();
167+
153168
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
154-
expect(pane.style.minWidth).toBe('200px');
169+
expect(parseInt(pane.style.minWidth)).toBeGreaterThan(0);
155170
});
156171
}));
157172

@@ -1893,6 +1908,16 @@ class MultiSelect {
18931908
})
18941909
class SelectWithPlainTabindex { }
18951910

1911+
@Component({
1912+
selector: 'basic-select-no-placeholder',
1913+
template: `
1914+
<md-select>
1915+
<md-option value="value">There are no other options</md-option>
1916+
</md-select>
1917+
`
1918+
})
1919+
class BasicSelectNoPlaceholder { }
1920+
18961921

18971922
class FakeViewportRuler {
18981923
getViewportRect() {

src/lib/select/select.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
308308
this._selectionModel = new SelectionModel<MdOption>(this.multiple, null, false);
309309
this._initKeyManager();
310310

311+
// If there is no placeholder, the setter won't fire so we need to trigger it from here.
312+
if (!this._placeholder) {
313+
this._triggerWidth = this._getWidth();
314+
}
315+
311316
this._changeSubscription = this.options.changes.startWith(null).subscribe(() => {
312317
this._resetOptions();
313318

0 commit comments

Comments
 (0)