Skip to content

Commit dc003e6

Browse files
committed
Move access to section content to AccordionSection
1 parent 87622f8 commit dc003e6

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ export class AccordionSection extends Component {
2626
})
2727
}
2828

29+
// Technically slightly different from the current implementation
30+
// However, I'm not sure we originally intended to check whether the content element
31+
// is present every time we toggle a section.
32+
// If that's the case, we can always wrap this in a setter
33+
this.$content = this.$root.querySelector(
34+
`.govuk-accordion__section-content`
35+
)
36+
37+
if (!this.$content) {
38+
throw new ElementError({
39+
component: AccordionSection,
40+
identifier: `Section content (\`<div class="govuk-accordion__section-content">\`)`
41+
})
42+
}
43+
2944
this.$toggleIcon = createElement('span', {
3045
class: 'govuk-accordion-nav__chevron'
3146
})

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ const iconOpenModifier = 'govuk-accordion-nav__chevron--down'
2929
/** @private */
3030
const sectionSummaryClass = 'govuk-accordion__section-summary'
3131

32-
/** @private */
33-
const sectionContentClass = 'govuk-accordion__section-content'
34-
3532
/**
3633
* Accordion component
3734
*
@@ -373,14 +370,7 @@ export class Accordion extends ConfigurableComponent {
373370
const $showHideIcon = section.$toggleIcon
374371
const $showHideText = section.$toggleText
375372
const $button = $section.querySelector(`.${sectionButtonClass}`)
376-
const $content = $section.querySelector(`.${sectionContentClass}`)
377-
378-
if (!$content) {
379-
throw new ElementError({
380-
component: Accordion,
381-
identifier: `Section content (\`<div class="${sectionContentClass}">\`)`
382-
})
383-
}
373+
const $content = section.$content
384374

385375
if (!$button) {
386376
// Return early for elements we create

packages/govuk-frontend/src/govuk/components/accordion/accordion.puppeteer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ describe('/components/accordion', () => {
855855
cause: {
856856
name: 'ElementError',
857857
message:
858-
'govuk-accordion: Section content (`<div class="govuk-accordion__section-content">`) not found'
858+
'govuk-accordion-section: Section content (`<div class="govuk-accordion__section-content">`) not found'
859859
}
860860
})
861861
})

0 commit comments

Comments
 (0)