diff --git a/src/lib/dialog/base-dialog.component.ts b/src/lib/dialog/base-dialog.component.ts index c1cb094..e89d880 100644 --- a/src/lib/dialog/base-dialog.component.ts +++ b/src/lib/dialog/base-dialog.component.ts @@ -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(); } diff --git a/src/lib/dialog/iqser-dialog-component.directive.ts b/src/lib/dialog/iqser-dialog-component.directive.ts index 8757ff8..9a2e4f2 100644 --- a/src/lib/dialog/iqser-dialog-component.directive.ts +++ b/src/lib/dialog/iqser-dialog-component.directive.ts @@ -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 readonly form?: FormGroup; initialFormValue: Record = {}; - constructor() { + constructor(private readonly _editMode = false) { this.dialogRef .backdropClick() .pipe(takeUntilDestroyed()) @@ -57,7 +58,12 @@ export abstract class IqserDialogComponent } 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) { diff --git a/src/lib/utils/types/events.type.ts b/src/lib/utils/types/events.type.ts index 2d5c97d..93dea19 100644 --- a/src/lib/utils/types/events.type.ts +++ b/src/lib/utils/types/events.type.ts @@ -1,3 +1,4 @@ export interface IqserEventTarget extends EventTarget { localName: string; + type: string; }