Class: StimulusPlumbers::Components::Combobox

Inherits:
Plumber::Base show all
Defined in:
lib/stimulus_plumbers/components/combobox.rb,
lib/stimulus_plumbers/components/combobox/date.rb,
lib/stimulus_plumbers/components/combobox/time.rb,
lib/stimulus_plumbers/components/combobox/options.rb,
lib/stimulus_plumbers/components/combobox/popover.rb,
lib/stimulus_plumbers/components/combobox/trigger.rb,
lib/stimulus_plumbers/components/combobox/dropdown.rb,
lib/stimulus_plumbers/components/combobox/time/drum.rb,
lib/stimulus_plumbers/components/combobox/autocomplete.rb,
lib/stimulus_plumbers/components/combobox/options/option.rb,
lib/stimulus_plumbers/components/combobox/options/option_group.rb

Defined Under Namespace

Classes: Autocomplete, Date, Dropdown, Options, Popover, Time, Trigger

Constant Summary collapse

STIMULUS_CONTROLLER =
"input-combobox"
FORMAT_CONTROLLER =
"input-format"
FORMAT_ACTION =
"input-combobox:changed->input-format#format"

Constants included from Plumber::HtmlOptions

Plumber::HtmlOptions::STIMULUS_SPACEJOIN_KEYS

Instance Attribute Summary

Attributes inherited from Plumber::Base

#template

Instance Method Summary collapse

Methods inherited from Plumber::Base

#initialize, #theme

Methods included from Plumber::HtmlOptions

#merge_data_options, #merge_html_options, #merge_string_option, #normalize_part

Constructor Details

This class inherits a constructor from StimulusPlumbers::Plumber::Base

Instance Method Details

#popover(popover_id, options) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/stimulus_plumbers/components/combobox.rb', line 43

def popover(popover_id, options)
  Combobox::Popover.new(template).render(
    stimulus_controller: STIMULUS_CONTROLLER,
    id:                  popover_id,
    **options.fetch(:popover, {})
  )
end

#render(base_id:, options: {}, **kwargs) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stimulus_plumbers/components/combobox.rb', line 10

def render(base_id:, options: {}, **kwargs)
  popover_id    = "#{base_id}_popover"
  initial_value = options.dig(:input, :value)

  base_data = {
    controller: "#{STIMULUS_CONTROLLER} #{FORMAT_CONTROLLER}",
    action:     FORMAT_ACTION
  }
  base_data[:input_combobox_value_value] = initial_value if initial_value.present?

  html_options = merge_html_options({ data: base_data }, kwargs)

  template.(:div, **html_options) do
    template.safe_join(
      [
        trigger(popover_id, options),
        hidden_input(options.fetch(:input, {})),
        popover(popover_id, options)
      ]
    )
  end
end

#trigger(popover_id, options) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/stimulus_plumbers/components/combobox.rb', line 33

def trigger(popover_id, options)
  haspopup = options.dig(:popover, :haspopup) || options.dig(:popover, :role) || "dialog"
  Combobox::Trigger.new(template).render(
    stimulus_controller: STIMULUS_CONTROLLER,
    popover_id:          popover_id,
    haspopup:            haspopup,
    **options.fetch(:trigger, {})
  )
end