Class: BetterUi::General::Dropdown::ItemComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
Components::Icon::IconHelper
Defined in:
app/components/better_ui/general/dropdown/item_component.rb

Constant Summary collapse

"flex items-center w-full px-4 py-2 text-sm transition-colors"
{
  default: "text-gray-700 hover:bg-gray-100 hover:text-gray-900",
  white: "text-gray-900 hover:bg-gray-50",
  red: "text-red-700 hover:bg-red-50 hover:text-red-900",
  rose: "text-rose-700 hover:bg-rose-50 hover:text-rose-900",
  orange: "text-orange-700 hover:bg-orange-50 hover:text-orange-900",
  green: "text-green-700 hover:bg-green-50 hover:text-green-900",
  blue: "text-blue-700 hover:bg-blue-50 hover:text-blue-900",
  yellow: "text-yellow-700 hover:bg-yellow-50 hover:text-yellow-900",
  violet: "text-violet-700 hover:bg-violet-50 hover:text-violet-900"
}.freeze
"opacity-50 cursor-not-allowed pointer-events-none"
"bg-gray-100 text-gray-900"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Components::Icon::IconHelper

#bui_icon

Constructor Details

#initialize(text:, icon: nil, href: nil, theme: :default, disabled: false, active: false, classes: nil, **html_options) ⇒ ItemComponent

Returns a new instance of ItemComponent.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 31

def initialize(
  text:,
  icon: nil,
  href: nil,
  theme: :default,
  disabled: false,
  active: false,
  classes: nil,
  **html_options
)
  @text = text
  @icon = icon
  @href = href
  @theme = theme.to_sym
  @disabled = disabled
  @active = active
  @classes = classes
  @html_options = html_options

  validate_params
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def active
  @active
end

#classesObject (readonly)

Returns the value of attribute classes.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def classes
  @classes
end

#disabledObject (readonly)

Returns the value of attribute disabled.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def disabled
  @disabled
end

#hrefObject (readonly)

Returns the value of attribute href.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def href
  @href
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def html_options
  @html_options
end

#iconObject (readonly)

Returns the value of attribute icon.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def icon
  @icon
end

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def text
  @text
end

#themeObject (readonly)

Returns the value of attribute theme.



9
10
11
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 9

def theme
  @theme
end

Instance Method Details

#item_attributesObject

Restituisce gli attributi per l’elemento



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 68

def item_attributes
  attrs = {
    class: item_classes,
    role: "menuitem",
    "data-bui-dropdown-target": "item"
  }

  if @href.present? && !@disabled
    attrs[:href] = @href
  end

  if @disabled
    attrs["aria-disabled"] = "true"
    attrs[:tabindex] = "-1"
  end

  @html_options.except(:class).each do |key, value|
    attrs[key] = value
  end

  attrs
end

#item_classesObject

Combina tutte le classi per l’elemento



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 54

def item_classes
  classes = [
    DROPDOWN_ITEM_BASE_CLASSES,
    get_theme_classes,
    @classes
  ]

  classes << DROPDOWN_ITEM_STATE_DISABLED if @disabled
  classes << DROPDOWN_ITEM_STATE_ACTIVE if @active

  classes.compact.join(" ")
end

#render?Boolean

Verifica se rendere il componente

Returns:

  • (Boolean)


97
98
99
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 97

def render?
  @text.present?
end

#tag_nameObject

Determina se usare un link o un button



92
93
94
# File 'app/components/better_ui/general/dropdown/item_component.rb', line 92

def tag_name
  @href.present? && !@disabled ? :a : :button
end