Skip to content

Commit

Permalink
fix(modal): pass modalRef as an argument into beforeDismiss callback
Browse files Browse the repository at this point in the history
  • Loading branch information
gentoo90 committed Jan 30, 2022
1 parent 8a8fc1e commit ef7c043
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/modal/modal-config.ts
@@ -1,10 +1,11 @@
import {Injectable, Injector} from '@angular/core';
import {NgbConfig} from '../ngb-config';
import {NgbModalRef} from './modal-ref';

/**
* Options available when opening new modal windows with `NgbModal.open()` method.
*/
export interface NgbModalOptions {
export interface NgbModalOptions<T = any> {
/**
* If `true`, modal opening and closing will be animated.
*
Expand Down Expand Up @@ -45,7 +46,7 @@ export interface NgbModalOptions {
*
* then the modal won't be dismissed.
*/
beforeDismiss?: () => boolean | Promise<boolean>;
beforeDismiss?: (modalRef: NgbModalRef<T>) => boolean | Promise<boolean>;

/**
* If `true`, the modal will be centered vertically.
Expand Down Expand Up @@ -121,7 +122,7 @@ export class NgbModalConfig implements Required<NgbModalOptions> {
ariaLabelledBy: string;
ariaDescribedBy: string;
backdrop: boolean | 'static' = true;
beforeDismiss: () => boolean | Promise<boolean>;
beforeDismiss: (modalRef: NgbModalRef<any>) => boolean | Promise<boolean>;
centered: boolean;
container: string;
injector: Injector;
Expand Down
4 changes: 2 additions & 2 deletions src/modal/modal-ref.ts
Expand Up @@ -100,7 +100,7 @@ export class NgbModalRef<T = any> {
constructor(
private _windowCmptRef: ComponentRef<NgbModalWindow>, private _contentRef: ContentRef,
private _backdropCmptRef?: ComponentRef<NgbModalBackdrop>,
private _beforeDismiss?: () => boolean | Promise<boolean>) {
private _beforeDismiss?: (modalRef: NgbModalRef<any>) => boolean | Promise<boolean>) {
_windowCmptRef.instance.dismissEvent.subscribe((reason: any) => { this.dismiss(reason); });

this.result = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -139,7 +139,7 @@ export class NgbModalRef<T = any> {
if (!this._beforeDismiss) {
this._dismiss(reason);
} else {
const dismiss = this._beforeDismiss();
const dismiss = this._beforeDismiss(this);
if (isPromise(dismiss)) {
dismiss.then(
result => {
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal.ts
Expand Up @@ -25,7 +25,7 @@ export class NgbModal {
*
* Also see the [`NgbModalOptions`](#/components/modal/api#NgbModalOptions) for the list of supported options.
*/
open<T>(content: T, options: NgbModalOptions = {}): NgbModalRef<T> {
open<T>(content: T, options: NgbModalOptions<T> = {}): NgbModalRef<T> {
const combinedOptions = {...this._config, animation: this._config.animation, ...options};
return this._modalStack.open(this._moduleCFR, this._injector, content, combinedOptions);
}
Expand Down

0 comments on commit ef7c043

Please sign in to comment.