Class: LightningUiKit::ComboboxComponent
- Inherits:
-
BaseComponent
- Object
- BaseComponent
- LightningUiKit::ComboboxComponent
- Defined in:
- app/components/lightning_ui_kit/combobox_component.rb
Instance Method Summary collapse
-
#association_mode? ⇒ Boolean
Nested-attributes mode: triggered by
foreign_key:alone so it can be used without an AR association (options/selected supplied explicitly). - #classes ⇒ Object
- #combobox_values ⇒ Object
- #data ⇒ Object
- #description_data ⇒ Object
- #error_data ⇒ Object
- #hidden_field_name ⇒ Object
-
#initialize(name: nil, form: nil, label: nil, description: nil, placeholder: nil, disabled: false, error: nil, multiple: false, allow_custom: false, options: [], selected: nil, url: nil, min_chars: 0, debounce: 300, association: nil, foreign_key: nil, nested_model: nil, collection: nil, label_method: :to_s, value_method: :id, **options_hash) ⇒ ComboboxComponent
constructor
A new instance of ComboboxComponent.
- #input_classes ⇒ Object
- #input_data ⇒ Object
- #label_data ⇒ Object
- #scoped_field_name ⇒ Object
Methods included from Labelable
#effective_label, #render_label?
Methods included from Errors
#error_messages, #has_errors?, #infer_errors_from_association
Constructor Details
#initialize(name: nil, form: nil, label: nil, description: nil, placeholder: nil, disabled: false, error: nil, multiple: false, allow_custom: false, options: [], selected: nil, url: nil, min_chars: 0, debounce: 300, association: nil, foreign_key: nil, nested_model: nil, collection: nil, label_method: :to_s, value_method: :id, **options_hash) ⇒ ComboboxComponent
Returns a new instance of ComboboxComponent.
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 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 5 def initialize( name: nil, form: nil, label: nil, description: nil, placeholder: nil, disabled: false, error: nil, multiple: false, allow_custom: false, options: [], selected: nil, url: nil, min_chars: 0, debounce: 300, association: nil, foreign_key: nil, nested_model: nil, collection: nil, label_method: :to_s, value_method: :id, ** ) @form = form @label = label @description = description @placeholder = placeholder @disabled = disabled @error = error @allow_custom = allow_custom @url = url @min_chars = min_chars @debounce = debounce @options_hash = @association = association @nested_model = nested_model @foreign_key = foreign_key || derive_foreign_key @collection = collection @label_method = label_method @value_method = value_method if association_mode? @name = @association ? nested_attributes_name : name @multiple = true @options = @collection ? : @selected = @association ? derive_selected_from_association : (selected || []) else @name = name @multiple = multiple @options = @selected = selected end end |
Instance Method Details
#association_mode? ⇒ Boolean
Nested-attributes mode: triggered by foreign_key: alone so it can be used
without an AR association (options/selected supplied explicitly). Matches the
Stimulus controller, which keys off the foreign-key value.
102 103 104 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 102 def association_mode? @foreign_key.present? end |
#classes ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 60 def classes merge_classes([ "lui:[&>[data-slot=label]+[data-slot=control]]:mt-3", "lui:[&>[data-slot=label]+[data-slot=description]]:mt-1", "lui:[&>[data-slot=description]+[data-slot=control]]:mt-3", "lui:[&>[data-slot=control]+[data-slot=description]]:mt-3", "lui:[&>[data-slot=control]+[data-slot=error]]:mt-3", "lui:*:data-[slot=label]:font-medium", @options_hash[:class] ].compact.join(" ")) end |
#combobox_values ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 80 def combobox_values base = { "lui-combobox-multiple-value" => @multiple, "lui-combobox-allow-custom-value" => @allow_custom, "lui-combobox-url-value" => @url, "lui-combobox-min-chars-value" => @min_chars, "lui-combobox-debounce-value" => @debounce, "lui-combobox-options-value" => , "lui-combobox-selected-value" => selected_json } if association_mode? base["lui-combobox-foreign-key-value"] = @foreign_key.to_s base["lui-combobox-nested-model-value"] = @nested_model.to_s if @nested_model end base.compact end |
#data ⇒ Object
72 73 74 75 76 77 78 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 72 def data default_data = { controller: "lui-combobox", action: "click@window->lui-combobox#clickOutside" } default_data.merge(@options_hash[:data] || {}).merge(combobox_values) end |
#description_data ⇒ Object
135 136 137 138 139 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 135 def description_data {slot: "description"}.merge(@options_hash[:description_data] || {}).tap do |data| data[:disabled] = "true" if @disabled end end |
#error_data ⇒ Object
141 142 143 144 145 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 141 def error_data {slot: "error"}.merge(@options_hash[:error_data] || {}).tap do |data| data[:disabled] = "true" if @disabled end end |
#hidden_field_name ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 147 def hidden_field_name base_name = scoped_field_name if association_mode? base_name elsif @multiple "#{base_name}[]" else base_name end end |
#input_classes ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 115 def input_classes merge_classes([ "lui:relative lui:block lui:w-full lui:appearance-none lui:rounded-lg", "lui:px-[calc(--spacing(3.5)-1px)] lui:py-[calc(--spacing(2.5)-1px)]", "lui:sm:px-[calc(--spacing(3)-1px)] lui:sm:py-[calc(--spacing(1.5)-1px)]", "lui:text-base/6 lui:text-foreground lui:placeholder:text-foreground-muted lui:sm:text-sm/6", "lui:border lui:border-border lui:data-[hover]:border-border-hover", "lui:bg-transparent lui:focus:outline-hidden", "lui:data-invalid:border-destructive-border lui:data-invalid:data-[hover]:border-destructive-border/60", "lui:data-disabled:border-border-hover", "lui:pr-10" ].join(" ")) end |
#input_data ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 106 def input_data { "lui-combobox-target" => "input", :action => "input->lui-combobox#onInput focus->lui-combobox#onFocus keydown->lui-combobox#onKeydown" }.tap do |d| d[:invalid] = "true" if has_errors? end end |
#label_data ⇒ Object
129 130 131 132 133 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 129 def label_data {slot: "label"}.merge(@options_hash[:label_data] || {}).tap do |data| data[:disabled] = "true" if @disabled end end |
#scoped_field_name ⇒ Object
159 160 161 162 163 164 165 |
# File 'app/components/lightning_ui_kit/combobox_component.rb', line 159 def scoped_field_name if @form "#{@form.object_name}[#{@name}]" else @name.to_s end end |