Skip to content

Commit eb6bc2d

Browse files
committed
Move ID of the content section to AccordionSection
This is a stable piece of information, so it makes sense that it's part of the `AccordionSection`'s property. It currently requires `getIdentifier` to remain within `Accordion`, but hopefully not for long, as the aim would be for the methods that use `getIdentifier` to use APIs from `AccordionSection` rather than access the DOM.
1 parent 03beeb0 commit eb6bc2d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

packages/govuk-frontend/src/govuk/components/accordion/accordion-section.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ export class AccordionSection extends Component {
1414

1515
/**
1616
* @param {Element} $root - The root of the section
17+
* @param {{accordionId: string, index: number}} config - Configuration for the section
1718
*/
18-
constructor($root) {
19+
constructor($root, config) {
1920
super($root)
21+
// We do not need all the cruft of ConfigurableComponent as there's no defaults
22+
// or need to read config from the root's HTML attributes
23+
// If anything we actually don't need to store the config at all during the section's lifetime
24+
// only the contentId
25+
this.contentId = `${config.accordionId}-content-${config.index + 1}`
2026

2127
this.$header = this.findRequiredElement(
2228
`.govuk-accordion__section-header`,
@@ -34,6 +40,7 @@ export class AccordionSection extends Component {
3440
// However, I'm not sure we originally intended to check whether the content element
3541
// is present every time we toggle a section.
3642
// If that's the case, we can always wrap this in a setter
43+
// TODO: Update to use the ID computed for the `aria-controls` of the `<button>`
3744
this.$content = this.findRequiredElement(
3845
`.govuk-accordion__section-content`,
3946
`Section content (\`<div class="govuk-accordion__section-content">\`)`

packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,16 @@ export class Accordion extends ConfigurableComponent {
141141
* @private
142142
*/
143143
initSectionHeaders() {
144-
this.$sections.forEach(($section, i) => {
145-
const section = new AccordionSection($section)
144+
this.$sections.forEach(($section, index) => {
145+
const section = new AccordionSection($section, {
146+
accordionId: this.$root.id,
147+
index
148+
})
146149
// Cache the AccordionSection for future retrieval
147150
this.sections.set($section, section)
148151

149152
// Set header attributes
150-
this.constructHeaderMarkup(section, i)
153+
this.constructHeaderMarkup(section, index)
151154
this.setExpanded(this.isExpanded($section), $section)
152155

153156
// Handle events
@@ -375,9 +378,10 @@ export class Accordion extends ConfigurableComponent {
375378
* @returns {string | undefined | null} Identifier for section
376379
*/
377380
getIdentifier($section) {
378-
const $button = $section.querySelector(`.${sectionButtonClass}`)
379-
380-
return $button?.getAttribute('aria-controls')
381+
// TODO: Temporary, this should be lifted once the accordion
382+
// all section related features as within `AccordionSection`
383+
const section = this.sections.get($section)
384+
return section?.contentId
381385
}
382386

383387
/**

0 commit comments

Comments
 (0)