RED-6960 - Enter should confirm a modal window by default

This commit is contained in:
Valentin Mihai 2024-02-06 20:13:52 +02:00
parent b4007f3fe5
commit 0885ad776d
3 changed files with 23 additions and 6 deletions

View File

@ -10,7 +10,8 @@ import { Toaster } from '../services';
import { hasFormChanged, IqserEventTarget } from '../utils';
import { ConfirmationDialogService } from './confirmation-dialog.service';
const TARGET_NODE = 'mat-dialog-container';
const DIALOG_CONTAINER = 'mat-dialog-container';
const TEXT_INPUT = 'text';
export interface SaveOptions {
closeAfterSave?: boolean;
@ -84,8 +85,17 @@ export abstract class BaseDialogComponent implements AfterViewInit, OnDestroy {
@HostListener('window:keydown.Enter', ['$event'])
onEnter(event: KeyboardEvent): void {
const node = (event.target as IqserEventTarget).localName?.trim()?.toLowerCase();
if (this.valid && !this.disabled && this.changed && node === TARGET_NODE && this.#dialog.openDialogs.length === 1) {
const target = event.target as IqserEventTarget;
const isDialogSelected = target.localName?.trim()?.toLowerCase() === DIALOG_CONTAINER;
const isTextInputSelected = target.type?.trim()?.toLowerCase() === TEXT_INPUT;
if (
this.valid &&
!this.disabled &&
this.changed &&
this.#dialog.openDialogs.length === 1 &&
(isDialogSelected || isTextInputSelected)
) {
event?.stopImmediatePropagation();
this.save();
}

View File

@ -1,9 +1,10 @@
import { Directive, HostListener, inject } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { hasFormChanged } from '../utils';
import { hasFormChanged, IqserEventTarget } from '../utils';
import { FormGroup } from '@angular/forms';
const DIALOG_CONTAINER = 'mat-dialog-container';
const DATA_TYPE_SYMBOL = Symbol.for('DATA_TYPE');
const RETURN_TYPE_SYMBOL = Symbol.for('RETURN_TYPE');
@ -21,7 +22,7 @@ export abstract class IqserDialogComponent<ComponentType, DataType, ReturnType>
readonly form?: FormGroup;
initialFormValue: Record<string, unknown> = {};
constructor() {
constructor(private readonly _editMode = false) {
this.dialogRef
.backdropClick()
.pipe(takeUntilDestroyed())
@ -57,7 +58,12 @@ export abstract class IqserDialogComponent<ComponentType, DataType, ReturnType>
}
onEnterValidator(event: KeyboardEvent) {
return this.valid && !this.disabled && this.changed;
const targetElement = (event.target as IqserEventTarget).localName?.trim()?.toLowerCase();
const canClose = targetElement === DIALOG_CONTAINER && this.valid;
if (this._editMode) {
return canClose && this.changed;
}
return canClose;
}
close(dialogResult?: ReturnType) {

View File

@ -1,3 +1,4 @@
export interface IqserEventTarget extends EventTarget {
localName: string;
type: string;
}