Class: Primer::AutoCompleteComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/auto_complete_component.rb

Overview

Use AutoComplete to populate input values from server search results.

Constant Summary collapse

DEFAULT_INPUT_TYPE =
:text
INPUT_TYPE_OPTIONS =
[DEFAULT_INPUT_TYPE, :search].freeze

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(src:, id:, **system_arguments) ⇒ AutoCompleteComponent

Returns a new instance of AutoCompleteComponent.

Examples:

Default

<%= render(Primer::AutoCompleteComponent.new(src: "/users/search", id: "user-popup", position: :relative)) do |c| %>
  <% c.input(type: :text, name: "input") %>
  <% c.results do %>
    <%= render(Primer::AutoCompleteItemComponent.new(selected: true, value: "value")) do |c| %>
      Selected
    <% end %>
    <%= render(Primer::AutoCompleteItemComponent.new(value: "value")) do |c| %>
      Not selected
    <% end %>
  <% end %>
<% end %>

With custom classes for the results

<%= render(Primer::AutoCompleteComponent.new(src: "/users/search", id: "user-popup", position: :relative)) do |c| %>
  <% c.input(type: :text, name: "input") %>
  <% c.results(classes: "my-custom-class") do %>
    <%= render(Primer::AutoCompleteItemComponent.new(selected: true, value: "value")) do |c| %>
      Selected
    <% end %>
    <%= render(Primer::AutoCompleteItemComponent.new(value: "value")) do |c| %>
      Not selected
    <% end %>
  <% end %>
<% end %>

With Icon

<%= render(Primer::AutoCompleteComponent.new(src: "/users/search", id: "user-popup", position: :relative)) do |c| %>
  <% c.input(type: :text, name: "input") %>
  <% c.icon(icon: :search) %>
  <% c.results do %>
    <%= render(Primer::AutoCompleteItemComponent.new(selected: true, value: "value")) do |c| %>
      Selected
    <% end %>
    <%= render(Primer::AutoCompleteItemComponent.new(value: "value")) do |c| %>
      Not selected
    <% end %>
  <% end %>
<% end %>


80
81
82
83
84
85
86
87
# File 'app/components/primer/auto_complete_component.rb', line 80

def initialize(src:, id:, **system_arguments)
  @id = id

  @system_arguments = system_arguments
  @system_arguments[:tag] = "auto-complete"
  @system_arguments[:src] = src
  @system_arguments[:for] = id
end

Instance Method Details

#before_renderObject

add ‘results` without needing to explicitly call it in the view

Raises:

  • (ArgumentError)


90
91
92
93
94
# File 'app/components/primer/auto_complete_component.rb', line 90

def before_render
  raise ArgumentError, "Missing `input` slot" if input.blank?

  results(classes: "") unless results
end