Class: Superform::Rails::Components::Checkbox
- Defined in:
- lib/superform/rails/components/checkbox.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #field_attributes ⇒ Object
-
#initialize(field, index: nil, **attributes) ⇒ Checkbox
constructor
A new instance of Checkbox.
- #view_template ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(field, index: nil, **attributes) ⇒ Checkbox
Returns a new instance of Checkbox.
5 6 7 8 |
# File 'lib/superform/rails/components/checkbox.rb', line 5 def initialize(field, index: nil, **attributes) super(field, **attributes) @index = index end |
Instance Method Details
#field_attributes ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/superform/rails/components/checkbox.rb', line 22 def field_attributes if boolean? { id: dom.id, name: dom.name, checked: field.value } elsif collection? { id: dom.id, name: dom.name, checked: true } else { id: DOM.join(dom.id, @index || @attributes[:value]), name: dom.array_name, checked: Array(field.value).include?(@attributes[:value]) } end end |
#view_template ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/superform/rails/components/checkbox.rb', line 10 def view_template(&) if boolean? # Rails convention: hidden input ensures a value is sent even when unchecked input(name: dom.name, type: :hidden, value: "0") input(type: :checkbox, value: "1", **attributes) elsif collection? input(type: :checkbox, value: dom.value, **attributes) else input(type: :checkbox, **attributes) end end |