Skip to content

Commit 1deed81

Browse files
committed
fix(select): wrong panel width if element is hidden initially
Fixes the select not having a proper width if it became visible after it was initialized. Fixes #3639.
1 parent aa3360a commit 1deed81

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/lib/select/select.spec.ts

Lines changed: 33 additions & 1 deletion
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+
BasicSelectInitiallyHidden
4748
],
4849
providers: [
4950
{provide: OverlayContainer, useFactory: () => {
@@ -155,6 +156,25 @@ describe('MdSelect', () => {
155156
});
156157
}));
157158

159+
it('should set the width of the overlay if the element was hidden initially', async(() => {
160+
let initiallyHidden = TestBed.createComponent(BasicSelectInitiallyHidden);
161+
162+
initiallyHidden.detectChanges();
163+
trigger = initiallyHidden.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
164+
trigger.style.width = '200px';
165+
166+
initiallyHidden.componentInstance.isVisible = true;
167+
initiallyHidden.detectChanges();
168+
169+
initiallyHidden.whenStable().then(() => {
170+
trigger.click();
171+
initiallyHidden.detectChanges();
172+
173+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
174+
expect(pane.style.minWidth).toBe('200px');
175+
});
176+
}));
177+
158178
it('should not attempt to open a select that does not have any options', () => {
159179
fixture.componentInstance.foods = [];
160180
fixture.detectChanges();
@@ -1893,6 +1913,18 @@ class MultiSelect {
18931913
})
18941914
class SelectWithPlainTabindex { }
18951915

1916+
@Component({
1917+
selector: 'basic-select-initially-hidden',
1918+
template: `
1919+
<md-select [style.display]="isVisible ? 'block' : 'none'">
1920+
<md-option value="value">There are no other options</md-option>
1921+
</md-select>
1922+
`
1923+
})
1924+
class BasicSelectInitiallyHidden {
1925+
isVisible = false;
1926+
}
1927+
18961928

18971929
class FakeViewportRuler {
18981930
getViewportRect() {

src/lib/select/select.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
236236
this._placeholder = value;
237237

238238
// Must wait to record the trigger width to ensure placeholder width is included.
239-
Promise.resolve(null).then(() => this._triggerWidth = this._getWidth());
239+
Promise.resolve(null).then(() => this._setTriggerWidth());
240240
}
241241

242242
/** Whether the component is disabled. */
@@ -341,6 +341,11 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
341341
if (this.disabled || !this.options.length) {
342342
return;
343343
}
344+
345+
if (!this._triggerWidth) {
346+
this._setTriggerWidth();
347+
}
348+
344349
this._calculateOverlayPosition();
345350
this._placeholderState = this._floatPlaceholderState();
346351
this._panelOpen = true;
@@ -423,11 +428,12 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
423428
return this._dir ? this._dir.value === 'rtl' : false;
424429
}
425430

426-
/** The width of the trigger element. This is necessary to match
431+
/**
432+
* Sets the width of the trigger element. This is necessary to match
427433
* the overlay width to the trigger width.
428434
*/
429-
_getWidth(): number {
430-
return this._getTriggerRect().width;
435+
private _setTriggerWidth(): void {
436+
this._triggerWidth = this._getTriggerRect().width;
431437
}
432438

433439
/** Ensures the panel opens if activated by the keyboard. */

0 commit comments

Comments
 (0)