amp-autocomplete
Description | An autocomplete-enabled input field suggests completed results corresponding to the user input as they type into the input field. |
Availability | Launched |
Required Script | <script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script> |
Supported Layouts | container |
Examples | See AMP By Example's amp-autocomplete sample. |
Behavior
The amp-autocomplete
extension should be used for suggesting completed items based on user input to help users carry out their task more quickly.
This can be used to power search experiences, in cases where the user may not know the full range of potential inputs, or in forms to help ensure inputs where there may be multiple ways to express the same intent (using a state abbreviation instead of its full name, for example) yield more predictable results.
Example:
<amp-autocomplete filter="substring" id="myAutocomplete"> <input /> <script type="application/json"> { "items": ["a", "b", "c"] } </script> </amp-autocomplete>
Attributes
filter (required) | The filtering mechanism applied to source data to produce filtered results for user input. In all cases the filtered results will be displayed in array order of data retrieved. If filtering is being done (filter != none ), it is done client side. The following are supported values:
|
src (optional) | The URL of the remote endpoint that returns the JSON that will be filtered and rendered within this amp-autocomplete . This must be a CORS HTTP service and the URL's protocol must be HTTPS. The endpoint must implement the requirements specified in the CORS Requests in AMP spec. If fetching the data at the src URL fails, the amp-autocomplete triggers a fallback. The src attribute may be omitted if the [src] attribute exists.
|
filter-expr (optional) | Required if filter==custom |
filter-value (optional) | If data is an array of JsonObjects, the filter-value is the property name that will be accessed for client side filtering. This attribute is unnecessary if filter is none. Defaults to "value". |
min-characters (optional) | The min character length of a user input to provide results, default 1 |
max-entries (optional) | The max specified number of items to suggest at once based on a user input, displays all if unspecified |
suggest-first (optional) |
Suggest the first entry in the list of results by marking it active; only possible if filter==prefix (does nothing otherwise)
|
submit-on-enter (optional) |
The enter key is primarily used for selecting suggestions in autocomplete, so it shouldn’t also submit the form unless the developer explicitly sets it to do so (for search fields/one field forms, et cetera).
The user flow is as follows: If submit-on-enter is true , pressing Enter will select any currently active item and engage in default behavior, including submitting the form if applicable. If submit-on-enter is false , pressing Enter while suggestions are displaying will select any currently active item only and prevent any other default behavior. If suggestions are not displaying, autocomplete allows default behavior. Defaults to false.
|
highlight-user-entry (optional) | If present, exposes the autocomplete-partial class on the substring within the suggested item that resulted in its match with the user input. This can be used to stylize the corresponding match to stand out to the user. Defaults to false.
|
items (optional) | Specifies the key to the data array within the JSON response. Nested keys can be expressed with a dot-notated value such as field1.field2. The default value is "items" . The following are examples with and without usage:
In the first example, the JSON payload is queued by the "items" key, and thus no component attribute is needed because the default value corresponds. In the second example, the JSON payload is queued by the "fruit" key, so the items attribute is given the value "fruit" so as to accurately etrieve the intended datasource. In both examples, the end user interaction is the same.
|
Events
Events may trigger actions on other AMP components using the on
attribute.
e.g. on="select: my-tab.show"
Read more about AMP Actions and Events.
select | amp-autocomplete triggers the select event when the user selects an option via click, tap, keyboard navigation or accepting typeahead. It also fires the select event if a user keyboard navigates to an item and Tabs away from the input field.
event contains the value attribute value of the selected element. |
You've read this document a dozen times but it doesn't really cover all of your questions? Maybe other people felt the same: reach out to them on Stack Overflow.
Go to Stack Overflow Found a bug or missing a feature?The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.
Go to GitHub