diff --git a/app/assets/javascripts/components/expander.js b/app/assets/javascripts/components/expander.js index 6e0cb9440e..a98cffd57b 100644 --- a/app/assets/javascripts/components/expander.js +++ b/app/assets/javascripts/components/expander.js @@ -69,17 +69,17 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; // if this ; is omitted, none $button.setAttribute('aria-expanded', expanded) $button.setAttribute('aria-controls', this.$content.getAttribute('id')) - // GA4 Accordion tracking. Relies on the ga4-finder-tracker setting the index first, so we wrap this in a custom event. - window.addEventListener('ga4-filter-indexes-added', function () { - if (window.GOVUK.analyticsGa4) { - if (window.GOVUK.analyticsGa4.Ga4FinderTracker) { - window.GOVUK.analyticsGa4.Ga4FinderTracker.addFilterButtonTracking($button, this.$toggle.innerHTML) - } + var buttonAttributes = this.$module.getAttribute('data-button-data-attributes') + if (buttonAttributes) { + buttonAttributes = JSON.parse(buttonAttributes) + for (var rawKey in buttonAttributes) { + var key = rawKey.replace(/_/i, '-').toLowerCase() + var rawValue = buttonAttributes[rawKey] + var value = typeof rawValue === 'object' ? JSON.stringify(rawValue) : rawValue + $button.setAttribute('data-' + key, value) } - }.bind(this)) - + } $button.innerHTML = toggleHtml - this.$toggle.parentNode.replaceChild($button, this.$toggle) } diff --git a/app/views/components/_expander.html.erb b/app/views/components/_expander.html.erb index 81f72a2417..1ea33aa8f5 100644 --- a/app/views/components/_expander.html.erb +++ b/app/views/components/_expander.html.erb @@ -12,6 +12,7 @@ component_helper.add_data_attribute({ "open-on-load": open_on_load }) component_helper.add_class("app-c-expander") component_helper.add_class(shared_helper.get_margin_bottom) unless margin_bottom == 0 + component_helper.add_data_attribute({ "button-data-attributes": button_data_attributes }) if local_assigns.include?(:button_data_attributes) %> <% if title %> <%= tag.div(**component_helper.all_attributes) do %> diff --git a/app/views/components/docs/expander.yml b/app/views/components/docs/expander.yml index cef3e760dd..f13078ffaa 100644 --- a/app/views/components/docs/expander.yml +++ b/app/views/components/docs/expander.yml @@ -28,3 +28,43 @@ examples: margin_bottom: 9 block: | This is some content that is passed to the component. It should be distinct from the component, in that the component should not style or interact with it, other than to show and hide it. + with_counter: + description: If there are form elements within the expander it can display a count of the number of form elements with a selected or input value. This is to make it appear the same as the option select component when appearing with it in search pages. Note that if any form elements are selected on page load, the component will expand by default. + data: + title: Things + block: | +