Class: NitroKit::Select

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

Constant Summary

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: [], option_tags: nil, id: nil, name: nil, value: nil, include_blank: nil, prompt: nil, disabled: false, required: false, multiple: false, autocomplete: nil, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, desperately_need_a_class: nil) ⇒ Select

Returns a new instance of Select.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
# File 'app/components/nitro_kit/select.rb', line 5

def initialize(
  options: [],
  option_tags: nil,
  id: nil,
  name: nil,
  value: nil,
  include_blank: nil,
  prompt: nil,
  disabled: false,
  required: false,
  multiple: false,
  autocomplete: nil,
  html: {},
  aria: {},
  data: {},
  control_html: {},
  control_aria: {},
  control_data: {},
  desperately_need_a_class: nil
)
  @options = Array(options).map { |choice| Choice.coerce(choice) }.freeze
  @option_tags = validate_option_tags!(option_tags)
  @value = value
  @include_blank = validate_blank_option!(include_blank)
  @prompt = validate_optional_text!(:prompt, prompt)
  if @include_blank && @prompt
    raise ArgumentError, "include_blank: and prompt: are mutually exclusive"
  end
  @disabled = validate_boolean!(:disabled, disabled)
  @required = validate_boolean!(:required, required)
  @multiple = validate_boolean!(:multiple, multiple)
  @name = multiple_name(name)
  @id = id
  @autocomplete = autocomplete
  @control_html = control_html
  @control_aria = control_aria
  @control_data = control_data

  if @option_tags && @options.any?
    raise ArgumentError, "options and option_tags are mutually exclusive"
  end

  super(
    component: :select,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



56
57
58
# File 'app/components/nitro_kit/select.rb', line 56

def id
  @id
end

#include_blankObject (readonly)

Returns the value of attribute include_blank.



56
57
58
# File 'app/components/nitro_kit/select.rb', line 56

def include_blank
  @include_blank
end

#nameObject (readonly)

Returns the value of attribute name.



56
57
58
# File 'app/components/nitro_kit/select.rb', line 56

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



56
57
58
# File 'app/components/nitro_kit/select.rb', line 56

def options
  @options
end

#promptObject (readonly)

Returns the value of attribute prompt.



56
57
58
# File 'app/components/nitro_kit/select.rb', line 56

def prompt
  @prompt
end

#valueObject (readonly)

Returns the value of attribute value.



56
57
58
# File 'app/components/nitro_kit/select.rb', line 56

def value
  @value
end

Instance Method Details

#view_templateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/components/nitro_kit/select.rb', line 58

def view_template
  span(**root_attributes) do
    select(**control_attributes) do
      render_blank_option if include_blank
      render_prompt_option if prompt && selected_values.empty?

      if @option_tags
        raw safe(@option_tags)
      else
        options.each { |choice| render_option(choice) }
      end
    end

    render_toggle_icon
  end
end