Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a gracefully close form on hit escape key in Angular dialog material?

I have a dialog form and I want to have it closed gracefully when a user hits the escape key. When the user hits the escape key the form is shut down immediately but for some reason the dialog form doesn't send a result to the parent form.

There is no problem when the shutdown is done via the cancel button.

I have tried it with an "onKey" event on the userform component but that also doesn't work.

In my typescript and template dialog files:

import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';

constructor(
  private dialogRef: MatDialogRef<UpdateBonusConditieComponent>,
  @Inject(MAT_DIALOG_DATA) private PassedData: ConditieTypeDialog,
) {}  // I also tried it with a public PassedData instead of private

onCancel() {
  console.log('komt ie hier');
  this.PassedData.cancel = true;
}

onKey(event: any) { 
  this.onCancel();
  console.log('toets ingedrukt ' + event.target);
}

onOK() {
  console.log('OK button pressed');
}
<mat-dialog-content [formGroup]="dialogConditieForm"
                    (keyup)="onKey($event)">

</mat-dialog-content>

<mat-dialog-actions>
  <button mat-button
          color="accent"
          (click)="onOK()"
          [mat-dialog-close]="PassedData"
          [disabled]="dialogConditieForm.invalid">Ok</button>
  <button mat-button
          color="warn"
          (click)="onCancel()"
          [mat-dialog-close]="PassedData">Cancel</button>
</mat-dialog-actions>

Then I have the parent form where I call the dialog, some details:

import { MatDialog } from '@angular/material';

constructor(private matDialog: MatDialog) {}

const dialogRef = this.matDialog.open(UpdateBonusConditieComponent, {
  data: {
    onderwerpGUI: onderwerpGUI,
    isNewRow: isNewRow,
    cancel: false,
  }
});

dialogRef.afterClosed().subscribe((result: ConditieTypeDialog) => {
  if (result.cancel) { //when hitting escape result is undefined for some reason
    this.selectedRow = null;
    return 0;
  }
});

I should expect a result back so that the this.selectedRow is set to null but if the escape key is used to close the dialog form this does not happen.

I think I am doing something wrong. Can anybody can help me ?

like image 639
Ben Avatar asked Aug 24 '19 13:08

Ben


1 Answers

The dialog form is not sending a result because the onKey function is never getting called. You can instead subscribe to the keyboardEvents in MatDialogRef and call onCancel if Escape is clicked. I have also added the same for the backdropClick as that would be required as well.

constructor(
    private dialogRef: MatDialogRef<UpdateBonusConditieComponent>,
    @Inject(MAT_DIALOG_DATA) private passedData: ConditieTypeDialog,
) { }

ngOnInit() {
    this.dialogRef.keydownEvents().subscribe(event => {
        if (event.key === "Escape") {
            this.onCancel();
        }
    });

    this.dialogRef.backdropClick().subscribe(event => {
        this.onCancel();
    });
}

onCancel(): void {
    this.passedData.cancel = true;
    this.dialogRef.close(this.passedData);
}

onOK() {
    console.log('OK button pressed');
}

Also, as per convention, use camelCase for your variable names (passedData).

like image 90
nash11 Avatar answered Oct 19 '22 14:10

nash11