Class: Superform::Rails::Choices::Choice

Inherits:
Object
  • Object
show all
Defined in:
lib/superform/rails/choices/choice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component:, field:, value:, text:, index:, type:) ⇒ Choice

Returns a new instance of Choice.



7
8
9
10
11
12
13
14
# File 'lib/superform/rails/choices/choice.rb', line 7

def initialize(component:, field:, value:, text:, index:, type:)
  @component = component
  @field = field
  @value = value
  @text = text
  @index = index
  @type = type
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/superform/rails/choices/choice.rb', line 5

def index
  @index
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/superform/rails/choices/choice.rb', line 5

def text
  @text
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/superform/rails/choices/choice.rb', line 5

def value
  @value
end

Instance Method Details

#build_input(**attrs) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/superform/rails/choices/choice.rb', line 28

def build_input(**attrs)
  case @type
  when :radio
    Components::Radio.new(@field, value: @value, index: @index, **attrs)
  when :checkbox
    Components::Checkbox.new(@field, value: @value, index: @index, **attrs)
  end
end

#input(**attrs) ⇒ Object



16
17
18
# File 'lib/superform/rails/choices/choice.rb', line 16

def input(**attrs)
  @component.render build_input(**attrs)
end

#label(**attrs, &block) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/superform/rails/choices/choice.rb', line 20

def label(**attrs, &block)
  label_text = @text
  block ||= proc { label_text }
  @component.render Components::Label.new(
    @field, for: DOM.join(@field.dom.id, @index), **attrs, &block
  )
end