Skip to content

Commit

Permalink
fix(popover): propagate 'popoverClass' change correctly (#3420)
Browse files Browse the repository at this point in the history
Changing `[popoverClass]="expr"` while the popover is opened was not correctly propagated to the popover window
  • Loading branch information
maxokorokov committed Oct 22, 2019
1 parent f450a7c commit 51bfc72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/popover/popover.spec.ts
Expand Up @@ -179,6 +179,21 @@ describe('ngb-popover', () => {
expect(directive.nativeElement.getAttribute('aria-describedby')).toBeNull();
});

it('should propagate popoverClass changes to the window', () => {
const fixture =
createTestComponent(`<div ngbPopover="Great tip!" popoverTitle="Title" [popoverClass]="popoverClass"></div>`);
const directive = fixture.debugElement.query(By.directive(NgbPopover));

triggerEvent(directive, 'click');
fixture.detectChanges();
const windowEl = getWindow(fixture.nativeElement);
expect(windowEl).not.toHaveCssClass('my-popover-class');

fixture.componentInstance.popoverClass = 'my-popover-class';
fixture.detectChanges();
expect(windowEl).toHaveCssClass('my-popover-class');
});

it('should accept a template for the title and properly destroy it when closing', () => {
const fixture = createTestComponent(`
<ng-template #t>Hello, {{name}}! <destroyable-cmpt></destroyable-cmpt></ng-template>
Expand Down Expand Up @@ -727,6 +742,7 @@ export class TestComponent {
show = true;
title: string;
placement: string;
popoverClass: string;

@ViewChild(NgbPopover, {static: true}) popover: NgbPopover;

Expand Down
7 changes: 5 additions & 2 deletions src/popover/popover.ts
Expand Up @@ -274,9 +274,12 @@ export class NgbPopover implements OnInit, OnDestroy, OnChanges {
this.close.bind(this), +this.openDelay, +this.closeDelay);
}

ngOnChanges(changes: SimpleChanges) {
ngOnChanges({ngbPopover, popoverTitle, disablePopover, popoverClass}: SimpleChanges) {
if (popoverClass && this.isOpen()) {
this._windowRef.instance.popoverClass = popoverClass.currentValue;
}
// close popover if title and content become empty, or disablePopover set to true
if ((changes['ngbPopover'] || changes['popoverTitle'] || changes['disablePopover']) && this._isDisabled()) {
if ((ngbPopover || popoverTitle || disablePopover) && this._isDisabled()) {
this.close();
}
}
Expand Down

0 comments on commit 51bfc72

Please sign in to comment.