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 ?