Sha256: c25771b69cc85359e3802471de37da9d2a4c35ab3110395dc1dd11ec410281c6

Contents?: true

Size: 811 Bytes

Versions: 1

Compression:

Stored size: 811 Bytes

Contents

# frozen_string_literal: true

module Phlexi
  module Form
    module Components
      class RadioButton < Input
        def view_template
          input(**attributes, value: @checked_value)
        end

        protected

        def build_input_attributes
          attributes[:type] = :radio
          super

          @checked_value = (attributes.key?(:checked_value) ? attributes.delete(:checked_value) : "1").to_s

          # this is a hack to workaround the fact that radio cannot be indexed/multiple
          attributes[:name] = attributes[:name].sub(/\[]$/, "")
          attributes[:value] = @checked_value
          attributes[:checked] = attributes.fetch(:checked) { checked? }
        end

        def checked?
          field.dom.value == @checked_value
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phlexi-form-0.2.0 lib/phlexi/form/components/radio_button.rb