Sha256: ac0eb85dbd3062d8d2e29058dc1c717c4549fa6b8bf538169977a4a91591c662

Contents?: true

Size: 1.28 KB

Versions: 147

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Primer
  module Forms
    module Dsl
      # :nodoc:
      class MultiInput < Input
        include InputMethods

        attr_reader :name, :label

        def initialize(name:, label:, **system_arguments)
          @name = name
          @label = label

          super(**system_arguments)

          yield(self) if block_given?
        end

        def to_component
          Multi.new(input: self)
        end

        def type
          :multi
        end

        private

        def add_input(input)
          super

          check_one_input_visible!
        end

        def decorate_options(name:, **options)
          new_options = { name: @name, label: nil, form_control: false, **options }
          new_options[:data] ||= {}
          new_options[:data][:name] = name
          new_options[:data][:targets] = "primer-multi-input.fields"
          new_options[:id] = nil if options[:hidden]
          new_options[:disabled] = true if options[:hidden] # disable to avoid submitting to server
          new_options
        end

        def check_one_input_visible!
          return if inputs.count { |input| !input.hidden? } <= 1

          raise ArgumentError, "Only one input can be visible at a time in a `multi' block."
        end
      end
    end
  end
end

Version data entries

147 entries across 147 versions & 2 rubygems

Version Path
primer_view_components-0.36.5 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.52.2 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.36.4 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.52.1 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.52.0 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.36.3 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.51.0 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.36.2 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.50.1 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.50.0 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.36.1 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.36.0 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.49.2 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.35.2 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.49.1 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.49.0 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.35.1 app/lib/primer/forms/dsl/multi_input.rb
primer_view_components-0.35.0 app/lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.48.2 lib/primer/forms/dsl/multi_input.rb
openproject-primer_view_components-0.48.1 lib/primer/forms/dsl/multi_input.rb