Class: UI::SelectItemComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
SelectItemBehavior
Defined in:
app/view_components/ui/select_item_component.rb

Overview

ItemComponent - ViewComponent implementation

Individual selectable option in the dropdown.

Examples:

Basic usage

<%= render UI::ItemComponent.new(value: "apple") { "Apple" } %>

Disabled item

<%= render UI::ItemComponent.new(value: "angular", disabled: true) { "Angular (Coming Soon)" } %>

Instance Method Summary collapse

Methods included from SelectItemBehavior

#select_item_classes, #select_item_html_attributes

Constructor Details

#initialize(value: nil, disabled: false, classes: "", **attributes) ⇒ SelectItemComponent

Returns a new instance of SelectItemComponent.

Parameters:

  • value (String) (defaults to: nil)

    Value of this option

  • disabled (Boolean) (defaults to: false)

    Whether the item is disabled

  • classes (String) (defaults to: "")

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



19
20
21
22
23
24
# File 'app/view_components/ui/select_item_component.rb', line 19

def initialize(value: nil, disabled: false, classes: "", **attributes)
  @value = value
  @disabled = disabled
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#callObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/view_components/ui/select_item_component.rb', line 26

def call
   :div, **select_item_html_attributes.deep_merge(@attributes) do
    safe_join([
      (:span, content, class: "flex-1"),
      (:svg,
        (:path, nil, d: "M20 6 9 17l-5-5"),
        data: {ui__select_target: "itemCheck"},
        class: "ml-auto size-4 opacity-0 transition-opacity",
        xmlns: "http://www.w3.org/2000/svg",
        viewBox: "0 0 24 24",
        fill: "none",
        stroke: "currentColor",
        stroke_width: "2",
        stroke_linecap: "round",
        stroke_linejoin: "round")
    ])
  end
end