Skip to content

Commit 56ba966

Browse files
committed
Refactor finding required elements into its own method
It encapsulates looking for an element using `querySelectorAll` and throwing if the element is not within the root of the component. Ultimately, this could move to the base component, but this would be the object of a separate commit.
1 parent 599f1e3 commit 56ba966

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,27 @@ export class AccordionSection extends Component {
1818
constructor($root) {
1919
super($root)
2020

21-
this.$header = this.$root.querySelector(`.govuk-accordion__section-header`)
22-
if (!this.$header) {
23-
throw new ElementError({
24-
component: AccordionSection,
25-
identifier: `Section headers (\`<div class="govuk-accordion__section-header">\`)`
26-
})
27-
}
21+
this.$header = this.findRequiredElement(
22+
`.govuk-accordion__section-header`,
23+
`Section headers (\`<div class="govuk-accordion__section-header">\`)`
24+
)
2825

29-
this.$buttonPlaceholder = this.$header.querySelector(
30-
`.govuk-accordion__section-button`
26+
this.$buttonPlaceholder = this.findRequiredElement(
27+
'.govuk-accordion__section-button',
28+
'Section button placeholder (`<span class="govuk-accordion__section-button">`)'
3129
)
32-
if (!this.$buttonPlaceholder) {
33-
throw new ElementError({
34-
component: AccordionSection,
35-
identifier: `Section button placeholder (\`<span class="govuk-accordion__section-button">\`)`
36-
})
37-
}
3830

3931
this.$headingText = createHeadingText(this.$buttonPlaceholder)
4032

4133
// Technically slightly different from the current implementation
4234
// However, I'm not sure we originally intended to check whether the content element
4335
// is present every time we toggle a section.
4436
// If that's the case, we can always wrap this in a setter
45-
this.$content = this.$root.querySelector(
46-
`.govuk-accordion__section-content`
37+
this.$content = this.findRequiredElement(
38+
`.govuk-accordion__section-content`,
39+
`Section content (\`<div class="govuk-accordion__section-content">\`)`
4740
)
4841

49-
if (!this.$content) {
50-
throw new ElementError({
51-
component: AccordionSection,
52-
identifier: `Section content (\`<div class="govuk-accordion__section-content">\`)`
53-
})
54-
}
55-
5642
this.$toggleIcon = createElement('span', {
5743
class: 'govuk-accordion-nav__chevron'
5844
})
@@ -63,16 +49,33 @@ export class AccordionSection extends Component {
6349

6450
this.$toggle = createShowHideToggle(this.$toggleIcon, this.$toggleText)
6551

66-
this.$heading = this.$root.querySelector(
67-
'.govuk-accordion__section-heading'
52+
this.$heading = this.findRequiredElement(
53+
'.govuk-accordion__section-heading',
54+
'Section heading (`.govuk-accordion__section-heading`)'
6855
)
56+
}
6957

70-
if (!this.$heading) {
58+
/**
59+
* Finds a require element under the component's root
60+
*
61+
* Throws if the element does not exist
62+
*
63+
* @param {string} selector - The selector to use for looking up the element
64+
* @param {string} identifier - A message that'll help user identify the element if it's missing
65+
* @returns {Element} - The element
66+
* @throws {ElementError} - If the element is not present
67+
*/
68+
findRequiredElement(selector, identifier) {
69+
const $element = this.$root.querySelector(selector)
70+
71+
if (!$element) {
7172
throw new ElementError({
7273
component: AccordionSection,
73-
identifier: `Section heading (\`.govuk-accordion__section-heading\`)`
74+
identifier
7475
})
7576
}
77+
78+
return $element
7679
}
7780
}
7881

0 commit comments

Comments
 (0)