Class: UI::SelectTrigger

Inherits:
Phlex::HTML
  • Object
show all
Includes:
SelectTriggerBehavior, SharedAsChildBehavior
Defined in:
app/components/ui/select_trigger.rb

Overview

Select Trigger - Phlex implementation

Button that opens the select dropdown. Supports asChild pattern for composition.

Examples:

Default button

render UI::Trigger.new(placeholder: "Select a fruit...")

With asChild (custom button)

render UI::Trigger.new(as_child: true, placeholder: "Select plan...") do |attrs|
  button(**attrs, class: "custom-button") do
    span(data_ui__select_target: "valueDisplay") { "Select plan..." }
  end
end

Instance Method Summary collapse

Methods included from SharedAsChildBehavior

#merge_attributes

Methods included from SelectTriggerBehavior

#select_trigger_classes, #select_trigger_html_attributes

Constructor Details

#initialize(as_child: false, placeholder: "Select...", classes: "", **attributes) ⇒ SelectTrigger

Returns a new instance of SelectTrigger.

Parameters:

  • as_child (Boolean) (defaults to: false)

    When true, yields attributes to block instead of rendering button

  • placeholder (String) (defaults to: "Select...")

    Placeholder text when no value selected

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

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



25
26
27
28
29
30
# File 'app/components/ui/select_trigger.rb', line 25

def initialize(as_child: false, placeholder: "Select...", classes: "", **attributes)
  @as_child = as_child
  @placeholder = placeholder
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#view_template(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/components/ui/select_trigger.rb', line 32

def view_template(&block)
  trigger_attrs = select_trigger_html_attributes.deep_merge(@attributes)

  if @as_child
    # asChild mode: yield attributes to block
    yield(trigger_attrs) if block_given?
  else
    # Default mode: render as button
    button(**trigger_attrs) do
      span(data_ui__select_target: "valueDisplay") { @placeholder }
    end
  end
end