Class: Brut::FrontEnd::Components::Inputs::RadioButton

Inherits:
InputTag show all
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

Methods inherited from InputTag

#invalid?, #view_template

Methods inherited from Brut::FrontEnd::Component

component_name, #component_name

Methods included from Brut::FrontEnd::Component::Helpers

#global_component, #inline_svg

Methods included from I18n::ForHTML

#html_escape, #t

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from Brut::Framework::Errors

#abstract_method!, #bug!

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.

Parameters:

  • form (Brut::FrontEnd::Form)

    The form that is being rendered. This method will consult this class to understand the requirements on this input so its HTML is generated correctly.

  • input_name (String)

    the name of the input, which should be a member of form

  • value (String)

    the value for this radio button. The Forms::RadioButtonGroupInput value is compared

  • html_attributes (Hash) (defaults to: {})

    any additional HTML attributes to include on the <input> element.



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