Autocomplete
Help users find and select from a number of options.
Contents
<div class="govuk-form-group">
<h1 class="govuk-label-wrapper">
<label class="govuk-label govuk-label--l" for="country">
Pick a country
</label>
</h1>
<select class="govuk-select" id="country" name="country"
data-module="autocomplete" data-autoselect="" data-display-menu="" data-min-length="" data-show-all-values="" data-show-no-options-found="">
<option value="" selected disabled>Select an option</option>
<option value="Austria">Austria</option>
<option value="Belgium">Belgium</option>
<option value="Bulgaria">Bulgaria</option>
<option value="Croatia">Croatia</option>
<option value="Republic of Cyprus">Republic of Cyprus</option>
<option value="Czech Republic">Czech Republic</option>
<option value="Denmark">Denmark</option>
<option value="Estonia">Estonia</option>
<option value="Finland">Finland</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Greece">Greece</option>
<option value="Hungary">Hungary</option>
<option value="Ireland">Ireland</option>
<option value="Italy">Italy</option>
<option value="Latvia">Latvia</option>
<option value="Lithuania">Lithuania</option>
<option value="Luxembourg">Luxembourg</option>
<option value="Malta">Malta</option>
<option value="Netherlands">Netherlands</option>
<option value="Poland">Poland</option>
<option value="Portugal">Portugal</option>
<option value="Romania">Romania</option>
<option value="Slovakia">Slovakia</option>
<option value="Slovenia">Slovenia</option>
<option value="Spain">Spain</option>
<option value="Sweden">Sweden</option>
</select>
</div>
{{ xGovukAutocomplete({
id: "country",
name: "country",
allowEmpty: false,
label: {
classes: "govuk-label--l",
isPageHeading: true,
text: "Pick a country"
},
items: [
{ text: "Austria" },
{ text: "Belgium" },
{ text: "Bulgaria" },
{ text: "Croatia" },
{ text: "Republic of Cyprus" },
{ text: "Czech Republic" },
{ text: "Denmark" },
{ text: "Estonia" },
{ text: "Finland" },
{ text: "France" },
{ text: "Germany" },
{ text: "Greece" },
{ text: "Hungary" },
{ text: "Ireland" },
{ text: "Italy" },
{ text: "Latvia" },
{ text: "Lithuania" },
{ text: "Luxembourg" },
{ text: "Malta" },
{ text: "Netherlands" },
{ text: "Poland" },
{ text: "Portugal" },
{ text: "Romania" },
{ text: "Slovakia" },
{ text: "Slovenia" },
{ text: "Spain" },
{ text: "Sweden" }
]
}) }}
This component implements the Accessible autocomplete pattern to enhance a fixed list of options provided by a <select>
element.
Unlike an autosuggest component, this component will only allow users to choose from a predetermined list of options.
Nunjucks macro options
Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.
Some options are required for the macro to work; these are marked as “Required” in the option description.
If you’re using Nunjucks macros in production with html
options, or ones ending with html
, you must sanitise the HTML to protect against cross-site scripting exploits.
Name | Type | Description |
---|---|---|
id | string | Required. ID for each autocomplete. |
name | string | Required. Name property for the autocomplete. |
items | Array | Required. Array of option items for the autocomplete. See items. |
value | string | Value for the option which should be selected. Use this as an alternative to setting the |
allowEmpty | boolean | Whether to allow no answer to be given (default is |
autoselect | boolean | Whether to highlight the first option when the user types in something and receives results. Pressing enter will select it (default is |
displayMenu | string | Specify the way the menu should appear, whether |
minLength | integer | The minimum number of characters that should be entered before the autocomplete will attempt to suggest options. When the query length is under this, the aria status region will also provide helpful text to the user informing them they should type in more (default is |
showAllValues | boolean | Whether all values are shown when the user clicks the input. This is similar to a default select menu, so the autocomplete is rendered with a dropdown arrow to convey this behaviour (default is |
showNoOptionsFound | boolean | Whether to display a ‘No results found’ message when there are no results (default is |
describedBy | string | One or more element IDs to add to the |
label | object | Label text or HTML by specifying value for either text or html keys. See label. |
hint | object | Options for the hint component. See hint. |
errorMessage | object | Options for the error message component. The error message component will not display if you use a falsy value for |
formGroup | object | Options for the form-group wrapper. See formGroup. |
classes | string | Classes to add to the autocomplete container. |
attributes | object | HTML attributes (for example data attributes) to add to the autocomplete container. |
Name | Type | Description |
---|---|---|
text | string | Required. Text for the option item. |
value | string | Value for the option item (default is an empty string). |
selected | boolean | Sets the option as selected. |
disabled | boolean | Sets the option item as disabled. |
attributes | object | HTML attributes (for example data attributes) to add to the option. |
Name | Type | Description |
---|---|---|
text | string | Required. If |
html | string | Required. If |
for | string | The value of the |
isPageHeading | boolean | Whether the label also acts as the heading for the page. |
classes | string | Classes to add to the label tag. |
attributes | object | HTML attributes (for example data attributes) to add to the label tag. |
Name | Type | Description |
---|---|---|
text | string | Required. If |
html | string | Required. If |
id | string | ID attribute to add to the hint span tag. |
classes | string | Classes to add to the hint span tag. |
attributes | object | HTML attributes (for example data attributes) to add to the hint span tag. |
Name | Type | Description |
---|---|---|
classes | string | Classes to add to the form group (for example to show error state for the whole group). |