Auto Suggest
Auto Suggest provides a short list of suggested responses based on a user’s input. The suggestions may include icons and other information to supplement the data.
Auto Suggest
This example allows users to see a list of available options. The options narrow down as the user types.
When to Use
- When it is possible to select from a broad list of options.
- When the list of options from which to select is dynamic, like a list of crew members.
When Not to Use
- When there is a short or predetermined list of options from which to select. Instead, consider a select input.
- When multiple options may be selected. Consider a multi-select input instead.
Examples
- Auto Suggest (shown above)
- Auto Suggest with Multiple Categories
- Auto Suggest with a Single Action
Usage
- In general, the Auto Suggest input field should stretch to fit the width of its container.
- Keep suggested terms simple and concise, since lists that wrap are harder to scan.
- Limit suggestions to 7 options (max), to prevent overwhelming the user with too many choices. If users require more context or assistance up front, a full list of options may be configured to display as soon as the field comes into focus. See the Developer tab for more information.
Content Options
To provide more clarity, additional content may display in the list of suggested options, such as iconography and auxiliary information.
Examples
Auto Suggest with Multiple Categories
This example allows users to view relevant options that are categorized into groups. Limit options to just a few per category, to prevent information overload.
When to Use
- When multiple groups of options are presented in a single list.
When Not to Use
- Do not use if you have a single category of options to display. Instead, consider adding iconography or text labels to individual options.
Auto Suggest with a Single Action
This example allows the addition of a single link that is related to the user’s search term.
When to Use
- If added functionality can assist the user in making a selection or advancing through the process, such as a link to an Advanced Search on the user’s search term.
When Not to Use
- For links that are not contextually related to the Auto Suggest search, such as a form of navigation.
Developer Notes
The Auto Suggest component is constructed using the spark-auto-suggest
class. Contained within the component are a text field with the class spark-auto-suggest__field
, a label with the class spark-label
, an optional button with the class spark-auto-suggest__clear-btn
, and a list element with the class spark-auto-suggest__listbox
. The initial three elements are contained within a label
element.
The component is instantiated using new Spark.AutoSuggest(el)
. The following options may be passed when using new Spark.AutoSuggest(el, {});
:
For all available methods and full API, refer to the component code on Bitbucket.
Auto Suggest
Auto Suggest - Auxiliary Information
Auto Suggest - Error
Auto Suggest - Show All Terms
To create an Auto Suggest that shows all terms on initial focus, add a data-show-all-terms
attribute to the spark-auto-suggest
element or pass
Auto Suggest - Show No Results Found
Auto Suggest with Multiple Categories
A custom function is required to create an Auto Suggest with grouped Categories. This is because it might be necessary to apply different weights to the categories instead of merely displaying the first few matched terms and their categories, or an alphabetical list of categories. A custom function also allows you to perform your own custom regex matching of terms if desired.
Auto Suggest with a Single Action
Similar to the previous Multiple Categories example, a custom function is required to create an Auto Suggest with a Single Action link appended to the Auto Suggest list. This is because the appended link could be contextual to the user’s input.
Additional Notes
- While the Auto Suggest is designed to stretch to the full width of its container, there might be a need to adjust this to suit a particular design. If that is the case, ensure that the minimum width for the suggested options overlay is 296px, though the field itself may be smaller.
- If working with a custom implementation, ensure that the
<mark>
element is used to highlight the additional text string, rather than the text that the user has entered when matching input to a result set. This makes it easier for the user to scan for the differences between options. - If looking to use the component’s rendering functionality but with a dynamic list of terms, you can update the contents of the terms array/object by accessing it via the instance of the component.
- If the Auto Suggest needs to be disabled, ensure that the
disabled
attribute is applied to thespark-auto-suggest__field
element as well as thespark-auto-suggest__clear-btn
element if it is in use.
Accessibility Notes
The overarching Auto Suggest component and the child elements found within it require additional attributes to make the component more accessible to assistive technologies. These attributes are discussed below:
Parent Auto Suggest Container
The spark-auto-suggest
element requires the following attributes:
- A
role="combobox"
attribute to identify the component as a combobox element. - An
aria-expanded="false"
attribute that indicates that the listbox element of the component is not displayed. This will be toggled by Javascript depending on the state of the listbox. - An
aria-haspopup="listbox"
attribute that indicates that the component can popup a listbox to suggest values.
Auto Suggest Text Input
Similar to the parent spark-auto-suggest
element, spark-auto-suggest__field
input requires the following attributes:
- An
aria-labelledby=""
attribute with the value set to theid
of the element that has the text to be used as a label for the field. In the provided examples, aspark-label
element is included with the text to make available. - An
aria-autocomplete="list"
to indicate that the autocomplete behavior of the input field is to present a list of possible values in a popup and that these suggestions are related to the value currently present in the input field. - An
aria-controls=""
attribute with the value set to theid
of the listbox element. - An
aria-activedescendant=""
with no value set. As a user interacts with the component, the value will be updated based on the user’s selection.
Auto Suggest Listbox
The Listbox is the container that displays suggestions to users. This container needs to be “connected” to the other elements described above using the following attributes:
- An
id
attribute whose value is used as the value for thearia-controls
attribute found on the input field. - A
role="listbox"
attribute to identify the list as a listbox.
Other ARIA attributes that apply to the list items are managed by Javascript.
Icons in Auto Suggest Results
When using Spark Icons as part of an Auto Suggest result set, as shown in the Content Options examples above, aria-hidden
attributes should be set to spark-icon
elements unless the icons are intended to be announced to users. If the latter, use aria-label
attributes to provide additional content and context to users.