Class: FoxTail::AutocompleteComponent

Inherits:
BaseComponent show all
Includes:
Concerns::Formable, Concerns::HasStimulusController, Concerns::Placeholderable
Defined in:
app/components/fox_tail/autocomplete_component.rb

Defined Under Namespace

Classes: StimulusController

Instance Attribute Summary

Attributes inherited from BaseComponent

#html_attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

classname_merger, stimulus_merger, use_stimulus?, #with_html_attributes, #with_html_class

Methods inherited from Base

fox_tail_config

Constructor Details

#initialize(url, html_attributes = {}) ⇒ AutocompleteComponent

Returns a new instance of AutocompleteComponent.



74
75
76
77
78
# File 'app/components/fox_tail/autocomplete_component.rb', line 74

def initialize(url, html_attributes = {})
  super(html_attributes)

  @url = url
end

Class Method Details

.append_option_action(value, text = nil, attributes = {}) ⇒ Object



130
131
132
133
134
135
136
137
# File 'app/components/fox_tail/autocomplete_component.rb', line 130

def append_option_action(value, text = nil, attributes = {})
  actions = stimulus_merger.merge_actions attributes.dig(:data, :action), stimulus_controller.action(:select)
  attributes[:data] ||= {}
  attributes[:data][:action] = actions
  attributes[:data][stimulus_controller.action_param_key(:value)] = value
  attributes[:data][stimulus_controller.action_param_key(:text)] = text
  attributes
end

Instance Method Details

#before_renderObject



93
94
95
96
97
98
99
100
101
# File 'app/components/fox_tail/autocomplete_component.rb', line 93

def before_render
  super

  with_input unless input?
  with_hidden_input unless hidden_input?
  with_dropdown unless dropdown?

  html_attributes[:class] = theme.apply(:root, self)
end

#callObject



103
104
105
106
107
108
109
# File 'app/components/fox_tail/autocomplete_component.rb', line 103

def call
   :div, html_attributes do
    concat hidden_input
    concat input
    concat dropdown
  end
end

#input_idObject



88
89
90
91
# File 'app/components/fox_tail/autocomplete_component.rb', line 88

def input_id
  options[:input_id] ||=
    "#{self.class.name.underscore.gsub("_component", "").gsub("/", "_")}_#{SecureRandom.base58(6)}"
end

#render_search_results(options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


118
119
120
121
122
# File 'app/components/fox_tail/autocomplete_component.rb', line 118

def render_search_results(options = {}, &block)
  raise ArgumentError, "block not given" unless block

  render FoxTail::DropdownComponent.new(options).with_menu, &block
end

#render_search_results_in(view_context, options = {}, &block) ⇒ Object



124
125
126
127
# File 'app/components/fox_tail/autocomplete_component.rb', line 124

def render_search_results_in(view_context, options = {}, &block)
  set_original_view_context view_context
  render_search_results options, &block
end

#stimulus_controller_optionsObject



111
112
113
114
115
116
# File 'app/components/fox_tail/autocomplete_component.rb', line 111

def stimulus_controller_options
  {
    url: @url,
    param: param
  }
end

#textObject



84
85
86
# File 'app/components/fox_tail/autocomplete_component.rb', line 84

def text
  options.key?(:text) ? options[:text].presence : value
end

#valueObject



80
81
82
# File 'app/components/fox_tail/autocomplete_component.rb', line 80

def value
  options.key?(:value) ? options[:value].presence : value_from_object
end