Add bridge buttons controller.
It sends all buttons in one connect payload.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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) return
|
||||
this.notifyBridgeOfConnect()
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
super.disconnect()
|
||||
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.activateButton(message)
|
||||
})
|
||||
}
|
||||
|
||||
notifyBridgeOfDisconnect() {
|
||||
this.send("disconnect")
|
||||
}
|
||||
|
||||
activateButton(message) {
|
||||
const selectedIndex = message.data.selectedIndex
|
||||
this.buttonTargets[selectedIndex].click()
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,7 @@ BridgeElement.prototype.getButton = function() {
|
||||
return {
|
||||
title: this.title,
|
||||
icon: this.getIcon(),
|
||||
label: this.getLabel(),
|
||||
order: this.getOrder()
|
||||
label: this.getLabel()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,14 +35,6 @@ BridgeElement.prototype.getLabel = function() {
|
||||
return this.bridgeAttribute("label")
|
||||
}
|
||||
|
||||
BridgeElement.prototype.getOrder = function() {
|
||||
return this.bridgeAttribute("order") || "1"
|
||||
}
|
||||
|
||||
BridgeElement.prototype.shouldDisplayAsFormSubmitMenu = function() {
|
||||
return this.bridgeAttribute("display-as-form-submit-menu") === "true"
|
||||
}
|
||||
|
||||
BridgeElement.prototype.displaysNavButtonMenu = function() {
|
||||
return this.bridgeAttribute("displays-nav-button-menu") === "true"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user