Skip to content

Commit

Permalink
Re-enable the submitter when a request is aborted
Browse files Browse the repository at this point in the history
  • Loading branch information
imacrayon committed Aug 17, 2024
1 parent 5bc8959 commit 54f1bd9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@imacrayon/alpine-ajax",
"description": "An Alpine.js plugin for building server-powered frontends.",
"version": "0.8.0",
"version": "0.8.1",
"license": "MIT",
"author": "Christian Taylor",
"homepage": "https://alpine-ajax.js.org",
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,13 @@ async function withSubmitter(submitter, callback) {
submitter.setAttribute('aria-disabled', 'true')
submitter.addEventListener('click', disableEvent)

let result = await callback()

submitter.removeAttribute('aria-disabled')
submitter.removeEventListener('click', disableEvent)
let result
try {
result = await callback()
} finally {
submitter.removeAttribute('aria-disabled')
submitter.removeEventListener('click', disableEvent)
}

return result
}
Expand Down
21 changes: 21 additions & 0 deletions tests/form.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,24 @@ test('performs a normal submit when a 500 status code is returned',
})
}
)

test('submitter is disabled while submitting',
html`<form x-init x-target id="replace" method="post"><button></button></form>`,
({ intercept, get, wait }) => {
intercept('POST', (req) => {
req.continue((res) => {
get('button').should('have.attr', 'aria-disabled')
})
}).as('response')
get('button').click()
}
)

test('submitter is reset when request is aborted',
html`<form x-init x-target id="replace" method="post" @ajax:before="event.preventDefault()"><button></button></form>`,
({ intercept, get, wait }) => {
get('button').click()
wait(300)
get('button').should('not.have.attr', 'aria-disabled')
}
)

0 comments on commit 54f1bd9

Please sign in to comment.