From ed0d43adde190423c4b2cb0a1a7e5b6fd07a0bd7 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 | 25 ++++++++++--------- .../non_uk_home_addresses/show.html.erb | 7 +++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/javascript/src/accessible-autocomplete.js b/app/javascript/src/accessible-autocomplete.js index 5fe58616b8..be71ac7474 100644 --- a/app/javascript/src/accessible-autocomplete.js +++ b/app/javascript/src/accessible-autocomplete.js @@ -1,46 +1,47 @@ 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') +const autocompleteComponent = document.querySelector('.autocomplete-component') +const searchSelectItem = autocompleteComponent.querySelector('.country-select') +const selectName = searchSelectItem.getAttribute('name') +const clearSearchButton = autocompleteComponent.querySelector('.clear-search') +const searchInputBox = autocompleteComponent.querySelector('.autocomplete__input') function enhanceSelectElement (searchSelectItem) { accessibleAutocomplete.enhanceSelectElement({ defaultValue: '', selectElement: searchSelectItem, inputClasses: 'govuk-input', - name: 'country' + name: selectName }) addSearchInputListeners() - if (document.querySelector('#non-uk-home-address-country-code-error')) { + if (autocompleteComponent.querySelector('.govuk-error-message')) { addErrorClasses() } } function addSearchInputListeners () { - const searchInputBox = document.querySelector('.autocomplete__input') + clearSearchButton.classList.remove('hidden') - document.querySelector('.clear-search').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) + setTimeout(() => { autocompleteComponent.querySelector('.screen-reader-messages').innerHTML = 'Search box has been cleared.' }, screenReaderMessageDelay) }) } function clearSearch () { - const selectedItem = document.querySelector('.country-select option[selected]') + 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') + autocompleteComponent.querySelector('.autocomplete__input').classList.add('govuk-input--error') + clearSearchButton.classList.add('clear-search-error') } document.addEventListener('DOMContentLoaded', () => { 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" } %>