Class: Protoform::Rails::Components::Select

Inherits:
FieldComponent show all
Defined in:
lib/protoform/rails/components/select.rb

Instance Method Summary collapse

Methods inherited from Component

#dom, #title

Instance Method Details

#blank_option(&block) ⇒ Object



38
39
40
# File 'lib/protoform/rails/components/select.rb', line 38

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

#false_option(&block) ⇒ Object



46
47
48
# File 'lib/protoform/rails/components/select.rb', line 46

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

#options(*collection) ⇒ Object



32
33
34
35
36
# File 'lib/protoform/rails/components/select.rb', line 32

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

#true_option(&block) ⇒ Object



42
43
44
# File 'lib/protoform/rails/components/select.rb', line 42

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

#view_template(&options) ⇒ Object



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

def view_template(&options)
  name = @multiple ? "#{attrs[:name]}[]" : attrs[:name]

  if @multiple
    input(
      name:,
      type: :hidden,
      value: ""
    )
  end

  if options
    select(multiple: @multiple, **attrs, name:, &options)
  else
    select(multiple: @multiple, **attrs, name:) do
      blank_option if @include_blank
      options(*@collection)
    end
  end
end