Class: MaquinaComponents::ComboboxHelper::ComboboxBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/maquina_components/combobox_helper.rb

Overview

Builder class for constructing comboboxes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, placeholder:) ⇒ ComboboxBuilder

Returns a new instance of ComboboxBuilder.



122
123
124
125
126
# File 'app/helpers/maquina_components/combobox_helper.rb', line 122

def initialize(view_context, placeholder:)
  @view = view_context
  @placeholder = placeholder
  @parts = []
end

Instance Attribute Details

#combobox_idObject

Returns the value of attribute combobox_id.



120
121
122
# File 'app/helpers/maquina_components/combobox_helper.rb', line 120

def combobox_id
  @combobox_id
end

Instance Method Details

#content(align: :start, width: :default, **options) { ... } ⇒ Object

Defines the content popover

Parameters:

  • align (Symbol) (defaults to: :start)

    Horizontal alignment

  • width (Symbol) (defaults to: :default)

    Width preset

  • options (Hash)

    Additional options

Yields:

  • Block containing combobox content



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/helpers/maquina_components/combobox_helper.rb', line 150

def content(align: :start, width: :default, **options, &block)
  content_builder = ComboboxContentBuilder.new(@view)
  # Exposed for the input/list/empty shortcuts below, so a caller can
  # either take the yielded builder or keep using `cb.input` & friends.
  previous_content_builder = @current_content_builder
  @current_content_builder = content_builder
  begin
    @view.capture(content_builder, &block)
  ensure
    @current_content_builder = previous_content_builder
  end

  @parts << @view.render(
    "components/combobox/content",
    id: combobox_id,
    align: align,
    width: width,
    **options
  ) { content_builder.to_html }
end

#empty(text: "No results found.", **options) ⇒ Object

Shortcut to add empty directly (delegates to content builder)



182
183
184
# File 'app/helpers/maquina_components/combobox_helper.rb', line 182

def empty(text: "No results found.", **options)
  @current_content_builder&.empty(text: text, **options)
end

#input(placeholder: "Search...", **options) ⇒ Object

Shortcut to add input directly (delegates to content builder)



172
173
174
# File 'app/helpers/maquina_components/combobox_helper.rb', line 172

def input(placeholder: "Search...", **options)
  @current_content_builder&.input(placeholder: placeholder, **options)
end

#list(**options, &block) ⇒ Object

Shortcut to add list directly (delegates to content builder)



177
178
179
# File 'app/helpers/maquina_components/combobox_helper.rb', line 177

def list(**options, &block)
  @current_content_builder&.list(**options, &block)
end

#to_htmlObject

Generates the final HTML



187
188
189
# File 'app/helpers/maquina_components/combobox_helper.rb', line 187

def to_html
  @view.safe_join(@parts)
end

#trigger(variant: :outline, size: :default, **options) ⇒ Object

Defines the trigger button

Parameters:

  • variant (Symbol) (defaults to: :outline)

    Button variant

  • size (Symbol) (defaults to: :default)

    Button size

  • options (Hash)

    Additional options



133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/maquina_components/combobox_helper.rb', line 133

def trigger(variant: :outline, size: :default, **options)
  @parts << @view.render(
    "components/combobox/trigger",
    for_id: combobox_id,
    placeholder: @placeholder,
    variant: variant,
    size: size,
    **options
  )
end