diff --git a/src/material/dialog/dialog-config.ts b/src/material/dialog/dialog-config.ts index 0cfb0ae86234..b02aa286b54d 100644 --- a/src/material/dialog/dialog-config.ts +++ b/src/material/dialog/dialog-config.ts @@ -35,7 +35,7 @@ export interface DialogPosition { /** * Configuration for opening a modal dialog with the MatDialog service. */ -export class MatDialogConfig { +export class MatDialogConfig { /** * Where the attached component should live in Angular's *logical* component tree. * This affects what is available for injection and the change detection order for the @@ -69,13 +69,9 @@ export class MatDialogConfig { disableClose?: boolean = false; /** Function used to determine whether the dialog is allowed to close. */ - closePredicate?: < - Result = unknown, - Component = unknown, - Config extends DialogConfig = MatDialogConfig, - >( + closePredicate?: ( result: Result | undefined, - config: Config, + config: this, componentInstance: Component | null, ) => boolean; diff --git a/src/material/dialog/dialog.spec.ts b/src/material/dialog/dialog.spec.ts index d68cc19fe351..d54bd47a7b63 100644 --- a/src/material/dialog/dialog.spec.ts +++ b/src/material/dialog/dialog.spec.ts @@ -1017,8 +1017,15 @@ describe('MatDialog', () => { it('should determine whether closing via the backdrop is allowed', fakeAsync(() => { let canClose = false; const closedSpy = jasmine.createSpy('closed spy'); - const ref = dialog.open(PizzaMsg, { - closePredicate: () => canClose, + const ref = dialog.open(PizzaMsg, { + data: {myData: 'my data'}, + closePredicate: (result, config, componentInstance) => { + expect(result?.myResult).toBe(''); + expect(config.data).toBe({myData: 'my data'}); + expect(componentInstance?.dialogRef).toBeInstanceOf(MatDialogRef); + + return canClose; + }, viewContainerRef: testViewContainerRef, }); @@ -2302,12 +2309,20 @@ class ComponentWithTemplateRef { } } +type PizzaData = { + myData?: string; +} + +type PizzaResult = { + myResult: string; +} + /** Simple component for testing ComponentPortal. */ @Component({ template: '

Pizza

', }) class PizzaMsg { - dialogRef = inject>(MatDialogRef); + dialogRef = inject>(MatDialogRef); dialogInjector = inject(Injector); directionality = inject(Directionality); } diff --git a/src/material/dialog/dialog.ts b/src/material/dialog/dialog.ts index 1452db39b75e..fee9c055612c 100644 --- a/src/material/dialog/dialog.ts +++ b/src/material/dialog/dialog.ts @@ -114,7 +114,7 @@ export class MatDialog implements OnDestroy { */ open( component: ComponentType, - config?: MatDialogConfig, + config?: MatDialogConfig, ): MatDialogRef; /**