Class: HotwireCombobox::Component

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Customizable
Defined in:
app/presenters/hotwire_combobox/component.rb

Defined Under Namespace

Modules: Customizable

Constant Summary

Constants included from Customizable

Customizable::CUSTOMIZABLE_ELEMENTS, Customizable::PROTECTED_ATTRS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, name, association_name: nil, async_src: nil, autocomplete: :both, data: {}, dialog_label: nil, form: nil, free_text: false, id: nil, input: {}, label: nil, mobile_at: "640px", multiselect_chip_src: nil, name_when_new: nil, open: false, options: [], value: nil, **rest) ⇒ Component

Returns a new instance of Component.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/presenters/hotwire_combobox/component.rb', line 11

def initialize \
      view, name,
      association_name:     nil,
      async_src:            nil,
      autocomplete:         :both,
      data:                 {},
      dialog_label:         nil,
      form:                 nil,
      free_text:            false,
      id:                   nil,
      input:                {},
      label:                nil,
      mobile_at:            "640px",
      multiselect_chip_src: nil,
      name_when_new:        nil,
      open:                 false,
      options:              [],
      value:                nil,
      **rest
  @view, @autocomplete, @id, @name, @value, @form, @async_src, @label, @free_text,
  @name_when_new, @open, @data, @mobile_at, @multiselect_chip_src, @options, @dialog_label =
    view, autocomplete, id, name.to_s, value, form, async_src, label, free_text,
    name_when_new, open, data, mobile_at, multiselect_chip_src, options, dialog_label

  @combobox_attrs = input.reverse_merge(rest).deep_symbolize_keys
  @association_name = association_name || infer_association_name

  validate!
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



7
8
9
# File 'app/presenters/hotwire_combobox/component.rb', line 7

def label
  @label
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'app/presenters/hotwire_combobox/component.rb', line 7

def options
  @options
end

Instance Method Details

#announcer_attrsObject



82
83
84
85
86
87
88
# File 'app/presenters/hotwire_combobox/component.rb', line 82

def announcer_attrs
  {
    class: "hw-combobox__announcer",
    aria: announcer_aria,
    data: announcer_data
  }
end

#dialog_attrsObject



134
135
136
137
138
139
140
# File 'app/presenters/hotwire_combobox/component.rb', line 134

def dialog_attrs
  apply_customizations_to :dialog, base: {
    class: "hw-combobox__dialog",
    role: :dialog,
    data: dialog_data
  }
end

#dialog_focus_trap_attrsObject



175
176
177
178
179
180
# File 'app/presenters/hotwire_combobox/component.rb', line 175

def dialog_focus_trap_attrs
  {
    tabindex: "-1",
    data: dialog_focus_trap_data
  }
end

#dialog_input_attrsObject



153
154
155
156
157
158
159
160
161
162
163
# File 'app/presenters/hotwire_combobox/component.rb', line 153

def dialog_input_attrs
  apply_customizations_to :dialog_input, base: {
    id: dialog_input_id,
    role: :combobox,
    class: "hw-combobox__dialog__input",
    autofocus: "",
    type: input_type,
    data: dialog_input_data,
    aria: dialog_input_aria
  }
end

#dialog_labelObject



142
143
144
# File 'app/presenters/hotwire_combobox/component.rb', line 142

def dialog_label
  @dialog_label || label
end

#dialog_label_attrsObject



146
147
148
149
150
151
# File 'app/presenters/hotwire_combobox/component.rb', line 146

def dialog_label_attrs
  apply_customizations_to :dialog_label, base: {
    class: "hw-combobox__dialog__label",
    for: dialog_input_id
  }
end

#dialog_listbox_attrsObject



165
166
167
168
169
170
171
172
173
# File 'app/presenters/hotwire_combobox/component.rb', line 165

def dialog_listbox_attrs
  apply_customizations_to :dialog_listbox, base: {
    id: dialog_listbox_id,
    class: "hw-combobox__dialog__listbox",
    role: :listbox,
    data: dialog_listbox_data,
    aria: dialog_listbox_aria
  }
end

#dialog_wrapper_attrsObject



128
129
130
131
132
# File 'app/presenters/hotwire_combobox/component.rb', line 128

def dialog_wrapper_attrs
  apply_customizations_to :dialog_wrapper, base: {
    class: "hw-combobox__dialog__wrapper"
  }
end

#fieldset_attrsObject



47
48
49
50
51
52
# File 'app/presenters/hotwire_combobox/component.rb', line 47

def fieldset_attrs
  apply_customizations_to :fieldset, base: {
    class: [ "hw-combobox", { "hw-combobox--multiple": multiselect? } ],
    data: fieldset_data
  }
end

#handle_attrsObject



108
109
110
111
112
113
# File 'app/presenters/hotwire_combobox/component.rb', line 108

def handle_attrs
  apply_customizations_to :handle, base: {
    class: "hw-combobox__handle",
    data: handle_data
  }
end

#hidden_field_attrsObject



64
65
66
67
68
69
70
71
# File 'app/presenters/hotwire_combobox/component.rb', line 64

def hidden_field_attrs
  apply_customizations_to :hidden_field, base: {
    id: hidden_field_id,
    name: hidden_field_name,
    data: hidden_field_data,
    value: hidden_field_value
  }
end

#input_attrsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/presenters/hotwire_combobox/component.rb', line 91

def input_attrs
  nested_attrs = %i[ data aria ]

  base = {
    id: input_id,
    role: :combobox,
    class: "hw-combobox__input",
    type: input_type,
    data: input_data,
    aria: input_aria,
    autocomplete: :off
  }.merge combobox_attrs.except(*nested_attrs)

  apply_customizations_to :input, base: base
end

#label_attrsObject



55
56
57
58
59
60
61
# File 'app/presenters/hotwire_combobox/component.rb', line 55

def label_attrs
  apply_customizations_to :label, base: {
    class: "hw-combobox__label",
    for: input_id,
    hidden: label.blank?
  }
end

#listbox_attrsObject



116
117
118
119
120
121
122
123
124
125
# File 'app/presenters/hotwire_combobox/component.rb', line 116

def listbox_attrs
  apply_customizations_to :listbox, base: {
    id: listbox_id,
    role: :listbox,
    class: "hw-combobox__listbox",
    hidden: "",
    data: listbox_data,
    aria: listbox_aria
  }
end

#main_wrapper_attrsObject



74
75
76
77
78
79
# File 'app/presenters/hotwire_combobox/component.rb', line 74

def main_wrapper_attrs
  apply_customizations_to :main_wrapper, base: {
    class: "hw-combobox__main__wrapper",
    data: main_wrapper_data
  }
end

#paginated?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'app/presenters/hotwire_combobox/component.rb', line 183

def paginated?
  async_src.present?
end

#pagination_attrsObject



187
188
189
# File 'app/presenters/hotwire_combobox/component.rb', line 187

def pagination_attrs
  { for_id: canonical_id, src: async_src }
end

#render_in(view_context, &block) ⇒ Object



41
42
43
44
# File 'app/presenters/hotwire_combobox/component.rb', line 41

def render_in(view_context, &block)
  block.call(self) if block_given?
  view_context.render partial: "hotwire_combobox/component", locals: { component: self }
end