Skip to content

Commit 2641405

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 e3b2486 commit 2641405

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/lib/select/select.spec.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ describe('MdSelect', () => {
3838
FloatPlaceholderSelect,
3939
SelectWithErrorSibling,
4040
ThrowsErrorOnInit,
41-
BasicSelectOnPush
41+
BasicSelectOnPush,
42+
BasicSelectNoPlaceholder
4243
],
4344
providers: [
4445
{provide: OverlayContainer, useFactory: () => {
@@ -119,13 +120,27 @@ describe('MdSelect', () => {
119120
}));
120121

121122
it('should set the width of the overlay based on the trigger', async(() => {
122-
trigger.style.width = '200px';
123-
124123
fixture.whenStable().then(() => {
125124
trigger.click();
126125
fixture.detectChanges();
126+
127+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
128+
expect(pane.style.minWidth).toBe(trigger.getBoundingClientRect().width + 'px');
129+
});
130+
}));
131+
132+
it('should set the width of the overlay if there is no placeholder', async(() => {
133+
let noPlaceholder = TestBed.createComponent(BasicSelectNoPlaceholder);
134+
135+
noPlaceholder.detectChanges();
136+
trigger = noPlaceholder.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
137+
138+
noPlaceholder.whenStable().then(() => {
139+
trigger.click();
140+
noPlaceholder.detectChanges();
141+
127142
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
128-
expect(pane.style.minWidth).toBe('200px');
143+
expect(parseInt(pane.style.minWidth)).toBeGreaterThan(0);
129144
});
130145
}));
131146

@@ -1588,6 +1603,16 @@ class FloatPlaceholderSelect {
15881603
@ViewChild(MdSelect) select: MdSelect;
15891604
}
15901605

1606+
@Component({
1607+
selector: 'basic-select-no-placeholder',
1608+
template: `
1609+
<md-select>
1610+
<md-option value="value">There are no other options</md-option>
1611+
</md-select>
1612+
`
1613+
})
1614+
class BasicSelectNoPlaceholder { }
1615+
15911616

15921617
/**
15931618
* TODO: Move this to core testing utility until Angular has event faking

src/lib/select/select.ts

+5
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
257257
ngAfterContentInit() {
258258
this._initKeyManager();
259259

260+
// If there is no placeholder, the setter won't fire so we need to trigger it from here.
261+
if (!this._placeholder) {
262+
this._triggerWidth = this._getWidth();
263+
}
264+
260265
this._changeSubscription = this.options.changes.startWith(null).subscribe(() => {
261266
this._resetOptions();
262267

0 commit comments

Comments
 (0)