Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alchemy-button): Re-enable on turbo:submit-end #2854

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/javascript/alchemy_admin/components/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Button extends HTMLButtonElement {
if (this.form.dataset.remote == "true") {
this.form.addEventListener("ajax:complete", this)
}

this.form.addEventListener("turbo:submit-end", this)
} else {
console.warn("No form for button found!", this)
}
Expand All @@ -26,6 +28,7 @@ class Button extends HTMLButtonElement {
}
break
case "ajax:complete":
case "turbo:submit-end":
this.enable()
break
}
Expand Down
26 changes: 26 additions & 0 deletions spec/javascript/alchemy_admin/components/button.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,30 @@ describe("alchemy-button", () => {
expect(button.innerHTML).toEqual("Save")
})
})

describe("on turbo forms", () => {
it("re-enables button on turbo:submit-end", () => {
const html = `
<turbo-frame>
<form>
<button type="submit" is="alchemy-button">Save</button>
</form>
</turbo-frame>
`
const button = renderComponent("alchemy-button", html)

const submit = new Event("submit", { bubbles: true })
button.form.dispatchEvent(submit)

expect(button.getAttribute("disabled")).toEqual("disabled")

const submitEnd = new CustomEvent("turbo:submit-end", { bubbles: true })
button.form.dispatchEvent(submitEnd)

expect(button.getAttribute("disabled")).toBeNull()
expect(button.getAttribute("tabindex")).toBeNull()
expect(button.classList.contains("disabled")).toBeFalsy()
expect(button.innerHTML).toEqual("Save")
})
})
})