Class: Fluxbit::Form::SelectComponent
- Inherits:
-
TextFieldComponent
- Object
- TextFieldComponent
- Fluxbit::Form::SelectComponent
- 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.
Instance Method Summary collapse
-
#build_options_for_select ⇒ Object
Build pre-formatted option tags for select_tag and form.select.
-
#build_select_options_hash ⇒ Object
Build options hash for form.select (prompt, include_blank, etc.) Note: Don’t include selected/disabled if options are pre-formatted HTML.
-
#initialize(**props) ⇒ SelectComponent
constructor
Initializes the select component with the given properties.
- #input ⇒ Object
Constructor Details
#initialize(**props) ⇒ SelectComponent
Initializes the select component with the given properties.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/components/fluxbit/form/select_component.rb', line 33 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) || {} # Extract select-specific options prompt_value = @select_options.delete(:prompt) || @props.delete(:prompt) @include_blank = @select_options.delete(:include_blank) || @props.delete(:include_blank) @selected = @select_options.delete(:selected) || @props.delete(:selected) @disabled_options = @select_options.delete(:disabled) || @props.delete(:disabled) @options = ::ActiveSupport::TimeZone.all if @time_zone # Define prompt with I18n support define_prompt(prompt_value) end |
Instance Method Details
#build_options_for_select ⇒ Object
Build pre-formatted option tags for select_tag and form.select
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/components/fluxbit/form/select_component.rb', line 97 def # If options are already HTML (from options_for_select/grouped_options_for_select), use as-is if html = @options.dup # Add prompt if needed and not already in the HTML (only for select_tag) html = add_prompt_to_html(html) if @prompt && !html.include?(@prompt.to_s) && !using_form_builder? return html end # Otherwise, build the HTML from raw data html = if @grouped ( @options, @selected, disabled: @disabled_options, divider: @divider ) elsif @time_zone (@selected) else (@options, selected: @selected, disabled: @disabled_options) end # Add prompt option at the beginning if specified # Only add it manually for select_tag (form.select handles it via options hash) html = add_prompt_to_html(html) if @prompt && !using_form_builder? html end |
#build_select_options_hash ⇒ Object
Build options hash for form.select (prompt, include_blank, etc.) Note: Don’t include selected/disabled if options are pre-formatted HTML
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/components/fluxbit/form/select_component.rb', line 82 def = @select_options.dup [:prompt] = @prompt if @prompt [:include_blank] = @include_blank if @include_blank # Only add selected/disabled if we're building options from raw data unless [:selected] = @selected if @selected [:disabled] = @disabled_options if @disabled_options end end |
#input ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/components/fluxbit/form/select_component.rb', line 53 def input if @form.present? && @attribute.present? # form.select(attribute, choices, options = {}, html_options = {}) # For form builder with time zones, use the specialized helper if @time_zone @form.time_zone_select(@attribute, nil, , @props) else # form.select can accept raw choices OR pre-formatted option tags # We use pre-formatted tags for consistency with grouped selects @form.select( @attribute, , , @props ) end else # select_tag(name, option_tags = nil, options = {}) # option_tags should be pre-formatted HTML select_tag( @name, , @props ) end end |