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.
-
#compact ⇒ Object
readonly
Returns the value of attribute compact.
-
#dropdown_height ⇒ Object
readonly
Returns the value of attribute dropdown_height.
-
#dropdown_max_height ⇒ Object
readonly
Returns the value of attribute dropdown_max_height.
-
#dropdown_min_height ⇒ Object
readonly
Returns the value of attribute dropdown_min_height.
-
#dropdown_width ⇒ Object
readonly
Returns the value of attribute dropdown_width.
-
#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.
- #input_class ⇒ Object
- #input_wrapper_classes ⇒ Object
- #item_html(item) ⇒ Object
- #option_value(item) ⇒ Object
- #options_array(obj) ⇒ Object
- #placeholder ⇒ Object
- #results_classes ⇒ Object
- #results_style ⇒ Object
- #search_input_classes ⇒ 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 39 40 41 42 43 44 45 46 47 |
# 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] @toggle_button = [:toggle_button] != false @dropdown_max_height = [:max_height] @dropdown_min_height = [:min_height] @dropdown_width = [:width] @dropdown_height = [:height] @compact = [:compact] || false [:input_html] ||= {} [:input_html][:value] = hidden_value [:input_html][:autofocus] ||= false if [:input_html][:autofocus] [:autofocus] = "autofocus" [:input_html].delete(:autofocus) end unless [:input_html]["data-reflex"] actions = [[:input_html]["data-action"], "change->satis-dropdown#display", "focus->satis-dropdown#focus"].join(" ") end [:input_html].merge!("data-satis-dropdown-target" => "hiddenSelect", "data-action" => actions) @block = block @page_size = [:page_size] || 25 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 |
#compact ⇒ Object (readonly)
Returns the value of attribute compact.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def compact @compact end |
#dropdown_height ⇒ Object (readonly)
Returns the value of attribute dropdown_height.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def dropdown_height @dropdown_height end |
#dropdown_max_height ⇒ Object (readonly)
Returns the value of attribute dropdown_max_height.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def dropdown_max_height @dropdown_max_height end |
#dropdown_min_height ⇒ Object (readonly)
Returns the value of attribute dropdown_min_height.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def dropdown_min_height @dropdown_min_height end |
#dropdown_width ⇒ Object (readonly)
Returns the value of attribute dropdown_width.
6 7 8 |
# File 'app/components/satis/dropdown/component.rb', line 6 def dropdown_width @dropdown_width 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
110 111 112 |
# File 'app/components/satis/dropdown/component.rb', line 110 def custom_item_html? !!@block end |
#hidden_value ⇒ Object
Deal with context
50 51 52 53 54 55 56 57 58 59 |
# File 'app/components/satis/dropdown/component.rb', line 50 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 |
#input_class ⇒ Object
118 119 120 |
# File 'app/components/satis/dropdown/component.rb', line 118 def input_class [@options.fetch(:input_html, {}).fetch(:class, ""), form.has_error?(attribute) ? "is-invalid" : ""].join(" ") end |
#input_wrapper_classes ⇒ Object
137 138 139 |
# File 'app/components/satis/dropdown/component.rb', line 137 def input_wrapper_classes compact ? 'h-9 p-1 flex rounded' : 'h-12 p-1 flex rounded' end |
#item_html(item) ⇒ Object
114 115 116 |
# File 'app/components/satis/dropdown/component.rb', line 114 def item_html(item) form.template.capture { @block.call(item) } end |
#option_value(item) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/components/satis/dropdown/component.rb', line 71 def option_value(item) text = value = "" if item.respond_to?(:id) value = item.send(:id) text = if item.respond_to?(:name) item.send(:name) else "" end elsif item.is_a?(Array) value = item.first text = item.second elsif item.is_a?(String) text = value = item end return nil if value.blank? [text, item, {selected: true}] end |
#options_array(obj) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'app/components/satis/dropdown/component.rb', line 61 def (obj) return [[]] unless obj if obj.is_a?(Array) obj.filter_map { |item| option_value(item) } else [option_value(obj)] end end |
#placeholder ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'app/components/satis/dropdown/component.rb', line 92 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 |
#results_classes ⇒ Object
131 132 133 134 135 |
# File 'app/components/satis/dropdown/component.rb', line 131 def results_classes classes = "hidden container sts-dropdown-results shadow dark:text-gray-300 z-10 rounded overflow-y-auto w-full" classes += " max-h-select" unless dropdown_max_height classes end |
#results_style ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'app/components/satis/dropdown/component.rb', line 122 def results_style styles = [] styles << "max-height: #{dropdown_max_height}" if dropdown_max_height styles << "min-height: #{dropdown_min_height}" if dropdown_min_height styles << "width: #{dropdown_width}" if dropdown_width styles << "height: #{dropdown_height}" if dropdown_height styles.join('; ') end |
#search_input_classes ⇒ Object
141 142 143 144 145 |
# File 'app/components/satis/dropdown/component.rb', line 141 def search_input_classes base = 'focus:ring-0 border-none p-1 px-2 appearance-none w-full sts-dropdown-input text-gray-800 dark:text-gray-300' base += ' text-sm' if compact base end |
#text_method ⇒ Object
106 107 108 |
# File 'app/components/satis/dropdown/component.rb', line 106 def text_method [:text_method] || :name end |
#value_method ⇒ Object
102 103 104 |
# File 'app/components/satis/dropdown/component.rb', line 102 def value_method [:value_method] || :id end |