Skip to content

Commit

Permalink
fix(cdk-experimental/dialog): disableClose not working for tem… (#18968)
Browse files Browse the repository at this point in the history
Fixes the `disableClose` option not working for template-based dialogs, because we have two separate places where the config was being synced up with the dialog ref and one of them wasn't updated. These changes move the creation logic to a single place.

Fixes #18964.
  • Loading branch information
crisbeto authored and mmalerba committed Apr 14, 2020
1 parent 4d22936 commit c0d19cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
19 changes: 19 additions & 0 deletions src/cdk-experimental/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,25 @@ describe('Dialog', () => {

expect(overlayContainerElement.querySelector('cdk-dialog-container')).toBeFalsy();
}));

it('should work when opening from a template', fakeAsync(() => {
const templateRefFixture = TestBed.createComponent(ComponentWithTemplateRef);
templateRefFixture.detectChanges();

dialog.openFromTemplate(templateRefFixture.componentInstance.templateRef, {
disableClose: true
});

templateRefFixture.detectChanges();

let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
backdrop.click();
templateRefFixture.detectChanges();
flush();

expect(overlayContainerElement.querySelector('cdk-dialog-container')).toBeTruthy();
}));

});

describe('hasBackdrop option', () => {
Expand Down
24 changes: 12 additions & 12 deletions src/cdk-experimental/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,11 @@ export class Dialog implements OnDestroy {

// Create a reference to the dialog we're creating in order to give the user a handle
// to modify and close it.
const dialogRef = new this._dialogRefConstructor(overlayRef, dialogContainer, config.id);
const dialogRef = this._createDialogRef(overlayRef, dialogContainer, config);
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);
const contentRef = dialogContainer.attachComponentPortal(
new ComponentPortal(componentOrTemplateRef, undefined, injector));

dialogRef.componentInstance = contentRef.instance;
dialogRef.disableClose = config.disableClose;

dialogRef.updateSize({width: config.width, height: config.height})
.updatePosition(config.position);

return dialogRef;
}

Expand All @@ -257,14 +251,10 @@ export class Dialog implements OnDestroy {

// Create a reference to the dialog we're creating in order to give the user a handle
// to modify and close it.
const dialogRef = new this._dialogRefConstructor(overlayRef, dialogContainer, config.id);

const dialogRef = this._createDialogRef(overlayRef, dialogContainer, config);
dialogContainer.attachTemplatePortal(
new TemplatePortal<T>(componentOrTemplateRef, null!,
<any>{$implicit: config.data, dialogRef}));
dialogRef.updateSize({width: config.width, height: config.height})
.updatePosition(config.position);

return dialogRef;
}

Expand Down Expand Up @@ -300,6 +290,16 @@ export class Dialog implements OnDestroy {
return new PortalInjector(userInjector || this._injector, injectionTokens);
}

/** Creates a new dialog ref. */
private _createDialogRef(overlayRef: OverlayRef,
dialogContainer: CdkDialogContainer,
config: DialogConfig) {
const dialogRef = new this._dialogRefConstructor(overlayRef, dialogContainer, config.id);
dialogRef.disableClose = config.disableClose;
dialogRef.updateSize(config).updatePosition(config.position);
return dialogRef;
}

/**
* Expands the provided configuration object to include the default values for properties which
* are undefined.
Expand Down

0 comments on commit c0d19cb

Please sign in to comment.