Skip to content

Commit d61a26d

Browse files
authored
fix(material/dialog): do not expose CDK dialog ref to users (#33603)
Fixes that we were exposing both the CDK and Material dialog refs to users which can be confusing. Fixes #33601.
1 parent 017a4fc commit d61a26d

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/material/dialog/dialog.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
inject,
3232
inputBinding,
3333
} from '@angular/core';
34+
import {DialogRef} from '@angular/cdk/dialog';
3435
import {ComponentFixture, TestBed} from '@angular/core/testing';
3536
import {By} from '@angular/platform-browser';
3637
import {Subject} from 'rxjs';
@@ -873,6 +874,14 @@ describe('MatDialog', () => {
873874
expect(pane.classList).not.toContain('custom-class-one', 'Expected class to be removed');
874875
});
875876

877+
it('should not inject the CDK dialog ref into the child component', () => {
878+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
879+
viewContainerFixture.detectChanges();
880+
881+
expect(dialogRef.componentInstance.cdkDialogRef).toBe(null);
882+
expect(dialogRef.componentInstance.dialogRef).toBeTruthy();
883+
});
884+
876885
describe('disableClose option', () => {
877886
it('should prevent closing via clicks on the backdrop', async () => {
878887
dialog.open(PizzaMsg, {disableClose: true, viewContainerRef: testViewContainerRef});
@@ -2372,6 +2381,7 @@ class PizzaMsg {
23722381
dialogRef = inject<MatDialogRef<PizzaMsg>>(MatDialogRef);
23732382
dialogInjector = inject(Injector);
23742383
directionality = inject(Directionality);
2384+
cdkDialogRef = inject(DialogRef, {optional: true});
23752385
}
23762386

23772387
@Component({

src/material/dialog/dialog.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {MatDialogConfig} from './dialog-config';
2626
import {MatDialogContainer} from './dialog-container';
2727
import {MatDialogRef} from './dialog-ref';
2828
import {defer, Observable, Subject} from 'rxjs';
29-
import {Dialog, DialogConfig} from '@angular/cdk/dialog';
29+
import {Dialog, DialogConfig, DialogRef} from '@angular/cdk/dialog';
3030
import {startWith} from 'rxjs/operators';
3131
import {_IdGenerator} from '@angular/cdk/a11y';
3232
import {_animationsDisabled} from '../core';
@@ -178,6 +178,7 @@ export class MatDialog implements OnDestroy {
178178
{provide: this._dialogContainerType, useValue: dialogContainer},
179179
{provide: this._dialogDataToken, useValue: cdkConfig.data},
180180
{provide: this._dialogRefConstructor, useValue: dialogRef},
181+
{provide: DialogRef, useValue: null},
181182
];
182183
},
183184
});

0 commit comments

Comments
 (0)