Skip to content

Commit

Permalink
feat(snackbar): add onAction to snackbar ref
Browse files Browse the repository at this point in the history
  • Loading branch information
josephperrott committed Nov 11, 2016
1 parent 243f98c commit 78f970a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/snack-bar/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './snack-bar';
export * from './snack-bar-container';
export * from './snack-bar-config';
export * from './snack-bar-ref';
export * from './snack-bar-ref';
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 78f970a

Please sign in to comment.