Class: Superform::Rails::Components::SelectField

Inherits:
FieldComponent show all
Defined in:
lib/superform/rails.rb

Instance Attribute Summary

Attributes inherited from BaseComponent

#dom, #field

Instance Method Summary collapse

Methods inherited from FieldComponent

#field_attributes

Methods inherited from BaseComponent

#field_attributes, #focus

Constructor Details

#initialize(collection: []) ⇒ SelectField

Returns a new instance of SelectField.



335
336
337
338
# File 'lib/superform/rails.rb', line 335

def initialize(*, collection: [], **, &)
  super(*, **, &)
  @collection = collection
end

Instance Method Details

#blank_optionObject



354
355
356
# File 'lib/superform/rails.rb', line 354

def blank_option(&)
  option(selected: field.value.nil?, &)
end

#false_optionObject



362
363
364
# File 'lib/superform/rails.rb', line 362

def false_option(&)
  option(selected: field.value == false, value: false.to_s, &)
end

#options(*collection) ⇒ Object



348
349
350
351
352
# File 'lib/superform/rails.rb', line 348

def options(*collection)
  map_options(collection).each do |key, value|
    option(selected: field.value == key, value: key) { value }
  end
end

#template(&options) ⇒ Object



340
341
342
343
344
345
346
# File 'lib/superform/rails.rb', line 340

def template(&options)
  if block_given?
    select(**attributes, &options)
  else
    select(**attributes) { options(*@collection) }
  end
end

#true_optionObject



358
359
360
# File 'lib/superform/rails.rb', line 358

def true_option(&)
  option(selected: field.value == true, value: true.to_s, &)
end