Skip to content

Commit a758eae

Browse files
kpwbopkozlowski-opensource
authored andcommitted
feat(modal): add 'beforeDismiss' option
Closes #1598 Closes #1799
1 parent d46250a commit a758eae

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

src/modal/modal-ref.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class NgbModalRef {
5050

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

5656
this.result = new Promise((resolve, reject) => {
@@ -75,8 +75,10 @@ export class NgbModalRef {
7575
*/
7676
dismiss(reason?: any): void {
7777
if (this._windowCmptRef) {
78-
this._reject(reason);
79-
this._removeModalElements();
78+
if (!this._beforeDismiss || this._beforeDismiss() !== false) {
79+
this._reject(reason);
80+
this._removeModalElements();
81+
}
8082
}
8183
}
8284

src/modal/modal-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class NgbModalStack {
5353
this._applicationRef.attachView(windowCmptRef.hostView);
5454
containerEl.appendChild(windowCmptRef.location.nativeElement);
5555

56-
ngbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef);
56+
ngbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);
5757

5858
activeModal.close = (result: any) => { ngbModalRef.close(result); };
5959
activeModal.dismiss = (reason: any) => { ngbModalRef.dismiss(reason); };

src/modal/modal.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,43 @@ describe('ngb-modal', () => {
367367
});
368368
});
369369

370+
describe('beforeDismiss options', () => {
371+
372+
it('should not dismiss when the callback returns false', () => {
373+
const modalInstance = fixture.componentInstance.openTplDismiss({beforeDismiss: () => { return false; }});
374+
fixture.detectChanges();
375+
expect(fixture.nativeElement).toHaveModal();
376+
377+
(<HTMLElement>document.querySelector('button#dismiss')).click();
378+
fixture.detectChanges();
379+
expect(fixture.nativeElement).toHaveModal();
380+
381+
modalInstance.close();
382+
fixture.detectChanges();
383+
expect(fixture.nativeElement).not.toHaveModal();
384+
});
385+
386+
it('should dimiss when the callback does not return false', () => {
387+
fixture.componentInstance.openTplDismiss({beforeDismiss: () => {}});
388+
fixture.detectChanges();
389+
expect(fixture.nativeElement).toHaveModal();
390+
391+
(<HTMLElement>document.querySelector('button#dismiss')).click();
392+
fixture.detectChanges();
393+
expect(fixture.nativeElement).not.toHaveModal();
394+
});
395+
396+
it('should dismiss when the callback is not defined', () => {
397+
fixture.componentInstance.openTplDismiss({});
398+
fixture.detectChanges();
399+
expect(fixture.nativeElement).toHaveModal();
400+
401+
(<HTMLElement>document.querySelector('button#dismiss')).click();
402+
fixture.detectChanges();
403+
expect(fixture.nativeElement).not.toHaveModal();
404+
});
405+
});
406+
370407
describe('container options', () => {
371408

372409
it('should attach window and backdrop elements to the specified container', () => {

src/modal/modal.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export interface NgbModalOptions {
1313
*/
1414
backdrop?: boolean | 'static';
1515

16+
/**
17+
* Function called when a modal will be dismissed.
18+
* If this function returns false, the modal is not dismissed.
19+
*/
20+
beforeDismiss?: () => boolean;
21+
1622
/**
1723
* An element to which to attach newly opened modal windows.
1824
*/

0 commit comments

Comments
 (0)