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

feat(snackbar): add onAction to snackbar ref #1826

Merged
merged 2 commits into from
Nov 15, 2016
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 @@ -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