Sha256: 58ee7ee459e13a4a2da8aa01a81d5b9733c9e6ebf5c04c3fe7782475451c1619

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Phlexi
  module Form
    module Components
      class Base < COMPONENT_BASE
        attr_reader :field, :attributes

        def initialize(field, **attributes)
          @field = field
          @attributes = attributes

          build_attributes
          append_attribute_classes
        end

        protected

        def build_attributes
          attributes.fetch(:id) { attributes[:id] = "#{field.dom.id}_#{component_name}" }
        end

        def append_attribute_classes
          return if attributes[:class] == false

          attributes[:class] = tokens(
            component_name,
            attributes[:class],
            -> { attributes[:required] } => "required",
            -> { !attributes[:required] } => "optional",
            -> { field.has_errors? } => "invalid",
            -> { attributes[:readonly] } => "readonly",
            -> { attributes[:disabled] } => "disabled"
          )
        end

        def component_name
          @component_name ||= self.class.name.demodulize.underscore
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phlexi-form-0.3.0.rc1 lib/phlexi/form/components/base.rb