Class: Polaris::SelectComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/polaris/select_component.rb

Constant Summary

Constants included from ViewHelper

ViewHelper::POLARIS_HELPERS, ViewHelper::POLARIS_TEXT_STYLES

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods included from ViewHelper

#polaris_body_styles, #polaris_html_classes, #polaris_html_styles, #polaris_icon_source

Methods included from StylesListHelper

#styles_list

Methods included from OptionHelper

#append_option, #prepend_option

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #fetch_or_fallback_nested

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(options: {}, form: nil, attribute: nil, name: nil, selected: nil, disabled_options: nil, label: nil, prompt: nil, divider: nil, time_zone: nil, label_hidden: false, label_inline: false, label_action: nil, disabled: false, required: false, help_text: nil, error: false, grouped: false, wrapper_arguments: {}, select_options: {}, input_options: {}, **system_arguments) ⇒ SelectComponent

Returns a new instance of SelectComponent.



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/components/polaris/select_component.rb', line 5

def initialize(
  options: {},
  form: nil,
  attribute: nil,
  name: nil,
  selected: nil,
  disabled_options: nil,
  label: nil,
  prompt: nil,
  divider: nil,
  time_zone: nil,
  label_hidden: false,
  label_inline: false,
  label_action: nil,
  disabled: false,
  required: false,
  help_text: nil,
  error: false,
  grouped: false,
  wrapper_arguments: {},
  select_options: {},
  input_options: {},
  **system_arguments
)
  @form = form
  @attribute = attribute
  @name = name
  @options = options
  @selected = selected
  @disabled_options = disabled_options
  @label = label
  @label_hidden = label_hidden
  @label_inline = label_inline
  @disabled = disabled
  @grouped = grouped
  @prompt = prompt
  @divider = divider
  @time_zone = time_zone

  if @time_zone
    @options = ::ActiveSupport::TimeZone.all
  end

  @system_arguments = system_arguments
  @system_arguments[:tag] = "div"
  @system_arguments[:data] ||= {}
  prepend_option(@system_arguments[:data], :controller, "polaris-select")
  prepend_option(@system_arguments[:data], :selected_value, @selected)
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    "Polaris-Select",
    "Polaris-Select--disabled": disabled,
    "Polaris-Select--error": error
  )

  @wrapper_arguments = {
    form: form,
    attribute: attribute,
    name: name,
    label: label,
    label_hidden: hides_label?,
    label_action: label_action,
    required: required,
    help_text: help_text,
    error: error
  }.merge(wrapper_arguments)

  @select_options = select_options

  @input_options = input_options
  @input_options.deep_merge!(select_options) unless @form
  @input_options[:class] = class_names(@input_options[:classes], "Polaris-Select__Input")
  @input_options[:disabled] = disabled
  @input_options[:data] ||= {}
  prepend_option(@input_options[:data], :polaris_select_target, "select")
  prepend_option(@input_options[:data], :action, "polaris-select#update")
end

Instance Method Details

#build_options_for_selectObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/components/polaris/select_component.rb', line 99

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

#hides_label?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/components/polaris/select_component.rb', line 83

def hides_label?
  @label_hidden || @label_inline
end

#selected_optionObject



87
88
89
90
91
92
93
94
95
96
97
# File 'app/components/polaris/select_component.rb', line 87

def selected_option
  if @grouped
    grouped_selected_option
  else
    options_to_find = @time_zone ? @options.map { |z| [z.to_s, z.name] } : @options.to_a
    option = options_to_find.find { |i| i.last.to_s == @selected.to_s }
    return unless option

    option.first
  end
end