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 Oct 17, 2023
1 parent d05e0dd commit 8cb3959
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 @@ -151,7 +152,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 | HTMLElement;
fullscreen: 'sm' | 'md' | 'lg' | 'xl' | 'xxl' | boolean | string = false;
Expand Down
4 changes: 2 additions & 2 deletions src/modal/modal-ref.ts
Expand Up @@ -160,7 +160,7 @@ export class NgbModalRef<T = any> {
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);
Expand Down Expand Up @@ -202,7 +202,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 @@ -23,7 +23,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._injector, content, combinedOptions);
}
Expand Down

0 comments on commit 8cb3959

Please sign in to comment.