Class: Brut::FrontEnd::Components::Inputs::RadioButton
- Inherits:
-
InputTag
- Object
- Phlex::HTML
- Brut::FrontEnd::Component
- Brut::FrontEnd::Components::Input
- InputTag
- Brut::FrontEnd::Components::Inputs::RadioButton
- Defined in:
- lib/brut/front_end/components/inputs/radio_button.rb
Overview
Renders an HTML ‘<input type=“radio”>`. Unlike other form fields, radio button groups require several HTML elements to present the visitor a choice. All of the classes internal to the Form treat the radio button group as a single input with a single name and value. When it comes time to generate HTML, this class is used to generate a single radio button from a group.
Instance Method Summary collapse
-
#initialize(form:, input_name:, value:, html_attributes: {}) ⇒ RadioButton
constructor
Creates a radio button that is part of a radio button group.
Methods inherited from InputTag
Methods inherited from Brut::FrontEnd::Component
#around_template, component_name, #component_name
Methods included from Brut::FrontEnd::Component::Helpers
#entity, #global_component, #inline_svg
Methods included from I18n::ForHTML
Methods included from I18n::BaseMethods
#l, #t, #t_direct, #this_field_value
Methods included from Brut::Framework::Errors
Constructor Details
#initialize(form:, input_name:, value:, html_attributes: {}) ⇒ RadioButton
Creates a radio button that is part of a radio button group. You should call this method once for each radio button in the group.
against this value to determine if this ‘<input>` will have the `checked` attribute.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/brut/front_end/components/inputs/radio_button.rb', line 15 def initialize(form:, input_name:, value:, html_attributes: {}) input = form.input(input_name) default_html_attributes = {} html_attributes = html_attributes.map { |key,value| [ key.to_sym, value ] }.to_h default_html_attributes[:required] = input.required default_html_attributes[:type] = "radio" default_html_attributes[:name] = if input.array? "#{input.name}[]" else input.name end default_html_attributes[:value] = value selected_value = input.value if selected_value == value default_html_attributes[:checked] = true end if !form.new? && !input.valid? default_html_attributes["data-invalid"] = true input.validity_state.each do |constraint,violated| if violated default_html_attributes["data-#{constraint}"] = true end end end @attributes = default_html_attributes.merge(html_attributes) end |