Class: Fluxbit::Form::SelectComponent

Inherits:
TextFieldComponent
  • Object
show all
Defined in:
app/components/fluxbit/form/select_component.rb

Overview

The ‘Fluxbit::Form::SelectComponent` is a styled dropdown/select field for forms. It supports standard, grouped, and time zone options, integrates with Rails form builders, and provides flexible props for prompt, disabled/selected options, helper text, and more.

Examples:

Basic usage

= render Fluxbit::Form::SelectComponent.new(name: :role, options: ["Admin", "User", "Guest"], label: "User Role")

See Also:

  • For detailed documentation and examples.

Instance Method Summary collapse

Constructor Details

#initialize(**props) ⇒ SelectComponent

Initializes the select component with the given properties.

Parameters:

  • name (String)

    Name of the field (required unless using form builder)

  • label (String)

    Label for the input (optional)

  • value (String)

    Value for the field (optional)

  • grouped (Boolean)

    Enables grouped select options (default: false)

  • time_zone (Boolean)

    Uses Rails time zone select options (default: false)

  • select_options (Hash)

    Options for select tag (prompt, selected, disabled, etc)

  • choices (Array)

    List of choices for options (alternative to options)

  • options (Array, Hash)

    List or hash of options (or groups if grouped)

  • help_text (String)

    Helper or error text below the field

  • class (String)

    Additional CSS classes for the select element

  • ...

    any other HTML attribute supported by <select>



25
26
27
28
29
30
31
32
33
# File 'app/components/fluxbit/form/select_component.rb', line 25

def initialize(**props)
  super(**props)
  @grouped = @props.delete(:grouped) || false
  @time_zone = @props.delete(:time_zone) || false
  @select_options = @props.delete(:select_options) || {}
  @choices = @props.delete(:choices) || nil
  @options = @props.delete(:options) || {}
  @options = ::ActiveSupport::TimeZone.all if @time_zone
end

Instance Method Details

#build_options_for_selectObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/components/fluxbit/form/select_component.rb', line 52

def build_options_for_select
  if @grouped
    grouped_options_for_select(
      @options,
      @selected,
      disabled: @disabled_options,
      prompt: @prompt,
      divider: @divider
    )
  elsif @time_zone
    time_zone_options_for_select(@selected)
  else
    options_for_select(@options, selected: @selected, disabled: @disabled_options)
  end
end

#inputObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/components/fluxbit/form/select_component.rb', line 35

def input
  if @form.present? && @attribute.present?
    @form.select(
      @attribute,
      build_options_for_select,
      @select_options,
      @props
    )
  else
    select_tag(
      @name,
      build_options_for_select,
      @props
    )
  end
end