Skip to content

Commit c11e968

Browse files
crisbetoVivian Hu
authored and
Vivian Hu
committedJan 18, 2019
fix(bottom-sheet): allow disableClose to be updated after opened (#14711)
Allows for the `disableClose` value of a bottom sheet to be updated after it has been opened, similarly to `MatDialog`. Fixes #14708.
1 parent 21dc01c commit c11e968

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed
 

‎src/lib/bottom-sheet/bottom-sheet-ref.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export class MatBottomSheetRef<T = any, R = any> {
2727
*/
2828
containerInstance: MatBottomSheetContainer;
2929

30+
/** Whether the user is allowed to close the bottom sheet. */
31+
disableClose: boolean | undefined;
32+
3033
/** Subject for notifying the user that the bottom sheet has been dismissed. */
3134
private readonly _afterDismissed = new Subject<R | undefined>();
3235

@@ -42,6 +45,7 @@ export class MatBottomSheetRef<T = any, R = any> {
4245
// @breaking-change 8.0.0 `_location` parameter to be removed.
4346
_location?: Location) {
4447
this.containerInstance = containerInstance;
48+
this.disableClose = containerInstance.bottomSheetConfig.disableClose;
4549

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

67-
if (!containerInstance.bottomSheetConfig.disableClose) {
68-
merge(
69-
_overlayRef.backdropClick(),
70-
_overlayRef.keydownEvents().pipe(filter(event => event.keyCode === ESCAPE))
71-
).subscribe(() => this.dismiss());
72-
}
71+
merge(
72+
_overlayRef.backdropClick(),
73+
_overlayRef.keydownEvents().pipe(filter(event => event.keyCode === ESCAPE))
74+
).subscribe(() => {
75+
if (!this.disableClose) {
76+
this.dismiss();
77+
}
78+
});
7379
}
7480

7581
/**

‎src/lib/bottom-sheet/bottom-sheet.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,27 @@ describe('MatBottomSheet', () => {
459459
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
460460
}));
461461

462+
it('should allow for the disableClose option to be updated while open', fakeAsync(() => {
463+
let bottomSheetRef = bottomSheet.open(PizzaMsg, {
464+
disableClose: true,
465+
viewContainerRef: testViewContainerRef
466+
});
467+
468+
viewContainerFixture.detectChanges();
469+
470+
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement;
471+
backdrop.click();
472+
473+
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
474+
475+
bottomSheetRef.disableClose = false;
476+
backdrop.click();
477+
viewContainerFixture.detectChanges();
478+
flush();
479+
480+
expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
481+
}));
482+
462483
});
463484

464485
describe('hasBackdrop option', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.