Skip to content

fix(select): incorrect panel width when trigger is resized post initialization #6658

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
25 changes: 18 additions & 7 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,26 @@ describe('MdSelect', () => {
});
}));

it('should set the width of the overlay based on the trigger', async(() => {
it('should set the width of the overlay based on the trigger', fakeAsync(() => {
trigger.style.width = '200px';

fixture.whenStable().then(() => {
trigger.click();
fixture.detectChanges();
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.style.minWidth).toBe('200px');
});
trigger.click();
fixture.detectChanges();
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.style.minWidth).toBe('200px');
}));

it('should set the correct overlay width when the trigger width was' +
' changed after the placeholder was initialized', fakeAsync(() => {

fixture.componentInstance.select.placeholder = 'Lorem ipsum dolor sit amet.';
tick();

trigger.style.width = '300px';
trigger.click();
fixture.detectChanges();
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.style.minWidth).toBe('300px');
}));

it('should set the width of the overlay if the element was hidden initially', async(() => {
Expand Down
8 changes: 6 additions & 2 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,12 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
set placeholder(value: string) {
this._placeholder = value;

// Must wait to record the trigger width to ensure placeholder width is included.
Promise.resolve(null).then(() => this._setTriggerWidth());
// Only re-calculate the width if we've had a width before. This avoids issues in the
// cases where the width might be assigned at a later point (e.g. flex-layout).
if (this._triggerWidth) {
// Must wait to record the trigger width to ensure placeholder width is included.
Promise.resolve(null).then(() => this._setTriggerWidth());
}
}

/** Whether the component is required. */
Expand Down