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()
@@ -6,13 +6,21 @@ export default class extends BridgeComponent {
static targets = [ "submit", "cancel" ]
static values = { submitTitle: String }
connect() {
window.addEventListener("beforeunload", this.handleBeforeUnload.bind(this))
}
disconnect() {
window.removeEventListener("beforeunload", this.handleBeforeUnload.bind(this))
}
submitTargetConnected() {
this.notifyBridgeOfConnect()
this.#observeSubmitTarget()
}
submitTargetDisconnected() {
this.notifyBridgeOfDisonnect()
this.notifyBridgeOfDisconnect()
this.submitObserver?.disconnect()
}
@@ -37,7 +45,7 @@ export default class extends BridgeComponent {
}
}
notifyBridgeOfDisonnect() {
notifyBridgeOfDisconnect() {
this.send("disconnect")
}
@@ -49,6 +57,10 @@ export default class extends BridgeComponent {
this.send("submitEnd")
}
handleBeforeUnload() {
this.notifyBridgeOfDisconnect()
}
#observeSubmitTarget() {
this.submitObserver = new MutationObserver(() => {
this.send(this.submitTarget.disabled ? "submitDisabled" : "submitEnabled")