Skip to content

Commit

Permalink
feat(modal): add 'beforeDismiss' option
Browse files Browse the repository at this point in the history
Closes #1598

Closes #1799
  • Loading branch information
kpwbo authored and pkozlowski-opensource committed Sep 1, 2017
1 parent d46250a commit a758eae
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/modal/modal-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class NgbModalRef {

constructor(
private _windowCmptRef: ComponentRef<NgbModalWindow>, private _contentRef: ContentRef,
private _backdropCmptRef?: ComponentRef<NgbModalBackdrop>) {
private _backdropCmptRef?: ComponentRef<NgbModalBackdrop>, private _beforeDismiss?: Function) {
_windowCmptRef.instance.dismissEvent.subscribe((reason: any) => { this.dismiss(reason); });

this.result = new Promise((resolve, reject) => {
Expand All @@ -75,8 +75,10 @@ export class NgbModalRef {
*/
dismiss(reason?: any): void {
if (this._windowCmptRef) {
this._reject(reason);
this._removeModalElements();
if (!this._beforeDismiss || this._beforeDismiss() !== false) {
this._reject(reason);
this._removeModalElements();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class NgbModalStack {
this._applicationRef.attachView(windowCmptRef.hostView);
containerEl.appendChild(windowCmptRef.location.nativeElement);

ngbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef);
ngbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);

activeModal.close = (result: any) => { ngbModalRef.close(result); };
activeModal.dismiss = (reason: any) => { ngbModalRef.dismiss(reason); };
Expand Down
37 changes: 37 additions & 0 deletions src/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,43 @@ describe('ngb-modal', () => {
});
});

describe('beforeDismiss options', () => {

it('should not dismiss when the callback returns false', () => {
const modalInstance = fixture.componentInstance.openTplDismiss({beforeDismiss: () => { return false; }});
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal();

(<HTMLElement>document.querySelector('button#dismiss')).click();
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal();

modalInstance.close();
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});

it('should dimiss when the callback does not return false', () => {
fixture.componentInstance.openTplDismiss({beforeDismiss: () => {}});
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal();

(<HTMLElement>document.querySelector('button#dismiss')).click();
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});

it('should dismiss when the callback is not defined', () => {
fixture.componentInstance.openTplDismiss({});
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal();

(<HTMLElement>document.querySelector('button#dismiss')).click();
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});
});

describe('container options', () => {

it('should attach window and backdrop elements to the specified container', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export interface NgbModalOptions {
*/
backdrop?: boolean | 'static';

/**
* Function called when a modal will be dismissed.
* If this function returns false, the modal is not dismissed.
*/
beforeDismiss?: () => boolean;

/**
* An element to which to attach newly opened modal windows.
*/
Expand Down

0 comments on commit a758eae

Please sign in to comment.