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_optionObject



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

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

#false_optionObject



50
51
52
53
54
55
56
# File 'lib/protoform/rails/components/select.rb', line 50

def false_option(&)
  option(
    selected: field.value == false,
    value: false.to_s,
    &
  )
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.to_s) { value }
  end
end

#true_optionObject



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

def true_option(&)
  option(
    selected: field.value == true,
    value: true.to_s,
    &
  )
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