[251] Dialoge: Enter löst die Hauptaktion nicht mehr auf „Abbrechen“ aus; e2e für weitere Dialoge
This commit is contained in:
6 files changed
+150
-6
No files matched your search
@@ -9,7 +9,7 @@
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { t } from "../i18n";
|
||||
import { useModalFocus } from "./modalFocus";
|
||||
import { enterConfirmsDialog, useModalFocus } from "./modalFocus";
|
||||
import type {
|
||||
LayoutOrientation,
|
||||
LayoutPaperFormat,
|
||||
@@ -163,7 +163,7 @@ function DialogFrame({
|
||||
useEffect(() => {
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") onClose();
|
||||
else if (e.key === "Enter") onConfirm();
|
||||
else if (e.key === "Enter" && enterConfirmsDialog(e.target)) onConfirm();
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import { memo, useCallback, useEffect, useState } from "react";
|
||||
import { t } from "../i18n";
|
||||
import { useModalFocus } from "./modalFocus";
|
||||
import { enterConfirmsDialog, useModalFocus } from "./modalFocus";
|
||||
import { PRESENTATION_SCALES, levelHasContent, presentationDefaults, type PresentationOptions } from "../core";
|
||||
import type { LayoutOrientation, LayoutPaperFormat } from "../model/types";
|
||||
import { PAPER_FORMAT_GROUPS } from "../model/layoutModel";
|
||||
@@ -44,7 +44,7 @@ export const PresentationDialogHost = memo(function PresentationDialogHost({
|
||||
// Enter aus der Befehlszeile (die den Dialog eben geöffnet hat) bestätigt nicht.
|
||||
if (e.target instanceof Element && e.target.closest("input, textarea") && !e.target.closest(".presentation-dialog")) return;
|
||||
if (e.key === "Escape") close();
|
||||
else if (e.key === "Enter") confirm();
|
||||
else if (e.key === "Enter" && enterConfirmsDialog(e.target)) confirm();
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// [249] Reine Logik der Dialog-Fokushülle: Tab-Falle, Enter-Felder, Hintergrund-Kürzel-Sperre, Walk-Sperre im Dialog.
|
||||
// Ohne DOM (Umgebung node): Elemente sind Attrappen mit `closest`/`tagName`.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { enterTriggersPrimary, trapTarget } from "./modalFocus";
|
||||
import { enterConfirmsDialog, enterTriggersPrimary, trapTarget } from "./modalFocus";
|
||||
import { isInModalDialog } from "../state/modalScope";
|
||||
import { walkKeyAllowed } from "../viewport/walk";
|
||||
|
||||
@@ -56,3 +56,17 @@ describe("Hintergrund-Kürzel im modalen Dialog", () => {
|
||||
expect(walkKeyAllowed(k, el("BUTTON", false))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("enterConfirmsDialog ([251])", () => {
|
||||
const inside = (tag: string, withinButton = false) =>
|
||||
({ tagName: tag, closest: (sel: string) => (withinButton && sel.includes("button") ? {} : null) }) as unknown as Element;
|
||||
it("Eingabe-, Auswahlfeld und Dokument bestätigen", () => {
|
||||
expect(enterConfirmsDialog(inside("INPUT"))).toBe(true);
|
||||
expect(enterConfirmsDialog(inside("SELECT"))).toBe(true);
|
||||
expect(enterConfirmsDialog(null)).toBe(true);
|
||||
});
|
||||
it("Knöpfe (auch Kinder darin) behalten ihre eigene Aktion", () => {
|
||||
expect(enterConfirmsDialog(inside("BUTTON", true))).toBe(false);
|
||||
expect(enterConfirmsDialog(inside("SPAN", true))).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -43,6 +43,17 @@ export function enterTriggersPrimary(el: Element | null): boolean {
|
||||
return type === "text" || type === "number" || type === "search" || type === "email" || type === "url";
|
||||
}
|
||||
|
||||
/**
|
||||
* [251] Darf ein `window`-Enter-Listener eines Dialogs die Hauptaktion auslösen? Nicht, wenn der Fokus auf einem Knopf,
|
||||
* Link, mehrzeiligen Text oder Aufklapper liegt: dort gilt Enters eigene Aktion (Knopf = Klick, auch beim Hauptknopf, dessen
|
||||
* Klick die Aktion schon auslöst). Eingabefelder, Auswahlfelder, Haken und das Dokument lösen aus.
|
||||
*/
|
||||
export function enterConfirmsDialog(target: EventTarget | null): boolean {
|
||||
const el = target as Element | null;
|
||||
if (!el || typeof el.closest !== "function") return true;
|
||||
return !el.closest("button, a, textarea, summary, [role='button'], [contenteditable='true']");
|
||||
}
|
||||
|
||||
export interface ModalFocusOptions {
|
||||
/** Esc schliesst; läuft gerade eine Aktion, die Esc nicht verlassen soll, `undefined` lassen. */
|
||||
onEscape?: () => void;
|
||||
|
||||
Reference in new issue
Block a user