RED-6960 - Enter should confirm a modal window by default
This commit is contained in:
parent
b4007f3fe5
commit
0885ad776d
@ -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();
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
export interface IqserEventTarget extends EventTarget {
|
||||
localName: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user