Korjaa satunnainen fokuksen katoaminen dialogilomakkeen 422-vastauksen jälkeen

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 <[email protected]>
This commit is contained in:
2026-11-29 14:54:00 +02:00
co-authored by Claude Opus 5.5
parent 679bf57eb4
commit f5ec508834
2 changed files with 5 additions and 1 deletions
+4
View File
@@ -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 },
+1 -1
View File
@@ -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")