Skip to content

Commit 93fe0fd

Browse files
author
emoralesb05
committed
feat(select): update the trigger width when select is opened
when changing the width of the md-select (e.g. using flex box) after its rendered, the template[cdk-connected-overlay] does not update its minWidth and keeps the initial minWidth every time its opened.
1 parent 85a6fff commit 93fe0fd

File tree

4 files changed

+88
-15
lines changed

4 files changed

+88
-15
lines changed

src/cdk/overlay/overlay-directives.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ describe('Overlay directives', () => {
135135

136136
const pane = overlayContainerElement.children[0] as HTMLElement;
137137
expect(pane.style.width).toEqual('250px');
138+
139+
fixture.componentInstance.isOpen = false;
140+
fixture.detectChanges();
141+
142+
fixture.componentInstance.width = 500;
143+
fixture.componentInstance.isOpen = true;
144+
fixture.detectChanges();
145+
146+
expect(pane.style.width).toEqual('500px');
138147
});
139148

140149
it('should set the height', () => {
@@ -144,6 +153,15 @@ describe('Overlay directives', () => {
144153

145154
const pane = overlayContainerElement.children[0] as HTMLElement;
146155
expect(pane.style.height).toEqual('100vh');
156+
157+
fixture.componentInstance.isOpen = false;
158+
fixture.detectChanges();
159+
160+
fixture.componentInstance.height = '50vh';
161+
fixture.componentInstance.isOpen = true;
162+
fixture.detectChanges();
163+
164+
expect(pane.style.height).toEqual('50vh');
147165
});
148166

149167
it('should set the min width', () => {
@@ -153,6 +171,15 @@ describe('Overlay directives', () => {
153171

154172
const pane = overlayContainerElement.children[0] as HTMLElement;
155173
expect(pane.style.minWidth).toEqual('250px');
174+
175+
fixture.componentInstance.isOpen = false;
176+
fixture.detectChanges();
177+
178+
fixture.componentInstance.minWidth = 500;
179+
fixture.componentInstance.isOpen = true;
180+
fixture.detectChanges();
181+
182+
expect(pane.style.minWidth).toEqual('500px');
156183
});
157184

158185
it('should set the min height', () => {
@@ -162,6 +189,15 @@ describe('Overlay directives', () => {
162189

163190
const pane = overlayContainerElement.children[0] as HTMLElement;
164191
expect(pane.style.minHeight).toEqual('500px');
192+
193+
fixture.componentInstance.isOpen = false;
194+
fixture.detectChanges();
195+
196+
fixture.componentInstance.minHeight = 500;
197+
fixture.componentInstance.isOpen = true;
198+
fixture.detectChanges();
199+
200+
expect(pane.style.minHeight).toEqual('500px');
165201
});
166202

167203
it('should create the backdrop if designated', () => {

src/cdk/overlay/overlay-directives.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
274274
private _buildConfig(): OverlayState {
275275
let overlayConfig = new OverlayState();
276276

277+
this._configureSize(overlayConfig);
278+
279+
overlayConfig.hasBackdrop = this.hasBackdrop;
280+
281+
if (this.backdropClass) {
282+
overlayConfig.backdropClass = this.backdropClass;
283+
}
284+
285+
this._position = this._createPositionStrategy() as ConnectedPositionStrategy;
286+
overlayConfig.positionStrategy = this._position;
287+
overlayConfig.scrollStrategy = this.scrollStrategy;
288+
289+
return overlayConfig;
290+
}
291+
292+
/** Configure the overlay size based on the directive's inputs */
293+
private _configureSize(overlayConfig: OverlayState): void {
277294
if (this.width || this.width === 0) {
278295
overlayConfig.width = this.width;
279296
}
@@ -289,18 +306,6 @@ export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
289306
if (this.minHeight || this.minHeight === 0) {
290307
overlayConfig.minHeight = this.minHeight;
291308
}
292-
293-
overlayConfig.hasBackdrop = this.hasBackdrop;
294-
295-
if (this.backdropClass) {
296-
overlayConfig.backdropClass = this.backdropClass;
297-
}
298-
299-
this._position = this._createPositionStrategy() as ConnectedPositionStrategy;
300-
overlayConfig.positionStrategy = this._position;
301-
overlayConfig.scrollStrategy = this.scrollStrategy;
302-
303-
return overlayConfig;
304309
}
305310

306311
/** Returns the position strategy of the overlay to be set on the overlay config */
@@ -340,6 +345,8 @@ export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
340345
this._position.withDirection(this.dir);
341346
this._overlayRef.getState().direction = this.dir;
342347
this._initEscapeListener();
348+
/** Update the overlay size, in case the directive's inputs have changed */
349+
this._configureSize(this._overlayRef.getState());
343350

344351
if (!this._overlayRef.hasAttached()) {
345352
this._overlayRef.attach(this._templatePortal);

src/lib/select/select.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,34 @@ describe('MdSelect', () => {
24782478
});
24792479
}));
24802480

2481+
it('should set the width of the overlay based on the trigger and resize', async(() => {
2482+
trigger.style.width = '200px';
2483+
fixture.detectChanges();
2484+
fixture.whenStable().then(() => {
2485+
trigger.click();
2486+
fixture.detectChanges();
2487+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
2488+
expect(pane.style.minWidth).toBe('200px',
2489+
'Expected pane minWidth to be 200px initially');
2490+
2491+
const backdrop =
2492+
overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
2493+
backdrop.click();
2494+
fixture.detectChanges();
2495+
fixture.whenStable().then(() => {
2496+
2497+
trigger.style.width = '400px';
2498+
fixture.detectChanges();
2499+
fixture.whenStable().then(() => {
2500+
trigger.click();
2501+
fixture.detectChanges();
2502+
expect(pane.style.minWidth).toBe('400px',
2503+
'Expected pane minWidth to be 400px after resize');
2504+
});
2505+
});
2506+
});
2507+
}));
2508+
24812509
});
24822510

24832511
describe('theming', () => {

src/lib/select/select.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,11 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
466466
return;
467467
}
468468

469-
if (!this._triggerWidth) {
470-
this._setTriggerWidth();
471-
}
469+
/**
470+
* Always set the trigger width since we need to
471+
* update the panel with its new minWidth when opening it
472+
*/
473+
this._setTriggerWidth();
472474

473475
this._calculateOverlayPosition();
474476
this._placeholderState = this._floatPlaceholderState();

0 commit comments

Comments
 (0)