summaryrefslogtreecommitdiff
path: root/app/components/pajamas/radio_component.rb
blob: 7a3d95c8565493f1bbf792391e23d91ce40c0e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
# frozen_string_literal: true

# Renders a Pajamas compliant radio button element
# Must be used in an instance of `ActionView::Helpers::FormBuilder`
module Pajamas
  class RadioComponent < Pajamas::Component
    include Pajamas::Concerns::CheckboxRadioLabelWithHelpText
    include Pajamas::Concerns::CheckboxRadioOptions

    renders_one :label
    renders_one :help_text

    def initialize(
      form:,
      method:,
      label: nil,
      help_text: nil,
      label_options: {},
      radio_options: {},
      value: nil
    )
      @form = form
      @method = method
      @label_argument = label
      @help_text_argument = help_text
      @label_options = label_options
      @input_options = radio_options
      @value = value
    end

    private

    attr_reader(
      :form,
      :method,
      :label_argument,
      :help_text_argument,
      :label_options,
      :input_options,
      :value
    )

    def label_content
      label? ? label : label_argument
    end

    def help_text_content
      help_text? ? help_text : help_text_argument
    end
  end
end