Sha256: f9f0c66d5a91574ea697837feecaf64b8aea089a08af30bc7334dbd153f80bf2

Contents?: true

Size: 1.73 KB

Versions: 71

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Primer
  module Forms
    module Dsl
      # :nodoc:
      class CheckBoxInput < Input
        DEFAULT_SCHEME = :boolean
        SCHEMES = [DEFAULT_SCHEME, :array].freeze

        attr_reader :name, :label, :value, :unchecked_value, :scheme, :nested_form_block, :nested_form_arguments

        def initialize(name:, label:, value: nil, unchecked_value: nil, scheme: DEFAULT_SCHEME, **system_arguments)
          raise ArgumentError, "Check box scheme must be one of #{SCHEMES.join(', ')}" unless SCHEMES.include?(scheme)

          raise ArgumentError, "Check box needs an explicit value if scheme is array" if scheme == :array && value.nil?

          @name = name
          @label = label
          @value = value
          @unchecked_value = unchecked_value
          @scheme = scheme

          super(**system_arguments)

          yield(self) if block_given?
        end

        # check boxes cannot be invalid, as both checked and unchecked are valid states
        # :nocov:
        def valid?
          true
        end
        # :nocov:

        def to_component
          CheckBox.new(input: self)
        end

        def nested_form(**system_arguments, &block)
          @nested_form_arguments = system_arguments
          @nested_form_block = block
        end

        def type
          :check_box
        end

        def supports_validation?
          false
        end

        private

        def caption_template_name
          @caption_template_name ||= if @scheme == :array
                                       :"#{name}_#{value}"
                                     else
                                       name.to_sym
                                     end
        end
      end
    end
  end
end

Version data entries

71 entries across 71 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.41.0 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.40.0 lib/primer/forms/dsl/check_box_input.rb
primer_view_components-0.30.0 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.39.1 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.39.0 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.38.0 lib/primer/forms/dsl/check_box_input.rb
primer_view_components-0.29.0 lib/primer/forms/dsl/check_box_input.rb
primer_view_components-0.28.0 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.37.1 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.37.0 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.36.2 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.36.1 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.36.0 lib/primer/forms/dsl/check_box_input.rb
primer_view_components-0.27.0 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.35.2 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.35.1 lib/primer/forms/dsl/check_box_input.rb
primer_view_components-0.26.1 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.35.0 lib/primer/forms/dsl/check_box_input.rb
primer_view_components-0.26.0 lib/primer/forms/dsl/check_box_input.rb
openproject-primer_view_components-0.34.0 lib/primer/forms/dsl/check_box_input.rb