diff --git a/app/javascript/add-another.js b/app/javascript/add-another.js index fbf7df5d6..2537f2b0e 100644 --- a/app/javascript/add-another.js +++ b/app/javascript/add-another.js @@ -153,15 +153,23 @@ const setNumbering = (section, counter) => { } section.querySelectorAll('[data-add-another-dynamic-elements]').forEach((element) => { element.dataset.addAnotherDynamicElements.split(",").forEach((pairString) => { - const parts = pairString.split(":"); - element.setAttribute(parts[0], parts[1].replace("ID", counter + 1)) + const [attribute, valueTemplate] = pairString.split(":"); + const newValue = valueTemplate.replace("ID", counter + 1); + element.setAttribute(attribute, newValue); + + // Handle aria-controls specifically if the attribute is `data-aria-controls` + if (attribute === "data-aria-controls") { + element.setAttribute("aria-controls", newValue); + } }); }) } const setUpRadios = (section) => { - if (section.querySelector('input[type="radio"]')) { + const radioContainer = section.querySelector('[data-module="govuk-radios"]'); + if (radioContainer && !radioContainer.dataset.radiosInitialized) { const radios = new Radios(section); + radioContainer.dataset.radiosInitialized = true; // This is to handle `govuk-frontend` 5.7.0, not allowing us to initialising components twice } } diff --git a/package.json b/package.json index c15f10395..23926939e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "@rails/ujs": "^7.1.402", "@sentry/browser": "8.37.1", "esbuild": "^0.24.0", - "govuk-frontend": "^5.6.0", + "govuk-frontend": "^5.7.1", "jquery": "^3.7.1", "puppeteer": "23.6.0", "rails_admin": "3.2.1", diff --git a/spec/system/add_another_spec.rb b/spec/system/add_another_spec.rb index d530ec704..c80361b0a 100644 --- a/spec/system/add_another_spec.rb +++ b/spec/system/add_another_spec.rb @@ -133,6 +133,43 @@ click_on "remove-2" expect(page).to have_content "Enter the estimated value of vehicle 2" end + + it "lets me remove a vehicle and I can still interact with conditional reveals", :slow do + fill_in "1-vehicle-value", with: "123" + choose "No", name: "vehicle_model[items][1][vehicle_pcp]" + choose "No", name: "vehicle_model[items][1][vehicle_over_3_years_ago]" + choose "No", name: "vehicle_model[items][1][vehicle_in_regular_use]" + click_on "Add another vehicle" + + fill_in "2-vehicle-value", with: "456" + choose "No", name: "vehicle_model[items][2][vehicle_pcp]" + choose "No", name: "vehicle_model[items][2][vehicle_over_3_years_ago]" + choose "No", name: "vehicle_model[items][2][vehicle_in_regular_use]" + click_on "Add another vehicle" + + fill_in "3-vehicle-value", with: "789" + choose "No", name: "vehicle_model[items][3][vehicle_pcp]" + choose "No", name: "vehicle_model[items][3][vehicle_over_3_years_ago]" + choose "No", name: "vehicle_model[items][3][vehicle_in_regular_use]" + + click_on "Save and continue" + + click_on "Back" + + click_on "remove-2" + + fill_in "2-vehicle-value", with: "456" + choose "Yes", name: "vehicle_model[items][2][vehicle_pcp]" + fill_in "2-vehicle-finance", with: "123" + choose "No", name: "vehicle_model[items][2][vehicle_over_3_years_ago]" + choose "No", name: "vehicle_model[items][2][vehicle_in_regular_use]" + click_on "Save and continue" + + click_on "Back" + + expect(find(id: "1-vehicle-value").value).to eq "123" + expect(find(id: "2-vehicle-value").value).to eq "456" + end end describe "additional properties" do diff --git a/yarn.lock b/yarn.lock index 6f7eceb18..efccc17e1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -766,11 +766,16 @@ get-uri@^6.0.1: debug "^4.3.4" fs-extra "^8.1.0" -govuk-frontend@^5.0.0, govuk-frontend@^5.6.0: +govuk-frontend@^5.0.0: version "5.6.0" resolved "https://registry.yarnpkg.com/govuk-frontend/-/govuk-frontend-5.6.0.tgz#8c0975f0d825ec7192bcfe64e3e97ef3dfa7dea1" integrity sha512-yNA4bL7i7mNrg36wPNZ3RctHo9mjl82Phs8MWs1lwovxJuQ4ogEo/XWn2uB1HxkXNqgMlW4wnd0iiKgRMfxYfw== +govuk-frontend@^5.7.1: + version "5.7.1" + resolved "https://registry.yarnpkg.com/govuk-frontend/-/govuk-frontend-5.7.1.tgz#d4c561ebf8c0b76130f31df8c2e4d70d340cd63f" + integrity sha512-jF1cq5rn57kxZmJRprUZhTQ31zaBBK4b5AyeJaPX3Yhg22lk90Mx/dQLvOk/ycV3wM7e0y+s4IPvb2fFaPlCGg== + graceful-fs@^4.1.6, graceful-fs@^4.2.0: version "4.2.11" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3"