Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snack-bar): indicate in afterDismissed whether dismissal was a result of an action #9154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/snack-bar/simple-snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class SimpleSnackBar {

/** Performs the action on the snack bar. */
action(): void {
this.snackBarRef.closeWithAction();
this.snackBarRef.dismissWithAction();
}

/** If the action button should be shown. */
Expand Down
36 changes: 28 additions & 8 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 dismissed using the action button. */
dismissedByAction: boolean;
}

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

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

/** Subject for notifying the user that the snack bar has opened and appeared. */
private _afterOpened = new Subject<void>();
Expand All @@ -39,6 +45,9 @@ export class MatSnackBarRef<T> {
*/
private _durationTimeoutId: number;

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

constructor(containerInstance: MatSnackBarContainer,
private _overlayRef: OverlayRef) {
this.containerInstance = containerInstance;
Expand All @@ -49,20 +58,30 @@ export class MatSnackBarRef<T> {

/** Dismisses the snack bar. */
dismiss(): void {
if (!this._afterClosed.closed) {
if (!this._afterDismissed.closed) {
this.containerInstance.exit();
}
clearTimeout(this._durationTimeoutId);
}

/** Marks the snackbar action clicked. */
closeWithAction(): void {
dismissWithAction(): void {
if (!this._onAction.closed) {
this._dismissedByAction = true;
this._onAction.next();
this._onAction.complete();
}
}


/**
* Marks the snackbar action clicked.
* @deprecated Use `dismissWithAction` instead.
*/
closeWithAction(): void {
this.dismissWithAction();
}

/** Dismisses the snack bar after some duration */
_dismissAfter(duration: number): void {
this._durationTimeoutId = setTimeout(() => this.dismiss(), duration);
Expand All @@ -84,13 +103,14 @@ 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();
afterDismissed(): Observable<MatSnackBarDismiss> {
return this._afterDismissed.asObservable();
}

/** Gets an observable that is notified when the snack bar has opened and appeared. */
Expand Down
23 changes: 19 additions & 4 deletions src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ describe('MatSnackBar', () => {
tick(500);
}));

it('should allow manually closing with an action', fakeAsync(() => {
it('should allow manually dismissing with an action', fakeAsync(() => {
const dismissCompleteSpy = jasmine.createSpy('dismiss complete spy');
const actionCompleteSpy = jasmine.createSpy('action complete spy');
const snackBarRef = snackBar.open('Some content');
Expand All @@ -297,7 +297,7 @@ describe('MatSnackBar', () => {
snackBarRef.afterDismissed().subscribe(undefined, undefined, dismissCompleteSpy);
snackBarRef.onAction().subscribe(undefined, undefined, actionCompleteSpy);

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

Expand All @@ -307,6 +307,21 @@ describe('MatSnackBar', () => {
tick(500);
}));

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

snackBarRef.afterDismissed().subscribe(closeSpy);

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

expect(closeSpy).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 @@ -401,7 +416,7 @@ describe('MatSnackBar', () => {
.toBe('Chimichanga', 'Expected the injected data object to be the one the user provided.');
});

it('should allow manually closing with an action', fakeAsync(() => {
it('should allow manually dismissing with an action', fakeAsync(() => {
const dismissCompleteSpy = jasmine.createSpy('dismiss complete spy');
const actionCompleteSpy = jasmine.createSpy('action complete spy');
const snackBarRef = snackBar.openFromComponent(BurritosNotification);
Expand All @@ -410,7 +425,7 @@ describe('MatSnackBar', () => {
snackBarRef.afterDismissed().subscribe(undefined, undefined, dismissCompleteSpy);
snackBarRef.onAction().subscribe(undefined, undefined, actionCompleteSpy);

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

Expand Down