Class: TaoForm::Components::SelectComponent

Inherits:
FieldComponent
  • Object
show all
Defined in:
lib/tao_form/components/select_component.rb

Instance Attribute Summary collapse

Attributes inherited from FieldComponent

#attribute_name, #builder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, builder, attribute_name, choices = nil, options = {}, html_options = {}) ⇒ SelectComponent

Returns a new instance of SelectComponent.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tao_form/components/select_component.rb', line 11

def initialize view, builder, attribute_name, choices = nil, options = {}, html_options = {}
  super view, builder, attribute_name, options
  @choices = choices
  @max_list_size = html_options.delete(:max_list_size)
  @disabled = html_options[:disabled].presence || false
  @multiple = @options.delete(:multiple) || html_options.delete(:multiple) || false

  html_options[:multiple] = @multiple
  html_options[:remote] = @options.delete(:remote)

  if html_options[:remote].present? && html_options[:remote].is_a?(Hash)
    html_options[:remote] = html_options[:remote].to_json
  end

  @html_options = transform_html_options html_options
end

Instance Attribute Details

#block_for_renderObject (readonly)

Returns the value of attribute block_for_render.



9
10
11
# File 'lib/tao_form/components/select_component.rb', line 9

def block_for_render
  @block_for_render
end

#choicesObject (readonly)

Returns the value of attribute choices.



9
10
11
# File 'lib/tao_form/components/select_component.rb', line 9

def choices
  @choices
end

#disabledObject (readonly)

Returns the value of attribute disabled.



9
10
11
# File 'lib/tao_form/components/select_component.rb', line 9

def disabled
  @disabled
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



9
10
11
# File 'lib/tao_form/components/select_component.rb', line 9

def html_options
  @html_options
end

#max_list_sizeObject (readonly)

Returns the value of attribute max_list_size.



9
10
11
# File 'lib/tao_form/components/select_component.rb', line 9

def max_list_size
  @max_list_size
end

#multipleObject (readonly)

Returns the value of attribute multiple.



9
10
11
# File 'lib/tao_form/components/select_component.rb', line 9

def multiple
  @multiple
end

Class Method Details

.component_nameObject



66
67
68
# File 'lib/tao_form/components/select_component.rb', line 66

def self.component_name
  :select
end

Instance Method Details

#clearableObject



50
51
52
# File 'lib/tao_form/components/select_component.rb', line 50

def clearable
  @clearable ||= options[:include_blank].present? || options[:prompt].present?
end

#placeholderObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tao_form/components/select_component.rb', line 54

def placeholder
  @placeholder ||= if options[:placeholder].present?
    options[:placeholder]
  elsif options[:include_blank].present? && options[:include_blank].is_a?(String)
    options[:include_blank]
  elsif options[:prompt].present? && options[:prompt].is_a?(String)
    options[:prompt]
  else
    t :placeholder
  end
end

#render(&block) ⇒ Object



28
29
30
31
# File 'lib/tao_form/components/select_component.rb', line 28

def render &block
  @block_for_render = block
  super
end

#render_listObject



46
47
48
# File 'lib/tao_form/components/select_component.rb', line 46

def render_list
  view.tao_select_list max_list_size: max_list_size
end

#render_resultObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tao_form/components/select_component.rb', line 33

def render_result
  if multiple
    view.tao_multiple_select_result(
      builder, attribute_name, choices, options, disabled: disabled, &block_for_render
    )
  else
    view.tao_select_result(
      builder, attribute_name, choices, options,
      placeholder: placeholder, clearable: clearable, disabled: disabled, &block_for_render
    )
  end
end