Class: Primer::AutoComplete

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/auto_complete.rb,
app/components/primer/auto_complete/item.rb

Overview

Use AutoComplete to populate input values from server search results.

Defined Under Namespace

Classes: Item

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 TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

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) ⇒ AutoComplete

Returns a new instance of AutoComplete.

Examples:

Default

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

With custom classes for the results

<%= render(Primer::AutoComplete.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::AutoComplete::Item.new(selected: true, value: "value")) do |c| %>
      Selected
    <% end %>
    <%= render(Primer::AutoComplete::Item.new(value: "value")) do |c| %>
      Not selected
    <% end %>
  <% end %>
<% end %>

With Icon

<%= render(Primer::AutoComplete.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::AutoComplete::Item.new(selected: true, value: "value")) do |c| %>
      Selected
    <% end %>
    <%= render(Primer::AutoComplete::Item.new(value: "value")) do |c| %>
      Not selected
    <% end %>
  <% end %>
<% end %>

Parameters:

  • src (String)

    The route to query.

  • id (String)

    Id of the list element.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



82
83
84
85
86
87
88
89
# File 'app/components/primer/auto_complete.rb', line 82

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)


92
93
94
95
96
# File 'app/components/primer/auto_complete.rb', line 92

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

  results(classes: "") unless results
end