Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/material/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface DialogPosition {
/**
* Configuration for opening a modal dialog with the MatDialog service.
*/
export class MatDialogConfig<D = any> {
export class MatDialogConfig<D = any, Component = unknown, Result = any> {
/**
* 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
Expand Down Expand Up @@ -69,13 +69,9 @@ export class MatDialogConfig<D = any> {
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;

Expand Down
21 changes: 18 additions & 3 deletions src/material/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, PizzaData, PizzaResult>(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,
});

Expand Down Expand Up @@ -2302,12 +2309,20 @@ class ComponentWithTemplateRef {
}
}

type PizzaData = {
myData?: string;
}

type PizzaResult = {
myResult: string;
}

/** Simple component for testing ComponentPortal. */
@Component({
template: '<p>Pizza</p> <input> <button>Close</button>',
})
class PizzaMsg {
dialogRef = inject<MatDialogRef<PizzaMsg>>(MatDialogRef);
dialogRef = inject<MatDialogRef<PizzaMsg, PizzaResult>>(MatDialogRef);
dialogInjector = inject(Injector);
directionality = inject(Directionality);
}
Expand Down
2 changes: 1 addition & 1 deletion src/material/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class MatDialog implements OnDestroy {
*/
open<T, D = any, R = any>(
component: ComponentType<T>,
config?: MatDialogConfig<D>,
config?: MatDialogConfig<D, T, R>,
): MatDialogRef<T, R>;

/**
Expand Down
Loading