Skip to content

Commit 98a6910

Browse files
crisbetojelbourn
authored andcommitted
feat(bottom-sheet): add result param when dismissing bottom sheet (#9810)
Something that came up while doing the docs. Adds the ability for the consumer to pass back a result from a bottom sheet. Also adds a test for `afterDismissed` since it didn't have one.
1 parent dcd9c79 commit 98a6910

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {MatBottomSheetContainer} from './bottom-sheet-container';
1818
/**
1919
* Reference to a bottom sheet dispatched from the bottom sheet service.
2020
*/
21-
export class MatBottomSheetRef<T = any> {
21+
export class MatBottomSheetRef<T = any, R = any> {
2222
/** Instance of the component making up the content of the bottom sheet. */
2323
instance: T;
2424

@@ -29,11 +29,14 @@ export class MatBottomSheetRef<T = any> {
2929
containerInstance: MatBottomSheetContainer;
3030

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

3434
/** Subject for notifying the user that the bottom sheet has opened and appeared. */
3535
private readonly _afterOpened = new Subject<void>();
3636

37+
/** Result to be passed down to the `afterDismissed` stream. */
38+
private _result: R | undefined;
39+
3740
constructor(containerInstance: MatBottomSheetContainer, private _overlayRef: OverlayRef) {
3841
this.containerInstance = containerInstance;
3942

@@ -54,7 +57,7 @@ export class MatBottomSheetRef<T = any> {
5457
)
5558
.subscribe(() => {
5659
this._overlayRef.dispose();
57-
this._afterDismissed.next();
60+
this._afterDismissed.next(this._result);
5861
this._afterDismissed.complete();
5962
});
6063

@@ -66,21 +69,25 @@ export class MatBottomSheetRef<T = any> {
6669
}
6770
}
6871

69-
/** Dismisses the bottom sheet. */
70-
dismiss(): void {
72+
/**
73+
* Dismisses the bottom sheet.
74+
* @param result Data to be passed back to the bottom sheet opener.
75+
*/
76+
dismiss(result?: R): void {
7177
if (!this._afterDismissed.closed) {
7278
// Transition the backdrop in parallel to the bottom sheet.
7379
this.containerInstance._animationStateChanged.pipe(
7480
filter(event => event.phaseName === 'start'),
7581
take(1)
7682
).subscribe(() => this._overlayRef.detachBackdrop());
7783

84+
this._result = result;
7885
this.containerInstance.exit();
7986
}
8087
}
8188

8289
/** Gets an observable that is notified when the bottom sheet is finished closing. */
83-
afterDismissed(): Observable<void> {
90+
afterDismissed(): Observable<R | undefined> {
8491
return this._afterDismissed.asObservable();
8592
}
8693

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,34 @@ describe('MatBottomSheet', () => {
295295
tick(500);
296296
}));
297297

298+
it('should emit after being dismissed', fakeAsync(() => {
299+
const bottomSheetRef = bottomSheet.open(PizzaMsg);
300+
const spy = jasmine.createSpy('afterDismissed spy');
301+
302+
bottomSheetRef.afterDismissed().subscribe(spy);
303+
viewContainerFixture.detectChanges();
304+
305+
bottomSheetRef.dismiss();
306+
viewContainerFixture.detectChanges();
307+
flush();
308+
309+
expect(spy).toHaveBeenCalledTimes(1);
310+
}));
311+
312+
it('should be able to pass a result back to the dismissed stream', fakeAsync(() => {
313+
const bottomSheetRef = bottomSheet.open<PizzaMsg, any, number>(PizzaMsg);
314+
const spy = jasmine.createSpy('afterDismissed spy');
315+
316+
bottomSheetRef.afterDismissed().subscribe(spy);
317+
viewContainerFixture.detectChanges();
318+
319+
bottomSheetRef.dismiss(1337);
320+
viewContainerFixture.detectChanges();
321+
flush();
322+
323+
expect(spy).toHaveBeenCalledWith(1337);
324+
}));
325+
298326
describe('passing in data', () => {
299327
it('should be able to pass in data', () => {
300328
const config = {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ export class MatBottomSheet {
3939
private _injector: Injector,
4040
@Optional() @SkipSelf() private _parentBottomSheet: MatBottomSheet) {}
4141

42-
open<T, D = any>(component: ComponentType<T>,
43-
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T>;
44-
open<T, D = any>(template: TemplateRef<T>,
45-
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T>;
42+
open<T, D = any, R = any>(component: ComponentType<T>,
43+
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T, R>;
44+
open<T, D = any, R = any>(template: TemplateRef<T>,
45+
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T, R>;
4646

47-
open<T, D = any>(componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
48-
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T> {
47+
open<T, D = any, R = any>(componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
48+
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T, R> {
4949

5050
const _config = _applyConfigDefaults(config);
5151
const overlayRef = this._createOverlay(_config);
5252
const container = this._attachContainer(overlayRef, _config);
53-
const ref = new MatBottomSheetRef<T>(container, overlayRef);
53+
const ref = new MatBottomSheetRef<T, R>(container, overlayRef);
5454

5555
if (componentOrTemplateRef instanceof TemplateRef) {
5656
container.attachTemplatePortal(new TemplatePortal<T>(componentOrTemplateRef, null!, {

0 commit comments

Comments
 (0)