Class: Primer::Alpha::AutoComplete

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/alpha/auto_complete.rb,
app/components/primer/alpha/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

Constants included from Primer::AttributesHelper

Primer::AttributesHelper::PLURAL_ARIA_ATTRIBUTES, Primer::AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

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

Methods included from Primer::AttributesHelper

#aria, #data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Constructor Details

#initialize(label_text:, src:, list_id:, input_id:, input_name: nil, is_label_visible: true, is_label_inline: false, with_icon: false, is_clearable: false, **system_arguments) ⇒ AutoComplete

Returns a new instance of AutoComplete.

Parameters:

  • label_text (String)

    The label of the input.

  • src (String)

    The route to query.

  • input_id (String)

    Id of the input element.

  • input_name (String) (defaults to: nil)

    Optional name of the input element, defaults to ‘input_id` when not set.

  • list_id (String)

    Id of the list element.

  • with_icon (Boolean) (defaults to: false)

    Controls if a search icon is visible, defaults to ‘false`.

  • is_label_visible (Boolean) (defaults to: true)

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

  • is_clearable (Boolean) (defaults to: false)

    Adds optional clear button.

  • is_label_inline (Boolean) (defaults to: false)

    Controls if the label is inline. On smaller screens, label will always become stacked.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/components/primer/alpha/auto_complete.rb', line 75

def initialize(label_text:, src:, list_id:, input_id:, input_name: nil, is_label_visible: true, is_label_inline: false, with_icon: false, is_clearable: false, **system_arguments)
  @label_text = label_text
  @list_id = list_id
  @input_id = input_id
  @input_name = input_name || input_id
  @is_label_visible = is_label_visible
  @with_icon = with_icon
  @is_clearable = is_clearable
  @label_classes = label_classes(is_label_visible: is_label_visible, is_label_inline: is_label_inline)
  @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 ‘input` and `results` without needing to explicitly call them in the view



91
92
93
94
# File 'app/components/primer/alpha/auto_complete.rb', line 91

def before_render
  with_results(classes: "") unless results?
  with_input(classes: "") unless input?
end