Skip to content

Commit a8d6c09

Browse files
committed
feat(snackbar): add onAction to snackbar ref
1 parent 243f98c commit a8d6c09

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/lib/snack-bar/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './snack-bar';
22
export * from './snack-bar-container';
33
export * from './snack-bar-config';
4-
export * from './snack-bar-ref';
4+
export * from './snack-bar-ref';

src/lib/snack-bar/simple-snack-bar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class SimpleSnackBar {
2424

2525
/** Dismisses the snack bar. */
2626
dismiss(): void {
27-
this.snackBarRef.dismiss();
27+
this.snackBarRef.action();
2828
}
2929

3030
/** If the action button should be shown. */

src/lib/snack-bar/snack-bar-ref.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ export class MdSnackBarRef<T> {
1919
/** Subject for notifying the user that the snack bar has closed. */
2020
private _afterClosed: Subject<any> = new Subject();
2121

22+
/** Subject for notifying the user that the snack bar action was called. */
23+
private _onAction: Subject<any> = new Subject();
24+
2225
constructor(instance: T,
2326
containerInstance: MdSnackBarContainer,
2427
private _overlayRef: OverlayRef) {
2528
// Sets the readonly instance of the snack bar content component.
2629
this.instance = instance;
2730
this.containerInstance = containerInstance;
31+
// Dismiss snackbar on action.
32+
this.onAction().subscribe(() => this.dismiss());
2833
}
2934

3035
/** Dismisses the snack bar. */
@@ -38,8 +43,21 @@ export class MdSnackBarRef<T> {
3843
}
3944
}
4045

46+
/** Marks the snackbar action clicked. */
47+
action(): void {
48+
if (!this._onAction.closed) {
49+
this._onAction.next();
50+
this._onAction.complete();
51+
}
52+
}
53+
4154
/** Gets an observable that is notified when the snack bar is finished closing. */
4255
afterDismissed(): Observable<void> {
4356
return this._afterClosed.asObservable();
4457
}
58+
59+
/** Gets an observable that is notified when the snack bar action is called. */
60+
onAction(): Observable<void> {
61+
return this._onAction.asObservable();
62+
}
4563
}

src/lib/snack-bar/snack-bar.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,32 @@ describe('MdSnackBar', () => {
259259
// Let remaining animations run.
260260
tick(500);
261261
}));
262+
263+
it('should dismiss the snackbar when the action is called, notifying of both action and dismiss',
264+
fakeAsync(() => {
265+
let dismissObservableCompleted = false;
266+
let actionObservableCompleted = false;
267+
let snackBarRef = snackBar.open('Some content', 'dismiss');
268+
viewContainerFixture.detectChanges();
269+
270+
snackBarRef.afterDismissed().subscribe(null, null, () => {
271+
dismissObservableCompleted = true;
272+
});
273+
snackBarRef.onAction().subscribe(null, null, () => {
274+
actionObservableCompleted = true;
275+
});
276+
277+
snackBarRef.action();
278+
viewContainerFixture.detectChanges();
279+
flushMicrotasks();
280+
281+
viewContainerFixture.whenStable().then(() => {
282+
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
283+
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');
284+
});
285+
286+
tick(500);
287+
}));
262288
});
263289

264290
@Directive({selector: 'dir-with-view-container'})

0 commit comments

Comments
 (0)