Class: NitroKit::Select
- Defined in:
- app/components/nitro_kit/select.rb
Instance Attribute Summary collapse
-
#include_empty ⇒ Object
readonly
Returns the value of attribute include_empty.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prompts ⇒ Object
readonly
Returns the value of attribute prompts.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Attributes inherited from Component
Instance Method Summary collapse
- #html_option ⇒ Object
-
#initialize(options = nil, value: nil, include_empty: false, prompt: nil, index: nil, **attrs) ⇒ Select
constructor
A new instance of Select.
- #option(text = nil, value = nil, **attrs, &block) ⇒ Object
- #view_template ⇒ Object
Methods inherited from Component
Constructor Details
#initialize(options = nil, value: nil, include_empty: false, prompt: nil, index: nil, **attrs) ⇒ Select
Returns a new instance of Select.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'app/components/nitro_kit/select.rb', line 5 def initialize( = nil, value: nil, include_empty: false, prompt: nil, index: nil, **attrs) @options = @value = value&.to_s @include_empty = include_empty @prompt = prompt @index = index super( attrs, class: wrapper_class ) end |
Instance Attribute Details
#include_empty ⇒ Object (readonly)
Returns the value of attribute include_empty.
18 19 20 |
# File 'app/components/nitro_kit/select.rb', line 18 def include_empty @include_empty end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
18 19 20 |
# File 'app/components/nitro_kit/select.rb', line 18 def index @index end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
18 19 20 |
# File 'app/components/nitro_kit/select.rb', line 18 def @options end |
#prompts ⇒ Object (readonly)
Returns the value of attribute prompts.
18 19 20 |
# File 'app/components/nitro_kit/select.rb', line 18 def prompts @prompts end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
18 19 20 |
# File 'app/components/nitro_kit/select.rb', line 18 def value @value end |
Instance Method Details
#html_option ⇒ Object
53 |
# File 'app/components/nitro_kit/select.rb', line 53 alias :html_option :option |
#option(text = nil, value = nil, **attrs, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/components/nitro_kit/select.rb', line 55 def option(text = nil, value = nil, **attrs, &block) builder do value ||= text html_option(value:, selected: @value == value.to_s, **attrs) do if block_given? yield else text end end end end |
#view_template ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/components/nitro_kit/select.rb', line 20 def view_template span(class: wrapper_class, data: { slot: "control" }) do select(**attrs, class: select_class) do if include_empty blank_text = if include_empty.is_a?(String) include_empty elsif @prompt @prompt else "" end html_option(value: "", selected: @value == "") { blank_text } end if .each do |opt| if opt.is_a?(Array) && opt.length >= 2 html_option(value: opt[1], selected: @value == opt[1].to_s) { opt[0] } else # Handle simple strings - use as both label and value html_option(value: opt.to_s, selected: @value == opt.to_s) { opt.to_s } end end else yield if block_given? end end chevron_icon end end |