diff --git a/src/lib/snack-bar/index.ts b/src/lib/snack-bar/index.ts index 4a20b47466ac..b07c7374a06e 100644 --- a/src/lib/snack-bar/index.ts +++ b/src/lib/snack-bar/index.ts @@ -1,4 +1,4 @@ export * from './snack-bar'; export * from './snack-bar-container'; export * from './snack-bar-config'; -export * from './snack-bar-ref'; \ No newline at end of file +export * from './snack-bar-ref'; diff --git a/src/lib/snack-bar/simple-snack-bar.ts b/src/lib/snack-bar/simple-snack-bar.ts index d74d6736c3df..a9a1f5d13132 100644 --- a/src/lib/snack-bar/simple-snack-bar.ts +++ b/src/lib/snack-bar/simple-snack-bar.ts @@ -24,7 +24,7 @@ export class SimpleSnackBar { /** Dismisses the snack bar. */ dismiss(): void { - this.snackBarRef.dismiss(); + this.snackBarRef.action_(); } /** If the action button should be shown. */ diff --git a/src/lib/snack-bar/snack-bar-ref.ts b/src/lib/snack-bar/snack-bar-ref.ts index 5b088df78c4f..1c54726efcfa 100644 --- a/src/lib/snack-bar/snack-bar-ref.ts +++ b/src/lib/snack-bar/snack-bar-ref.ts @@ -19,12 +19,17 @@ export class MdSnackBarRef { /** Subject for notifying the user that the snack bar has closed. */ private _afterClosed: Subject = new Subject(); + /** Subject for notifying the user that the snack bar action was called. */ + private _onAction: Subject = new Subject(); + constructor(instance: T, containerInstance: MdSnackBarContainer, private _overlayRef: OverlayRef) { // Sets the readonly instance of the snack bar content component. this.instance = instance; this.containerInstance = containerInstance; + // Dismiss snackbar on action. + this.onAction().subscribe(() => this.dismiss()); } /** Dismisses the snack bar. */ @@ -38,8 +43,21 @@ export class MdSnackBarRef { } } + /** Marks the snackbar action clicked. */ + action_(): void { + if (!this._onAction.closed) { + this._onAction.next(); + this._onAction.complete(); + } + } + /** Gets an observable that is notified when the snack bar is finished closing. */ afterDismissed(): Observable { return this._afterClosed.asObservable(); } + + /** Gets an observable that is notified when the snack bar action is called. */ + onAction(): Observable { + return this._onAction.asObservable(); + } } diff --git a/src/lib/snack-bar/snack-bar.spec.ts b/src/lib/snack-bar/snack-bar.spec.ts index 11b50d8f7ffd..5640fedaa0cc 100644 --- a/src/lib/snack-bar/snack-bar.spec.ts +++ b/src/lib/snack-bar/snack-bar.spec.ts @@ -259,6 +259,34 @@ describe('MdSnackBar', () => { // Let remaining animations run. tick(500); })); + + it('should dismiss the snackbar when the action is called, notifying of both action and dismiss', + fakeAsync(() => { + let dismissObservableCompleted = false; + let actionObservableCompleted = false; + let snackBarRef = snackBar.open('Some content', 'dismiss'); + viewContainerFixture.detectChanges(); + + snackBarRef.afterDismissed().subscribe(null, null, () => { + dismissObservableCompleted = true; + }); + snackBarRef.onAction().subscribe(null, null, () => { + actionObservableCompleted = true; + }); + + let actionButton = + overlayContainerElement.querySelector('.md-simple-snackbar-action') as HTMLButtonElement; + actionButton.click(); + viewContainerFixture.detectChanges(); + flushMicrotasks(); + + viewContainerFixture.whenStable().then(() => { + expect(dismissObservableCompleted).toBeTruthy('Expected the snack bar to be dismissed'); + expect(actionObservableCompleted).toBeTruthy('Expected the snack bar to notify of action'); + }); + + tick(500); + })); }); @Directive({selector: 'dir-with-view-container'})