diff --git a/app/javascript/controllers/evaluation_criteria_controller.js b/app/javascript/controllers/evaluation_criteria_controller.js index 1a6c08d1..2412069a 100644 --- a/app/javascript/controllers/evaluation_criteria_controller.js +++ b/app/javascript/controllers/evaluation_criteria_controller.js @@ -16,6 +16,9 @@ export default class extends Controller { this.replacePlaceholders(newCriteria); this.enableInputs(newCriteria); + this.collapseAllCriteria(); + this.expandCriterion(newCriteria); + this.criteriaListTarget.appendChild(newCriteria); this.updateCriteriaTitles(); @@ -40,6 +43,26 @@ export default class extends Controller { this.updateCriteriaTitles(); } + collapseAllCriteria() { + const accordionButtons = this.element.querySelectorAll( + ".usa-accordion__button" + ); + const accordions = this.element.querySelectorAll(".usa-accordion__content"); + + accordionButtons.forEach((button) => + button.setAttribute("aria-expanded", false) + ); + accordions.forEach((content) => content.setAttribute("hidden", "")); + } + + expandCriterion(criterion) { + const accordionButton = criterion.querySelector(".usa-accordion__button"); + const accordionContent = criterion.querySelector(".usa-accordion__content"); + + if (accordionButton) accordionButton.setAttribute("aria-expanded", true); + if (accordionContent) accordionContent.removeAttribute("hidden"); + } + toggleScoringType(event) { const row = event.target.closest(".criteria-row"); const scoringType = row.querySelector(".scoring-type-radio:checked").value;