Skip to content

Commit 879507a

Browse files
committed
feat(material/dialog): Let types flow to MatDialogConfig.closePredicate() WIP
The goal is to be able to use `MatDialogConfig.closePredicate()` with proper typing without repeating them, or without any casting. This is a work in progress, because even though the modified test would compile, and most likely pass, other places in the codebase are broken by the breaking change introduced in `MatDialogConfig`. So I would like to ask the opinion of the Angular team here. Is there any chance to have something similar merged, if it gets polished up ? Fixes #31873
1 parent 48e1b35 commit 879507a

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

src/material/dialog/dialog-config.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface DialogPosition {
3535
/**
3636
* Configuration for opening a modal dialog with the MatDialog service.
3737
*/
38-
export class MatDialogConfig<D = any> {
38+
export class MatDialogConfig<D = any, Component = unknown, Result = any> {
3939
/**
4040
* Where the attached component should live in Angular's *logical* component tree.
4141
* This affects what is available for injection and the change detection order for the
@@ -69,13 +69,9 @@ export class MatDialogConfig<D = any> {
6969
disableClose?: boolean = false;
7070

7171
/** Function used to determine whether the dialog is allowed to close. */
72-
closePredicate?: <
73-
Result = unknown,
74-
Component = unknown,
75-
Config extends DialogConfig = MatDialogConfig,
76-
>(
72+
closePredicate?: (
7773
result: Result | undefined,
78-
config: Config,
74+
config: this,
7975
componentInstance: Component | null,
8076
) => boolean;
8177

src/material/dialog/dialog.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,15 @@ describe('MatDialog', () => {
10171017
it('should determine whether closing via the backdrop is allowed', fakeAsync(() => {
10181018
let canClose = false;
10191019
const closedSpy = jasmine.createSpy('closed spy');
1020-
const ref = dialog.open(PizzaMsg, {
1021-
closePredicate: () => canClose,
1020+
const ref = dialog.open<PizzaMsg, PizzaData, PizzaResult>(PizzaMsg, {
1021+
data: {myData: 'my data'},
1022+
closePredicate: (result, config, componentInstance) => {
1023+
expect(result?.myResult).toBe('');
1024+
expect(config.data).toBe({myData: 'my data'});
1025+
expect(componentInstance?.dialogRef).toBeInstanceOf(MatDialogRef);
1026+
1027+
return canClose;
1028+
},
10221029
viewContainerRef: testViewContainerRef,
10231030
});
10241031

@@ -2302,12 +2309,20 @@ class ComponentWithTemplateRef {
23022309
}
23032310
}
23042311

2312+
type PizzaData = {
2313+
myData?: string;
2314+
}
2315+
2316+
type PizzaResult = {
2317+
myResult: string;
2318+
}
2319+
23052320
/** Simple component for testing ComponentPortal. */
23062321
@Component({
23072322
template: '<p>Pizza</p> <input> <button>Close</button>',
23082323
})
23092324
class PizzaMsg {
2310-
dialogRef = inject<MatDialogRef<PizzaMsg>>(MatDialogRef);
2325+
dialogRef = inject<MatDialogRef<PizzaMsg, PizzaResult>>(MatDialogRef);
23112326
dialogInjector = inject(Injector);
23122327
directionality = inject(Directionality);
23132328
}

src/material/dialog/dialog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class MatDialog implements OnDestroy {
114114
*/
115115
open<T, D = any, R = any>(
116116
component: ComponentType<T>,
117-
config?: MatDialogConfig<D>,
117+
config?: MatDialogConfig<D, T, R>,
118118
): MatDialogRef<T, R>;
119119

120120
/**

0 commit comments

Comments
 (0)