Skip to content

Commit a40cae9

Browse files
josephperrottkara
authored andcommitted
feat(snackbar): add onAction to snackbar ref (#1826)
1 parent 8b08f69 commit a40cae9

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,34 @@ 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+
let actionButton =
278+
overlayContainerElement.querySelector('.md-simple-snackbar-action') as HTMLButtonElement;
279+
actionButton.click();
280+
viewContainerFixture.detectChanges();
281+
flushMicrotasks();
282+
283+
viewContainerFixture.whenStable().then(() => {
284+
expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed');
285+
expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action');
286+
});
287+
288+
tick(500);
289+
}));
262290
});
263291

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

0 commit comments

Comments
 (0)