Skip to content

fix(select): incorrect menu width when there is no placeholder #3247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ describe('MdSelect', () => {
ThrowsErrorOnInit,
BasicSelectOnPush,
BasicSelectOnPushPreselected,
SelectWithPlainTabindex
SelectWithPlainTabindex,
BasicSelectNoPlaceholder
],
providers: [
{provide: OverlayContainer, useFactory: () => {
Expand Down Expand Up @@ -150,11 +151,27 @@ describe('MdSelect', () => {
fixture.whenStable().then(() => {
trigger.click();
fixture.detectChanges();

const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.style.minWidth).toBe('200px');
});
}));

it('should set the width of the overlay if there is no placeholder', async(() => {
let noPlaceholder = TestBed.createComponent(BasicSelectNoPlaceholder);

noPlaceholder.detectChanges();
trigger = noPlaceholder.debugElement.query(By.css('.mat-select-trigger')).nativeElement;

noPlaceholder.whenStable().then(() => {
trigger.click();
noPlaceholder.detectChanges();

const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(parseInt(pane.style.minWidth)).toBeGreaterThan(0);
});
}));

it('should not attempt to open a select that does not have any options', () => {
fixture.componentInstance.foods = [];
fixture.detectChanges();
Expand Down Expand Up @@ -1893,6 +1910,16 @@ class MultiSelect {
})
class SelectWithPlainTabindex { }

@Component({
selector: 'basic-select-no-placeholder',
template: `
<md-select>
<md-option value="value">There are no other options</md-option>
</md-select>
`
})
class BasicSelectNoPlaceholder { }


class FakeViewportRuler {
getViewportRect() {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
this._selectionModel = new SelectionModel<MdOption>(this.multiple, null, false);
this._initKeyManager();

// If there is no placeholder, the setter won't fire so we need to trigger it from here.
if (!this._placeholder) {
this._triggerWidth = this._getWidth();
}

this._changeSubscription = this.options.changes.startWith(null).subscribe(() => {
this._resetOptions();

Expand Down