Skip to content

Commit

Permalink
feat(snackbar): add onAction to snackbar ref (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephperrott authored and kara committed Nov 15, 2016
1 parent 8b08f69 commit a40cae9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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 @@ -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. */
Expand Down
18 changes: 18 additions & 0 deletions src/lib/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ export class MdSnackBarRef<T> {
/** Subject for notifying the user that the snack bar has closed. */
private _afterClosed: Subject<any> = new Subject();

/** Subject for notifying the user that the snack bar action was called. */
private _onAction: Subject<any> = 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. */
Expand All @@ -38,8 +43,21 @@ export class MdSnackBarRef<T> {
}
}

/** 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<void> {
return this._afterClosed.asObservable();
}

/** Gets an observable that is notified when the snack bar action is called. */
onAction(): Observable<void> {
return this._onAction.asObservable();
}
}
28 changes: 28 additions & 0 deletions src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down

0 comments on commit a40cae9

Please sign in to comment.