Class: Satis::Dropdown::Component
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Satis::Dropdown::Component
- Defined in:
- app/components/satis/dropdown/component.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #custom_item_html? ⇒ Boolean
-
#hidden_value ⇒ Object
Deal with context.
-
#initialize(form:, attribute:, **options, &block) ⇒ Component
constructor
A new instance of Component.
- #item_html(item) ⇒ Object
- #placeholder ⇒ Object
- #text_method ⇒ Object
- #value_method ⇒ Object
Constructor Details
#initialize(form:, attribute:, **options, &block) ⇒ Component
Returns a new instance of Component.
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 |
# File 'app/components/satis/dropdown/component.rb', line 8 def initialize(form:, attribute:, **, &block) super @form = form @attribute = attribute @title = title @options = @url = [:url] @chain_to = [:chain_to] @free_text = [:free_text] @needs_exact_match = [:needs_exact_match] @reset_button = [:reset_button] || [:include_blank] [:input_html] ||= {} [:input_html][:value] = hidden_value [:input_html][:autofocus] ||= false if [:input_html][:autofocus] [:autofocus] = 'autofocus' [:input_html].delete(:autofocus) end actions = [[:input_html]['data-action'], 'change->satis-dropdown#display', 'focus->satis-dropdown#focus'].join(' ') unless [:input_html]['data-reflex'] [:input_html].merge!('data-satis-dropdown-target' => 'hiddenInput', 'data-action' => actions) @block = block @page_size = [:page_size] || 10 end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def attribute @attribute end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def form @form end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def @options end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def title @title end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def url @url end |
Instance Method Details
#custom_item_html? ⇒ Boolean
68 69 70 |
# File 'app/components/satis/dropdown/component.rb', line 68 def custom_item_html? !!@block end |
#hidden_value ⇒ Object
Deal with context
41 42 43 44 45 46 47 48 |
# File 'app/components/satis/dropdown/component.rb', line 41 def hidden_value value = @options[:selected] value ||= @options.dig(:input_html, :value) value ||= form.object&.send(attribute) value = value.id if value.respond_to? :id value = value.second if value.is_a?(Array) && value.size == 2 && value.first.casecmp?(value.second) value end |
#item_html(item) ⇒ Object
72 73 74 |
# File 'app/components/satis/dropdown/component.rb', line 72 def item_html(item) form.template.capture { @block.call(item) } end |
#placeholder ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'app/components/satis/dropdown/component.rb', line 50 def placeholder return title if title.present? if form.object.class.respond_to?(:human_attribute_name) form.object.class.human_attribute_name(attribute) else attribute.to_s.humanize end end |
#text_method ⇒ Object
64 65 66 |
# File 'app/components/satis/dropdown/component.rb', line 64 def text_method [:text_method] || :name end |
#value_method ⇒ Object
60 61 62 |
# File 'app/components/satis/dropdown/component.rb', line 60 def value_method [:value_method] || :id end |