Skip to content

Commit

Permalink
fix(snack-bar): indicate in afterDismissed whether dismissal was a re…
Browse files Browse the repository at this point in the history
…sult of an action

Adds a boolean to the `MatSnackBarRef.afterDismissed` result that indicates whether the dismissal came as a result of the user pressing the action button.

Fixes #9147.
  • Loading branch information
crisbeto committed Jan 17, 2018
1 parent c3d7cd9 commit 045aefb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
27 changes: 20 additions & 7 deletions src/lib/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {MatSnackBarContainer} from './snack-bar-container';

/** Event that is emitted when a snack bar is dismissed. */
export interface MatSnackBarDismiss {
/** Whether the snack bar was dismissied using the action button. */
dismissedByAction: boolean;
}

/**
* Reference to a snack bar dispatched from the snack bar service.
*/
Expand All @@ -25,14 +31,17 @@ export class MatSnackBarRef<T> {
containerInstance: MatSnackBarContainer;

/** Subject for notifying the user that the snack bar has closed. */
private _afterClosed = new Subject<void>();
private _afterDismissed = new Subject<MatSnackBarDismiss>();

/** Subject for notifying the user that the snack bar has opened and appeared. */
private _afterOpened = new Subject<void>();

/** Subject for notifying the user that the snack bar action was called. */
private _onAction = new Subject<void>();

/** Whether the snack bar was closed using the action button. */
private _dismissedByAction = false;

/**
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
* dismissed before the duration passes.
Expand All @@ -49,7 +58,7 @@ export class MatSnackBarRef<T> {

/** Dismisses the snack bar. */
dismiss(): void {
if (!this._afterClosed.closed) {
if (!this._afterDismissed.closed) {
this.containerInstance.exit();
}
clearTimeout(this._durationTimeoutId);
Expand All @@ -58,6 +67,7 @@ export class MatSnackBarRef<T> {
/** Marks the snackbar action clicked. */
closeWithAction(): void {
if (!this._onAction.closed) {
this._dismissedByAction = true;
this._onAction.next();
this._onAction.complete();
}
Expand All @@ -84,13 +94,16 @@ export class MatSnackBarRef<T> {
this._onAction.complete();
}

this._afterClosed.next();
this._afterClosed.complete();
this._afterDismissed.next({dismissedByAction: this._dismissedByAction});
this._afterDismissed.complete();
this._dismissedByAction = false;
}

/** Gets an observable that is notified when the snack bar is finished closing. */
afterDismissed(): Observable<void> {
return this._afterClosed.asObservable();
/**
* Gets an observable that is notified when the snack bar is finished closing.
*/
afterDismissed(): Observable<MatSnackBarDismiss> {
return this._afterDismissed.asObservable();
}

/** Gets an observable that is notified when the snack bar has opened and appeared. */
Expand Down
16 changes: 15 additions & 1 deletion src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,21 @@ describe('MatSnackBar', () => {
tick(500);
}));

it('should indicate in `afterDismissed` whether it was dismissed by an action', fakeAsync(() => {
const dismissSpy = jasmine.createSpy('dismiss spy');
const snackBarRef = snackBar.open('Some content');
viewContainerFixture.detectChanges();

snackBarRef.afterDismissed().subscribe(dismissSpy);

snackBarRef.closeWithAction();
viewContainerFixture.detectChanges();
tick();

expect(dismissSpy).toHaveBeenCalledWith(jasmine.objectContaining({dismissedByAction: true}));
tick(500);
}));

it('should complete the onAction stream when not closing via an action', fakeAsync(() => {
const actionCompleteSpy = jasmine.createSpy('action complete spy');
const snackBarRef = snackBar.open('Some content');
Expand Down Expand Up @@ -489,7 +504,6 @@ describe('MatSnackBar with parent MatSnackBar', () => {
}));
});


describe('MatSnackBar Positioning', () => {
let snackBar: MatSnackBar;
let liveAnnouncer: LiveAnnouncer;
Expand Down

0 comments on commit 045aefb

Please sign in to comment.