Skip to content

Commit 311242a

Browse files
author
emoralesb05
committed
feat(select): update the trigger width when select is opened
1 parent 85a6fff commit 311242a

File tree

6 files changed

+116
-20
lines changed

6 files changed

+116
-20
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: 22 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 */
@@ -335,6 +340,11 @@ export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
335340
private _attachOverlay() {
336341
if (!this._overlayRef) {
337342
this._createOverlay();
343+
} else {
344+
// Update the overlay size, in case the directive's inputs have changed
345+
const overlayState = new OverlayState();
346+
this._configureSize(overlayState);
347+
this._overlayRef.updateSize(overlayState);
338348
}
339349

340350
this._position.withDirection(this.dir);

src/cdk/overlay/overlay-ref.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class OverlayRef implements PortalHost {
4747

4848
// Update the pane element with the given state configuration.
4949
this._updateStackingOrder();
50-
this.updateSize();
50+
this._setSizeStyles();
5151
this.updateDirection();
5252
this.updatePosition();
5353
this._state.scrollStrategy.enable();
@@ -155,8 +155,29 @@ export class OverlayRef implements PortalHost {
155155
this._pane.setAttribute('dir', this._state.direction!);
156156
}
157157

158+
/** Updates the state size of the overlay */
159+
updateSize(state: OverlayState) {
160+
if (state.width || state.width === 0) {
161+
this._state.width = state.width;
162+
}
163+
164+
if (state.height || state.height === 0) {
165+
this._state.height = state.height;
166+
}
167+
168+
if (state.minWidth || state.minWidth === 0) {
169+
this._state.minWidth = state.minWidth;
170+
}
171+
172+
if (state.minHeight || state.minHeight === 0) {
173+
this._state.minHeight = state.minHeight;
174+
}
175+
176+
this._setSizeStyles();
177+
}
178+
158179
/** Updates the size of the overlay based on the overlay config. */
159-
updateSize() {
180+
private _setSizeStyles() {
160181
if (this._state.width || this._state.width === 0) {
161182
this._pane.style.width = formatCssUnit(this._state.width);
162183
}

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
176176
if (!this._overlayRef) {
177177
this._createOverlay();
178178
} else {
179-
/** Update the panel width, in case the host width has changed */
180-
this._overlayRef.getState().width = this._getHostWidth();
181-
this._overlayRef.updateSize();
179+
// Update the panel width, in case the host width has changed
180+
const overlayState = new OverlayState();
181+
overlayState.width = this._getHostWidth();
182+
this._overlayRef.updateSize(overlayState);
182183
}
183184

184185
if (this._overlayRef && !this._overlayRef.hasAttached()) {

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
466466
return;
467467
}
468468

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

473473
this._calculateOverlayPosition();
474474
this._placeholderState = this._floatPlaceholderState();

0 commit comments

Comments
 (0)