Notify the form/buttons components of a disconnect before a page reload. This gives the native components an opportunity to reset their state before the fresh page loads.

This commit is contained in:
Jay Ohms
2026-02-25 15:32:00 -05:00
parent 91aa806b72
commit 6ceb5ef7c3
2 changed files with 30 additions and 2 deletions
@@ -5,6 +5,14 @@ export default class extends BridgeComponent {
static component = "buttons"
static targets = [ "button" ]
connect() {
window.addEventListener("beforeunload", this.handleBeforeUnload.bind(this))
}
disconnect() {
window.removeEventListener("beforeunload", this.handleBeforeUnload.bind(this))
}
buttonTargetConnected() {
this.notifyBridgeOfConnect()
}
@@ -27,6 +35,14 @@ export default class extends BridgeComponent {
})
}
notifyBridgeOfDisconnect() {
this.send("disconnect")
}
handleBeforeUnload() {
this.notifyBridgeOfDisconnect()
}
#clickButton(message) {
const selectedIndex = message.data.selectedIndex
this.#enabledButtonTargets[selectedIndex].click()