From f86e737f030c797e90b84df481a8d24703207f06 Mon Sep 17 00:00:00 2001 From: Adam Goldstone <13471320+agoldstone93@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:15:29 +0000 Subject: [PATCH] Refactor javascript Update javascript code so accessible autocomplete component is reusable --- app/javascript/src/accessible-autocomplete.js | 43 ++++++++++--------- .../non_uk_home_addresses/show.html.erb | 7 ++- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/app/javascript/src/accessible-autocomplete.js b/app/javascript/src/accessible-autocomplete.js index 5fe58616b8..adeecd69eb 100644 --- a/app/javascript/src/accessible-autocomplete.js +++ b/app/javascript/src/accessible-autocomplete.js @@ -1,48 +1,51 @@ import accessibleAutocomplete from 'accessible-autocomplete' const screenReaderMessageDelay = 1000 // wait before updating the screen reader message, to avoid interrupting queue -const searchSelectItem = document.querySelector('.country-select') -function enhanceSelectElement (searchSelectItem) { +function enhanceSelectElement (autocompleteComponent) { + const searchSelectItem = autocompleteComponent.querySelector('.country-select') + const clearSearchButton = autocompleteComponent.querySelector('.clear-search') + const selectName = searchSelectItem.getAttribute('name') + accessibleAutocomplete.enhanceSelectElement({ defaultValue: '', selectElement: searchSelectItem, inputClasses: 'govuk-input', - name: 'country' + name: selectName }) - addSearchInputListeners() + addSearchInputListeners(autocompleteComponent, clearSearchButton) - if (document.querySelector('#non-uk-home-address-country-code-error')) { - addErrorClasses() + if (autocompleteComponent.querySelector('.govuk-error-message')) { + addErrorClasses(autocompleteComponent, clearSearchButton) } } -function addSearchInputListeners () { - const searchInputBox = document.querySelector('.autocomplete__input') - - document.querySelector('.clear-search').classList.remove('hidden') +function addSearchInputListeners (autocompleteComponent, clearSearchButton) { + const searchInputBox = autocompleteComponent.querySelector('.autocomplete__input') + clearSearchButton.classList.remove('hidden') - document - .querySelector('#clear-country-search') + clearSearchButton .addEventListener('click', (event) => { event.preventDefault() searchInputBox.value = '' - clearSearch() - setTimeout(() => { document.querySelector('#screen-reader-messages').innerHTML = 'Search box has been cleared.' }, screenReaderMessageDelay) + clearSearch(autocompleteComponent) + setTimeout(() => { autocompleteComponent.querySelector('.screen-reader-messages').innerHTML = 'Search box has been cleared.' }, screenReaderMessageDelay) }) } -function clearSearch () { - const selectedItem = document.querySelector('.country-select option[selected]') +function clearSearch (autocompleteComponent) { + const selectedItem = autocompleteComponent.querySelector('.country-select option[selected]') if (selectedItem) selectedItem.removeAttribute('selected') } -function addErrorClasses () { - document.querySelector('.autocomplete__input').classList.add('govuk-input--error') - document.querySelector('.clear-search').classList.add('clear-search-error') +function addErrorClasses (autocompleteComponent, clearSearchButton) { + autocompleteComponent.querySelector('.autocomplete__input').classList.add('govuk-input--error') + clearSearchButton.classList.add('clear-search-error') } document.addEventListener('DOMContentLoaded', () => { - if (searchSelectItem) enhanceSelectElement(searchSelectItem) + const autocompleteComponent = document.querySelector('.autocomplete-component') + + if (autocompleteComponent) enhanceSelectElement(autocompleteComponent) }) diff --git a/app/views/providers/home_address/non_uk_home_addresses/show.html.erb b/app/views/providers/home_address/non_uk_home_addresses/show.html.erb index 51ac6ff31a..fc7c3ea515 100644 --- a/app/views/providers/home_address/non_uk_home_addresses/show.html.erb +++ b/app/views/providers/home_address/non_uk_home_addresses/show.html.erb @@ -11,11 +11,10 @@ form:, ) do %> - - <%= hidden_field_tag "legal-framework-api-host", nil, { "data-uri": Rails.configuration.x.legal_framework_api_host_for_js } %> -
+
+
<%= form.govuk_collection_select :country_code, @form.countries, @@ -26,7 +25,7 @@ label: { text: "Country", size: "m" } %>