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(dialog): Pass an observable to the mat-dialog-close directive #20571

Closed
bampakoa opened this issue Sep 15, 2020 · 2 comments
Closed

feat(dialog): Pass an observable to the mat-dialog-close directive #20571

bampakoa opened this issue Sep 15, 2020 · 2 comments
Labels
area: material/dialog feature This issue represents a new feature or feature request rather than a bug or bug fix

Comments

@bampakoa
Copy link
Contributor

Feature Description

It would be great if we could pass an Observable into the mat-dialog-close directive so that the dialog can be closed when the observable emits.

Use Case

In our codebase, we use many dialog components for filling up forms. These components usually have the following actions:

<mat-dialog-actions>
  <button mat-button mat-dialog-close>Cancel</button>
  <button mat-button (click)="save()">Save</button>
</mat-dialog-actions> 

The save method usually makes an HTTP POST request to a backend API and, when completes, it closes the dialog using the close method of the MatDialogRef service.

It would be awesome if we could avoid all the boilerplate code of using the MatDialogRef service which also adds a bit of noise in our constructors. Injecting MatDialogRef service as private dialogRef: MatDialogRef<MyComponent> quickly makes our constructor violate the max-line-length rule.

@bampakoa bampakoa added feature This issue represents a new feature or feature request rather than a bug or bug fix needs triage This issue needs to be triaged by the team labels Sep 15, 2020
@crisbeto
Copy link
Member

I'm not sure that this makes sense from an API standpoint, because passing in an observable to the button is no different than doing stream.subscribe(() => dialog.close()) yourself. We already have the APIs so that you can implement it yourself and tailor it to your app's needs. You can do something like this:

@Directive({
  selector: '[close-on-emit]' // Or alternatively you can target `[mat-dialog-close]`
})
class CloseOnEmit {
  @Input('close-on-emit') stream: Observable<unknown>;

  constructor(private _dialog: MatDialog) {}

  ngOnChanges() {
    // TODO: also needs to unsubscribe
    this.stream.subscribe(() => {
      const dialogs = this._dialog.openDialogs;
      if (dialogs.length) {
        dialogs[dialogs.length - 1].close();
      }
    });
  }
}

Closing the issue since the feature request is too narrow to build into Material.

@crisbeto crisbeto added area: material/dialog and removed needs triage This issue needs to be triaged by the team labels Sep 20, 2020
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Oct 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: material/dialog feature This issue represents a new feature or feature request rather than a bug or bug fix
Projects
None yet
Development

No branches or pull requests

2 participants