Sha256: 81073a7c6b6b32e6c33447d6d7f258b947c775097ecc662f4cfcecaa39a161a2

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Yattho
  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] = "yattho-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

2 entries across 2 versions & 1 rubygems

Version Path
yattho_view_components-0.1.1 lib/yattho/forms/dsl/multi_input.rb
yattho_view_components-0.0.1 lib/yattho/forms/dsl/multi_input.rb