Skip to content
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

feat(select): update the trigger width when select is opened #3623

Closed
wants to merge 2 commits into from
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
36 changes: 36 additions & 0 deletions src/lib/core/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ describe('Overlay directives', () => {

const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.width).toEqual('250px');

fixture.componentInstance.isOpen = false;
fixture.detectChanges();

fixture.componentInstance.width = 500;
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(pane.style.width).toEqual('500px');
});

it('should set the height', () => {
Expand All @@ -116,6 +125,15 @@ describe('Overlay directives', () => {

const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.height).toEqual('100vh');

fixture.componentInstance.isOpen = false;
fixture.detectChanges();

fixture.componentInstance.height = '50vh';
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(pane.style.height).toEqual('50vh');
});

it('should set the min width', () => {
Expand All @@ -125,6 +143,15 @@ describe('Overlay directives', () => {

const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.minWidth).toEqual('250px');

fixture.componentInstance.isOpen = false;
fixture.detectChanges();

fixture.componentInstance.minWidth = 500;
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(pane.style.minWidth).toEqual('500px');
});

it('should set the min height', () => {
Expand All @@ -134,6 +161,15 @@ describe('Overlay directives', () => {

const pane = overlayContainerElement.children[0] as HTMLElement;
expect(pane.style.minHeight).toEqual('500px');

fixture.componentInstance.isOpen = false;
fixture.detectChanges();

fixture.componentInstance.minHeight = 500;
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(pane.style.minHeight).toEqual('500px');
});

it('should create the backdrop if designated', () => {
Expand Down
29 changes: 18 additions & 11 deletions src/lib/core/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ export class ConnectedOverlayDirective implements OnDestroy {
private _buildConfig(): OverlayState {
let overlayConfig = new OverlayState();

this._configureState(overlayConfig);

overlayConfig.hasBackdrop = this.hasBackdrop;

if (this.backdropClass) {
overlayConfig.backdropClass = this.backdropClass;
}

this._position = this._createPositionStrategy() as ConnectedPositionStrategy;
overlayConfig.positionStrategy = this._position;

return overlayConfig;
}

/** Configure the overlay state based on the directive's inputs */
private _configureState(overlayConfig: OverlayState): void {
if (this.width || this.width === 0) {
overlayConfig.width = this.width;
}
Expand All @@ -200,17 +216,6 @@ export class ConnectedOverlayDirective implements OnDestroy {
if (this.minHeight || this.minHeight === 0) {
overlayConfig.minHeight = this.minHeight;
}

overlayConfig.hasBackdrop = this.hasBackdrop;

if (this.backdropClass) {
overlayConfig.backdropClass = this.backdropClass;
}

this._position = this._createPositionStrategy() as ConnectedPositionStrategy;
overlayConfig.positionStrategy = this._position;

return overlayConfig;
}

/** Returns the position strategy of the overlay to be set on the overlay config */
Expand Down Expand Up @@ -249,6 +254,8 @@ export class ConnectedOverlayDirective implements OnDestroy {

this._position.withDirection(this.dir);
this._overlayRef.getState().direction = this.dir;
/** Update the overlay state, in case the directive's inputs have changed */
this._configureState(this._overlayRef.getState());

if (!this._overlayRef.hasAttached()) {
this._overlayRef.attach(this._templatePortal);
Expand Down
19 changes: 16 additions & 3 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
} from '@angular/core';
import {MdSelectModule} from './index';
import {OverlayContainer} from '../core/overlay/overlay-container';
import {MdSelect, MdSelectFloatPlaceholderType} from './select';
import {
MdSelect, MdSelectFloatPlaceholderType, SELECT_MULTIPLE_PANEL_PADDING_X, SELECT_PANEL_PADDING_X
} from './select';
import {MdSelectDynamicMultipleError, MdSelectNonArrayValueError} from './select-errors';
import {MdOption} from '../core/option/option';
import {Dir} from '../core/rtl/dir';
Expand Down Expand Up @@ -151,7 +153,7 @@ describe('MdSelect', () => {
trigger.click();
fixture.detectChanges();
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.style.minWidth).toBe('200px');
expect(pane.style.minWidth).toBe((200 + SELECT_PANEL_PADDING_X) + 'px');
});
}));

Expand Down Expand Up @@ -1302,7 +1304,7 @@ describe('MdSelect', () => {
fixture.detectChanges();

const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
expect(pane.style.minWidth).toEqual('300px');
expect(pane.style.minWidth).toEqual((300 + SELECT_PANEL_PADDING_X) + 'px');

expect(fixture.componentInstance.select.panelOpen).toBe(true);
expect(overlayContainerElement.textContent).toContain('Steak');
Expand Down Expand Up @@ -1593,6 +1595,17 @@ describe('MdSelect', () => {
});
}));

it('should set the width of the overlay based on the trigger', async(() => {
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((200 + SELECT_MULTIPLE_PANEL_PADDING_X) + 'px');
});
}));

});

});
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 @@ -342,6 +342,11 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
return;
}
this._calculateOverlayPosition();
/**
* Update the trigger width, in case the select width has changed
* Taking into account the "offsetX"/"dir" to fit it better.
*/
this._triggerWidth = this._getWidth() + (this._offsetX * (!this._isRtl() ? -1 : 1));
this._placeholderState = this._floatPlaceholderState();
this._panelOpen = true;
}
Expand Down