Skip to content

fix(bottom-sheet): allow disableClose to be updated after opened #14711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
18 changes: 12 additions & 6 deletions src/lib/bottom-sheet/bottom-sheet-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class MatBottomSheetRef<T = any, R = any> {
*/
containerInstance: MatBottomSheetContainer;

/** Whether the user is allowed to close the bottom sheet. */
disableClose: boolean | undefined;

/** Subject for notifying the user that the bottom sheet has been dismissed. */
private readonly _afterDismissed = new Subject<R | undefined>();

Expand All @@ -42,6 +45,7 @@ export class MatBottomSheetRef<T = any, R = any> {
// @breaking-change 8.0.0 `_location` parameter to be removed.
_location?: Location) {
this.containerInstance = containerInstance;
this.disableClose = containerInstance.bottomSheetConfig.disableClose;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this potentially change the behavior since you are setting the value in the class instead of reading it from the object at the time of the check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't change the behavior, because the old approach only read it once on init.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, yes because it set up the subscription based on that. Makes sense.


// Emit when opening animation completes
containerInstance._animationStateChanged.pipe(
Expand All @@ -64,12 +68,14 @@ export class MatBottomSheetRef<T = any, R = any> {
this._afterDismissed.complete();
});

if (!containerInstance.bottomSheetConfig.disableClose) {
merge(
_overlayRef.backdropClick(),
_overlayRef.keydownEvents().pipe(filter(event => event.keyCode === ESCAPE))
).subscribe(() => this.dismiss());
}
merge(
_overlayRef.backdropClick(),
_overlayRef.keydownEvents().pipe(filter(event => event.keyCode === ESCAPE))
).subscribe(() => {
if (!this.disableClose) {
this.dismiss();
}
});
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/lib/bottom-sheet/bottom-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,27 @@ describe('MatBottomSheet', () => {
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
}));

it('should allow for the disableClose option to be updated while open', fakeAsync(() => {
let bottomSheetRef = bottomSheet.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

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

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();

bottomSheetRef.disableClose = false;
backdrop.click();
viewContainerFixture.detectChanges();
flush();

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));

});

describe('hasBackdrop option', () => {
Expand Down