Class: Primer::Beta::AutoComplete

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

Overview

Use ‘AutoComplete` to provide a user with a list of selectable suggestions that appear when they type into the input field. This list is populated by server search results.

Defined Under Namespace

Classes: Item

Constant Summary

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

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(label_text:, src:, list_id:, input_id:, is_label_visible: true, **system_arguments) ⇒ AutoComplete

Returns a new instance of AutoComplete.

Examples:

Default

<%= render(Primer::Beta::AutoComplete.new(label_text: "Fruits", src: "/auto_complete", input_id: "fruits-input-1", list_id: "fruits-popup-1", position: :relative)) %>

With Non-Visible Label

<%= render(Primer::Beta::AutoComplete.new(label_text: "Fruits", src: "/auto_complete", input_id: "fruits-input-2", list_id: "fruits-popup-2", is_label_visible: false, position: :relative)) %>

With Custom Classes for the Results

<%= render(Primer::Beta::AutoComplete.new(label_text: "Fruits", src: "/auto_complete", input_id: "fruits-input-3", list_id: "fruits-popup-3", position: :relative)) do |c| %>
  <% c.results(classes: "custom-class") do %>
    <%= render(Primer::Beta::AutoComplete::Item.new(selected: true, value: "apple")) do |c| %>
      Apple
    <% end %>
    <%= render(Primer::Beta::AutoComplete::Item.new(value: "orange")) do |c| %>
      Orange
    <% end %>
  <% end %>
<% end %>

With Icon

<%= render(Primer::Beta::AutoComplete.new(label_text: "Fruits", src: "/auto_complete", list_id: "fruits-popup-4", input_id: "fruits-input-4", position: :relative)) do |c| %>
  <% c.icon(icon: :search) %>
<% end %>

With Icon and Non-Visible Label

<%= render(Primer::Beta::AutoComplete.new(label_text: "Fruits", src: "/auto_complete", list_id: "fruits-popup-5", input_id: "fruits-input-5", is_label_visible: false, position: :relative)) do |c| %>
  <% c.icon(icon: :search) %>
<% end %>

Parameters:

  • label_text (String)

    The label of the input.

  • src (String)

    The route to query.

  • input_id (String)

    Id of the input element.

  • list_id (String)

    Id of the list element.

  • is_label_visible (Boolean) (defaults to: true)

    Controls if the label is visible. If ‘false`, screen reader only text will be added.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



68
69
70
71
72
73
74
75
76
77
78
# File 'app/components/primer/beta/auto_complete.rb', line 68

def initialize(label_text:, src:, list_id:, input_id:, is_label_visible: true, **system_arguments)
  @label_text = label_text
  @list_id = list_id
  @input_id = input_id
  @is_label_visible = is_label_visible

  @system_arguments = deny_tag_argument(**system_arguments)
  @system_arguments[:tag] = "auto-complete"
  @system_arguments[:src] = src
  @system_arguments[:for] = list_id
end

Instance Method Details

#before_renderObject

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



81
82
83
# File 'app/components/primer/beta/auto_complete.rb', line 81

def before_render
  results(classes: "") unless results
end