Class: ERBLint::Linters::ArgumentMappers::Button

Inherits:
Base
  • Object
show all
Defined in:
lib/yattho/view_components/linters/argument_mappers/button.rb

Overview

Maps classes in a button element to arguments for the Button component.

Constant Summary collapse

SCHEME_MAPPINGS =
Yattho::ViewComponents::Constants.get(
  component: "Yattho::ButtonComponent",
  constant: "SCHEME_MAPPINGS",
  symbolize: true
).freeze
SIZE_MAPPINGS =
Yattho::ViewComponents::Constants.get(
  component: "Yattho::ButtonComponent",
  constant: "SIZE_MAPPINGS",
  symbolize: true
).freeze
TYPE_OPTIONS =
Yattho::ViewComponents::Constants.get(
  component: "Yattho::Beta::BaseButton",
  constant: "TYPE_OPTIONS"
).freeze
DEFAULT_TAG =
Yattho::ViewComponents::Constants.get(
  component: "Yattho::Beta::BaseButton",
  constant: "DEFAULT_TAG"
).freeze
ATTRIBUTES =
%w[disabled type href name value tabindex].freeze

Instance Method Summary collapse

Methods inherited from Base

#initialize, #map_classes, #system_arguments_to_args, #to_args, #to_s

Constructor Details

This class inherits a constructor from ERBLint::Linters::ArgumentMappers::Base

Instance Method Details

#attribute_to_args(attribute) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yattho/view_components/linters/argument_mappers/button.rb', line 34

def attribute_to_args(attribute)
  attr_name = attribute.name

  case attr_name
  when "disabled"
    { disabled: true }
  when "type"
    # button is the default type, so we don't need to do anything.
    return {} if attribute.value == "button"

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

    { type: ":#{attribute.value}" }
  else
    { attr_name.to_sym => erb_helper.convert(attribute) }
  end
end

#classes_to_args(classes) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/yattho/view_components/linters/argument_mappers/button.rb', line 55

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

    if SCHEME_MAPPINGS[class_name] && acc[:scheme].nil?
      acc[:scheme] = SCHEME_MAPPINGS[class_name]
    elsif SIZE_MAPPINGS[class_name] && acc[:size].nil?
      acc[:size] = SIZE_MAPPINGS[class_name]
    elsif class_name == "btn-block"
      acc[:block] = true
    elsif class_name == "BtnGroup-item"
      acc[:group_item] = true
    else
      acc[:classes] << class_name
    end
  end
end