Class: Shadcn::SelectComponent

Inherits:
Object
  • Object
show all
Includes:
ComponentsHelper
Defined in:
app/components/shadcn/select_component.rb

Constant Summary

Constants included from ComponentsHelper

ComponentsHelper::DESTRUCTIVE_CLASSES, ComponentsHelper::GHOST_CLASSES, ComponentsHelper::OUTLINE_CLASSES, ComponentsHelper::PRIMARY_CLASSES, ComponentsHelper::SECONDARY_CLASSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComponentsHelper

#tw

Constructor Details

#initialize(name:, view_context:, selected: nil, **options, &block) ⇒ SelectComponent

Returns a new instance of SelectComponent.



5
6
7
8
9
10
11
# File 'app/components/shadcn/select_component.rb', line 5

def initialize(name:, view_context:, selected: nil, **options, &block)
  @name = name
  @view_context = view_context
  @selected = selected
  @options = options
  @content = view_context.capture(self, &block) if block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'app/components/shadcn/select_component.rb', line 3

def name
  @name
end

#selectedObject (readonly)

Returns the value of attribute selected.



3
4
5
# File 'app/components/shadcn/select_component.rb', line 3

def selected
  @selected
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



3
4
5
# File 'app/components/shadcn/select_component.rb', line 3

def view_context
  @view_context
end

Instance Method Details

#callObject



20
21
22
# File 'app/components/shadcn/select_component.rb', line 20

def call
  view_context. :select, @content, name: name, class: tw("rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50", @options[:class])
end

#option(value:, label: nil, &block) ⇒ Object



13
14
15
16
17
18
# File 'app/components/shadcn/select_component.rb', line 13

def option(value:, label: nil, &block)
  content = label || view_context.capture(&block)
  option_options = {value: value}
  option_options[:selected] = "selected" if value == selected
  view_context. :option, content, option_options
end