-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autocomplete stimulus controller
This is based on the implementation in the prototype and customises the accessible-autocomplete with some of the available options.
- Loading branch information
Showing
9 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Ensure the autocomplete uses the correct typeface | ||
.autocomplete__wrapper { | ||
font-family: $govuk-font-family; | ||
} | ||
|
||
.autocomplete__input { | ||
font-family: inherit; | ||
} | ||
|
||
// Style the autocomplete if there’s an error | ||
.nhsuk-form-group--error { | ||
.autocomplete__input { | ||
border-color: $govuk-error-colour; | ||
} | ||
|
||
.autocomplete__input--focused { | ||
border-color: $govuk-input-border-colour; | ||
} | ||
} | ||
|
||
.autocomplete__dropdown-arrow-down { | ||
// Ensure dropdown arrow can be clicked | ||
// https://github.com/alphagov/accessible-autocomplete/issues/202 | ||
pointer-events: none; | ||
// Ensure dropdown arrow can be seen | ||
z-index: 0; | ||
} | ||
|
||
.autocomplete__input { | ||
background-color: $color_nhsuk-white; | ||
} | ||
|
||
.autocomplete__option { | ||
margin-bottom: 0; | ||
} | ||
|
||
.autocomplete__option-hint { | ||
color: $nhsuk-secondary-text-color; | ||
|
||
.autocomplete__option:hover &, | ||
.autocomplete__option:focus & { | ||
color: $color_nhsuk-grey-5; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
import accessibleAutocomplete from "accessible-autocomplete"; | ||
|
||
const enhanceOption = (option) => { | ||
return { | ||
name: option.label, | ||
append: option.getAttribute("data-append"), | ||
hint: option.getAttribute("data-hint"), | ||
}; | ||
}; | ||
|
||
const suggestion = (value, options) => { | ||
const option = options.find(({ name }) => name === value); | ||
if (option) { | ||
const html = option.append ? `${value} – ${option.append}` : value; | ||
return option.hint | ||
? `${html}<br><span class="autocomplete__option-hint">${option.hint}</span>` | ||
: html; | ||
} else { | ||
return "No results found"; | ||
} | ||
}; | ||
|
||
const autocomplete = ($module) => { | ||
if (!$module) { | ||
return; | ||
} | ||
|
||
const params = $module.dataset; | ||
|
||
const selectOptions = Array.from($module.options); | ||
const options = selectOptions.map((option) => enhanceOption(option)); | ||
|
||
accessibleAutocomplete.enhanceSelectElement({ | ||
autoselect: params.autoselect === "true", | ||
defaultValue: params.defaultValue || "", | ||
displayMenu: params.displayMenu, | ||
minLength: params.minLength ? parseInt(params.minLength) : 0, | ||
selectElement: $module, | ||
showAllValues: params.showAllValues === "true", | ||
showNoOptionsFound: params.showNoOptionsFound === "true", | ||
templates: { | ||
suggestion: (value) => suggestion(value, options), | ||
}, | ||
onConfirm: (value) => { | ||
const selectedOption = [].filter.call( | ||
selectOptions, | ||
(option) => (option.textContent || option.innerText) === value, | ||
)[0]; | ||
if (selectedOption) selectedOption.selected = true; | ||
}, | ||
}); | ||
}; | ||
|
||
// Connects to data-module="autocomplete" | ||
export default class extends Controller { | ||
connect() { | ||
autocomplete(this.element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters