bigskysoftware / htmx-extensions

164 stars 49 forks source link

response-targets doesn't reset htmx-request class if hx-disabled-elt is used #79

Open craigharman opened 3 months ago

craigharman commented 3 months ago

If you have an element that uses hx-target-error and hx-disabled-elt and get an error response back htmx-request class doesn't get removed. It does if you remove hx-disabled-elt.

eg. This keeps .htmx-request class on elements:

<form hx-post="/tip" hx-target="#targetDiv" hx-swap="outerHTML swap:1s show:top" hx-trigger="click" hx-target-error="#showError" hx-disabled-elt="this">

While this removes them:

<form hx-post="/tip" hx-target="#targetDiv" hx-swap="outerHTML swap:1s show:top" hx-trigger="click" hx-target-error="#showError">

Telroshan commented 3 months ago

Hey, took a quick look, it seems that both disableElements and addRequestIndicatorClasses in the core, increment an internal requestCount property, and remove their associated classes when that count reaches 0.

I would guess that the issue is, since removeRequestIndicators processes the indicators (thus htmx-request) first, for an element that has both attributes, as for a single request the count will be 2 (as indicator + disable-elt incremented it), the count is 1 when decrementing and processing htmx-request, in which case the class is not removed (check is against a 0 value), then the disabled attribute is properly removed as the count reaches 0.

Didn't spend more time than this so there might be more to it, but if you feel like investigating, a bugfix PR would be welcome! I suspect this could even be a problem with the lib itself (with that double count thing) and not the extension, but this needs confirmation!