From f5ec50883414124ad03967f056dafb03571e8462 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Sun, 29 Nov 2026 14:54:00 +0200 Subject: [PATCH] =?UTF-8?q?Korjaa=20satunnainen=20fokuksen=20katoaminen=20?= =?UTF-8?q?dialogilomakkeen=20422-vastauksen=20j=C3=A4lkeen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HTMX kytkee uuden sisällön hx-attribuutit (esim. dialogilomakkeen hx-post) vasta settle-vaiheessa, oletuksena 20 ms swapin jälkeen. Jos lomake lähetettiin tuona aikana, selain teki tavallisen sivulatauksen: palvelin palautti koko sivun, eikä htmx:afterSettle siirtänyt fokusta virheelliseen kenttään. Tämä aiheutti test_add_contact_with_keyboard-testin noin joka kolmannen ajon epäonnistumisen. Asetetaan htmx.config.defaultSettleDelay = 0, jolloin uusi sisältö kytketään heti swapin yhteydessä. Sovellus ei käytä htmx-settling- tai htmx-added-luokkien CSS-siirtymiä, joten viiveelle ei ole tarvetta. Co-Authored-By: Claude Opus 5.5 --- static/src/app.js | 4 ++++ tests/browser/test_customers.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/static/src/app.js b/static/src/app.js index da39d7d..eac43a4 100644 --- a/static/src/app.js +++ b/static/src/app.js @@ -9,6 +9,10 @@ // Forms answer validation errors with 422; HTMX must swap those too so errors are shown. if (window.htmx) { + // Settle right after the swap. HTMX wires hx-* attributes of new content (such as a dialog + // form's hx-post) only when it settles; with the default 20 ms delay a quick Enter in that + // gap submitted the form as a normal page load and focus was lost. + window.htmx.config.defaultSettleDelay = 0; window.htmx.config.responseHandling = [ { code: "204", swap: false }, { code: "[23]..", swap: true }, diff --git a/tests/browser/test_customers.py b/tests/browser/test_customers.py index d1cbdf7..204626f 100644 --- a/tests/browser/test_customers.py +++ b/tests/browser/test_customers.py @@ -64,7 +64,7 @@ def test_add_contact_with_keyboard( page.locator("#item-dialog button[type=submit]").focus() page.keyboard.press("Enter") page.wait_for_selector("#id_name[aria-invalid='true']") - # Focus moves in htmx:afterSettle, a moment after the swap. + # Focus moves in htmx:afterSettle. page.wait_for_function("document.activeElement && document.activeElement.id === 'id_name'") page.keyboard.type("Pekka Puhelin")