Files
fizzy/app/javascript/controllers/bridge/buttons_controller.js
T
2026-01-08 13:46:31 -05:00

44 lines
961 B
JavaScript

import { BridgeComponent } from "@hotwired/hotwire-native-bridge"
import { BridgeElement } from "@hotwired/hotwire-native-bridge"
export default class extends BridgeComponent {
static component = "buttons"
static targets = [ "button" ]
connect() {
super.connect()
if (this.hasButtonTarget) {
this.notifyBridgeOfConnect()
}
}
disconnect() {
super.disconnect()
if (this.hasButtonTarget) {
this.notifyBridgeOfDisconnect()
}
}
notifyBridgeOfConnect() {
const buttons = this.buttonTargets.map((target, index) => {
const element = new BridgeElement(target)
return { ...element.getButton(), index }
})
this.send("connect", { buttons }, message => {
this.clickButton(message)
})
}
notifyBridgeOfDisconnect() {
this.send("disconnect")
}
clickButton(message) {
const selectedIndex = message.data.selectedIndex
this.buttonTargets[selectedIndex].click()
}
}