Class: NitroKit::Select

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/select.rb

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#builder, from_template

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(options = nil, value: nil, include_empty: false, prompt: nil, index: nil, **attrs)
  @options = options
  @value = value&.to_s
  @include_empty = include_empty
  @prompt = prompt
  @index = index

  super(
    attrs,
    class: wrapper_class
  )
end

Instance Attribute Details

#include_emptyObject (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

#indexObject (readonly)

Returns the value of attribute index.



18
19
20
# File 'app/components/nitro_kit/select.rb', line 18

def index
  @index
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'app/components/nitro_kit/select.rb', line 18

def options
  @options
end

#promptsObject (readonly)

Returns the value of attribute prompts.



18
19
20
# File 'app/components/nitro_kit/select.rb', line 18

def prompts
  @prompts
end

#valueObject (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_optionObject



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_templateObject



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 options
        options.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