Sha256: bdf9c080e3e95d5e31221f0eaeaf8337c6ca557adcff1d2bbe69b48712422bfd

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require_relative "base"

module ERBLint
  module Linters
    module ArgumentMappers
      # Maps classes in a close-button element to arguments for the CloseButton component.
      class CloseButton < Base
        ATTRIBUTES = %w[type].freeze

        TYPE_OPTIONS = Yattho::ViewComponents::Constants.get(
          component: "Yattho::Beta::CloseButton",
          constant: "TYPE_OPTIONS"
        ).freeze

        DEFAULT_TYPE = Yattho::ViewComponents::Constants.get(
          component: "Yattho::Beta::CloseButton",
          constant: "DEFAULT_TYPE"
        ).freeze

        DEFAULT_CLASS = "close-button"

        def attribute_to_args(attribute)
          # button is the default type, so we don't need to do anything.
          return {} if attribute.value == DEFAULT_TYPE

          unless TYPE_OPTIONS.include?(attribute.value)
            raise ConversionError,
                  "CloseButton component does not support type \"#{attribute.value}\""
          end

          { type: ":#{attribute.value}" }
        end

        def classes_to_args(classes)
          classes.each_with_object({ classes: [] }) do |class_name, acc|
            next if class_name == DEFAULT_CLASS

            acc[:classes] << class_name
          end
        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/view_components/linters/argument_mappers/close_button.rb
yattho_view_components-0.0.1 lib/yattho/view_components/linters/argument_mappers/close_button.rb