Class: IronAdmin::Form::BelongsToAutocompleteComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- IronAdmin::Form::BelongsToAutocompleteComponent
- Includes:
- Concerns::FormInputBehavior
- Defined in:
- app/components/iron_admin/form/belongs_to_autocomplete_component.rb
Overview
Renders an autocomplete input for belongs_to associations with many records.
Used automatically when association has more than 100 records,
or can be forced via autocomplete: true option.
Instance Attribute Summary collapse
-
#autocomplete_url ⇒ String
Autocomplete endpoint URL.
-
#has_error ⇒ Boolean
readonly
Whether input has error state.
-
#name ⇒ String
readonly
Input name attribute.
-
#placeholder ⇒ String
readonly
Placeholder text.
-
#resource_name ⇒ String
readonly
Resource name for autocomplete URL.
-
#selected_id ⇒ Integer?
readonly
Currently selected ID.
-
#selected_label ⇒ String?
readonly
Display label for selected record.
Instance Method Summary collapse
-
#before_render ⇒ void
private
Sets the autocomplete URL before rendering.
-
#component_id ⇒ String
private
Unique component DOM ID.
-
#dropdown_classes ⇒ String
private
CSS classes for autocomplete dropdown panel.
-
#dropdown_item_classes ⇒ String
private
CSS classes for dropdown list items.
-
#initialize(name:, resource_name:, selected_id: nil, selected_label: nil, placeholder: nil, disabled: false, has_error: false, field: nil, current_user: nil) ⇒ BelongsToAutocompleteComponent
constructor
A new instance of BelongsToAutocompleteComponent.
-
#input_classes ⇒ String
private
CSS classes for text input field.
Methods included from Concerns::FormInputBehavior
#effectively_disabled?, #field_readonly?, #theme
Constructor Details
#initialize(name:, resource_name:, selected_id: nil, selected_label: nil, placeholder: nil, disabled: false, has_error: false, field: nil, current_user: nil) ⇒ BelongsToAutocompleteComponent
Returns a new instance of BelongsToAutocompleteComponent.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 50 def initialize(name:, resource_name:, selected_id: nil, selected_label: nil, placeholder: nil, disabled: false, has_error: false, field: nil, current_user: nil) @name = name @resource_name = resource_name @selected_id = selected_id @selected_label = selected_label @placeholder = placeholder || "Search..." @disabled = disabled @has_error = has_error @field = field @current_user = current_user end |
Instance Attribute Details
#autocomplete_url ⇒ String
Returns Autocomplete endpoint URL.
39 40 41 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 39 def autocomplete_url @autocomplete_url end |
#has_error ⇒ Boolean (readonly)
Returns Whether input has error state.
36 37 38 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 36 def has_error @has_error end |
#name ⇒ String (readonly)
Returns Input name attribute.
21 22 23 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 21 def name @name end |
#placeholder ⇒ String (readonly)
Returns Placeholder text.
33 34 35 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 33 def placeholder @placeholder end |
#resource_name ⇒ String (readonly)
Returns Resource name for autocomplete URL.
24 25 26 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 24 def resource_name @resource_name end |
#selected_id ⇒ Integer? (readonly)
Returns Currently selected ID.
27 28 29 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 27 def selected_id @selected_id end |
#selected_label ⇒ String? (readonly)
Returns Display label for selected record.
30 31 32 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 30 def selected_label @selected_label end |
Instance Method Details
#before_render ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Sets the autocomplete URL before rendering.
67 68 69 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 67 def before_render self.autocomplete_url = helpers.iron_admin.autocomplete_path(resource_name) end |
#component_id ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Unique component DOM ID.
96 97 98 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 96 def component_id @component_id ||= "autocomplete-#{SecureRandom.hex(4)}" end |
#dropdown_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns CSS classes for autocomplete dropdown panel.
83 84 85 86 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 83 def dropdown_classes "absolute z-50 mt-1 w-full max-h-60 overflow-auto #{theme.border_radius} " \ "#{theme.card_bg} border #{theme.input_border} shadow-lg" end |
#dropdown_item_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns CSS classes for dropdown list items.
90 91 92 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 90 def dropdown_item_classes "px-3 py-2 text-sm cursor-pointer hover:bg-gray-100 #{theme.body_text}" end |
#input_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns CSS classes for text input field.
73 74 75 76 77 78 79 |
# File 'app/components/iron_admin/form/belongs_to_autocomplete_component.rb', line 73 def input_classes base = "block w-full border px-3 py-2 text-sm shadow-sm outline-none transition duration-150 ease-in-out " \ "#{theme.border_radius} #{theme.input_border} #{theme.card_bg} #{theme.body_text} #{theme.input_focus}" base += " !border-red-400 !focus:border-red-500 !focus:ring-red-500/20" if has_error base += " bg-gray-50 cursor-not-allowed" if effectively_disabled? base end |