Module: Voidable::Hotwire::Helpers

Defined in:
lib/voidable/hotwire/helpers.rb

Instance Method Summary collapse

Instance Method Details

#void_attrs(events, action = nil) ⇒ Object

Generates data attributes for wiring a Voidable component to Stimulus.

void_attrs("void-click", "form#submit")
# => { controller: "void-event", "void-event-events-value": "void-click",
#      action: "void-click->form#submit" }


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/voidable/hotwire/helpers.rb', line 10

def void_attrs(events, action = nil)
  attrs = {
    data: {
      controller: "void-event",
      "void-event-events-value": Array(events).join(",")
    }
  }
  if action
    event_list = Array(events)
    attrs[:data][:action] = event_list.map { |e| "#{e}->#{action}" }.join(" ")
  end
  attrs
end