Skip to content

refactor(connected-overlay-directive): avoid issues with property initialization #3228

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

Merged
merged 4 commits into from
May 11, 2017
Merged
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
32 changes: 29 additions & 3 deletions src/lib/core/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {ComponentFixture, TestBed, async} from '@angular/core/testing';
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ConnectedOverlayDirective, OverlayModule} from './overlay-directives';
import {ConnectedOverlayDirective, OverlayModule, OverlayOrigin} from './overlay-directives';
import {OverlayContainer} from './overlay-container';
import {ConnectedPositionStrategy} from './position/connected-position-strategy';
import {ConnectedOverlayPositionChange} from './position/connected-position';
Expand All @@ -18,7 +18,7 @@ describe('Overlay directives', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [OverlayModule],
declarations: [ConnectedOverlayDirectiveTest],
declarations: [ConnectedOverlayDirectiveTest, ConnectedOverlayPropertyInitOrder],
providers: [
{provide: OverlayContainer, useFactory: () => {
overlayContainerElement = document.createElement('div');
Expand Down Expand Up @@ -111,6 +111,21 @@ describe('Overlay directives', () => {
'Expected overlay to have been detached.');
});

it('should not depend on the order in which the `origin` and `open` are set', async(() => {
fixture.destroy();

const propOrderFixture = TestBed.createComponent(ConnectedOverlayPropertyInitOrder);
propOrderFixture.detectChanges();

const overlayDirective = propOrderFixture.componentInstance.connectedOverlayDirective;

expect(() => {
overlayDirective.open = true;
overlayDirective.origin = propOrderFixture.componentInstance.trigger;
propOrderFixture.detectChanges();
}).not.toThrow();
}));

describe('inputs', () => {

it('should set the width', () => {
Expand Down Expand Up @@ -310,3 +325,14 @@ class ConnectedOverlayDirectiveTest {

@ViewChild(ConnectedOverlayDirective) connectedOverlayDirective: ConnectedOverlayDirective;
}

@Component({
template: `
<button cdk-overlay-origin #trigger="cdkOverlayOrigin">Toggle menu</button>
<ng-template cdk-connected-overlay>Menu content</ng-template>`,
})
class ConnectedOverlayPropertyInitOrder {
@ViewChild(ConnectedOverlayDirective) connectedOverlayDirective: ConnectedOverlayDirective;
@ViewChild('trigger') trigger: OverlayOrigin;
}

24 changes: 12 additions & 12 deletions src/lib/core/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
Output,
ElementRef,
Renderer2,
OnChanges,
SimpleChanges,
} from '@angular/core';
import {Overlay, OVERLAY_PROVIDERS} from './overlay';
import {OverlayRef} from './overlay-ref';
Expand Down Expand Up @@ -63,10 +65,9 @@ export class OverlayOrigin {
selector: '[cdk-connected-overlay], [connected-overlay], [cdkConnectedOverlay]',
exportAs: 'cdkConnectedOverlay'
})
export class ConnectedOverlayDirective implements OnDestroy {
export class ConnectedOverlayDirective implements OnDestroy, OnChanges {
private _overlayRef: OverlayRef;
private _templatePortal: TemplatePortal;
private _open = false;
private _hasBackdrop = false;
private _backdropSubscription: Subscription;
private _positionSubscription: Subscription;
Expand Down Expand Up @@ -125,6 +126,9 @@ export class ConnectedOverlayDirective implements OnDestroy {
/** Strategy to be used when handling scroll events while the overlay is open. */
@Input() scrollStrategy: ScrollStrategy = new RepositionScrollStrategy(this._scrollDispatcher);

/** Whether the overlay is open. */
@Input() open: boolean = false;

/** Whether or not the overlay should attach a backdrop. */
@Input()
get hasBackdrop() {
Expand All @@ -135,16 +139,6 @@ export class ConnectedOverlayDirective implements OnDestroy {
this._hasBackdrop = coerceBooleanProperty(value);
}

@Input()
get open() {
return this._open;
}

set open(value: boolean) {
value ? this._attachOverlay() : this._detachOverlay();
this._open = value;
}

/** Event emitted when the backdrop is clicked. */
@Output() backdropClick = new EventEmitter<void>();

Expand Down Expand Up @@ -183,6 +177,12 @@ export class ConnectedOverlayDirective implements OnDestroy {
this._destroyOverlay();
}

ngOnChanges(changes: SimpleChanges) {
if (changes['open']) {
this.open ? this._attachOverlay() : this._detachOverlay();
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a unit test for the case this fixes?

/** Creates an overlay */
private _createOverlay() {
if (!this.positions || !this.positions.length) {
Expand Down