Class: Yattho::Alpha::AutoComplete

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

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

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.

Examples:

Default

@description
  Labels are stacked by default.
@code
  <%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", input_id: "fruits-input--default", list_id: "fruits-popup--default")) %>

With inline label

@description
  Labels can be inline by setting `is_label_inline: true`. However, labels will always become stacked on smaller screen sizes.
@code
  <%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", is_label_inline: true, input_id: "fruits-input--inline-label", list_id: "fruits-popup--inline-label")) %>

With non-visible label

@description
  A non-visible label may be rendered with `is_label_visible: false`, but it is highly discouraged. See <%= link_to_accessibility %>.
@code
  <%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", input_id: "fruits-input--non-visible-label", list_id: "fruits-popup--non-visible-label", is_label_visible: false)) %>

With icon

@description
  To display a search icon, set `with_icon` to `true`.
@code
  <%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", list_id: "fruits-popup--icon", input_id: "fruits-input--icon", with_icon: true)) %>

With icon and non-visible label

<%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", list_id: "fruits-popup--icon-no-label", input_id: "fruits-input--icon-no-label", with_icon: true, is_label_visible: false)) %>

With clear button

<%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", input_id: "fruits-input--clear", list_id: "fruits-popup--clear", is_clearable: true)) %>

With custom classes for the input

<%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", input_id: "fruits-input--custom-input", list_id: "fruits-popup--custom-input")) do |component| %>
  <% component.with_input(classes: "custom-class") %>
<% end %>

With custom classes for the results

<%= render(Yattho::Alpha::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete?version=alpha", input_id: "fruits-input--custom-results", list_id: "fruits-popup--custom-results")) do |component| %>
  <% component.with_results(classes: "custom-class") do %>
    <%= render(Yattho::Alpha::AutoComplete::Item.new(selected: true, value: "apple")) do %>
      Apple
    <% end %>
    <%= render(Yattho::Alpha::AutoComplete::Item.new(value: "orange")) do %>
      Orange
    <% end %>
  <% end %>
<% end %>

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 %>



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/components/yattho/alpha/auto_complete.rb', line 122

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



139
140
141
142
# File 'app/components/yattho/alpha/auto_complete.rb', line 139

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