Skip to content

Commit 791276d

Browse files
Remove component init() functions
1 parent 928dc21 commit 791276d

File tree

4 files changed

+43
-50
lines changed

4 files changed

+43
-50
lines changed

packages/components/button/button.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
const KEY_SPACE = 32
2+
const DEBOUNCE_TIMEOUT_IN_SECONDS = 1
3+
14
class Button {
25
constructor($module) {
3-
this.KEY_SPACE = 32
4-
this.DEBOUNCE_TIMEOUT_IN_SECONDS = 1
6+
if (!$module) {
7+
return this
8+
}
59

610
this.$module = $module
711
this.debounceFormSubmitTimer = null
12+
13+
/**
14+
* Initialise an event listener for keydown at document level
15+
* this will help listening for later inserted elements with a role="button"
16+
*/
17+
this.$module.addEventListener('keydown', this.handleKeyDown.bind(this))
18+
this.$module.addEventListener('click', this.debounce.bind(this))
819
}
920

1021
/**
@@ -23,7 +34,7 @@ class Button {
2334
// if the element has a role='button' and the pressed key is a space, we'll simulate a click
2435
if (
2536
target.getAttribute('role') === 'button' &&
26-
event.keyCode === this.KEY_SPACE
37+
event.keyCode === KEY_SPACE
2738
) {
2839
event.preventDefault()
2940
// trigger the target's click event
@@ -51,22 +62,13 @@ class Button {
5162

5263
this.debounceFormSubmitTimer = setTimeout(() => {
5364
this.debounceFormSubmitTimer = null
54-
}, this.DEBOUNCE_TIMEOUT_IN_SECONDS * 1000)
55-
}
56-
57-
/**
58-
* Initialise an event listener for keydown at document level
59-
* this will help listening for later inserted elements with a role="button"
60-
*/
61-
init() {
62-
this.$module.addEventListener('keydown', this.handleKeyDown.bind(this))
63-
this.$module.addEventListener('click', this.debounce.bind(this))
65+
}, DEBOUNCE_TIMEOUT_IN_SECONDS * 1000)
6466
}
6567
}
6668

6769
module.exports = ({ scope = document } = {}) => {
6870
const buttons = scope.querySelectorAll('[data-module="nhsuk-button"]')
6971
buttons.forEach((el) => {
70-
new Button(el).init()
72+
new Button(el)
7173
})
7274
}

packages/components/character-count/character-count.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
class CharacterCount {
22
constructor($module) {
3+
if (!$module) {
4+
return this
5+
}
6+
7+
const $textarea = $module.querySelector('.nhsuk-js-character-count')
8+
if (!$textarea) {
9+
return this
10+
}
11+
312
this.$module = $module
4-
this.$textarea = $module.querySelector('.nhsuk-js-character-count')
13+
this.$textarea = $textarea
514
this.$visibleCountMessage = null
615
this.$screenReaderCountMessage = null
716
this.lastInputTimestamp = null
8-
}
9-
10-
// Initialise component
11-
init() {
12-
// Check that required elements are present
13-
if (!this.$textarea) {
14-
return
15-
}
1617

1718
// Check for module
18-
const { $module } = this
19-
const { $textarea } = this
2019
const $fallbackLimitMessage = document.getElementById(
21-
`${$textarea.id}-info`
20+
`${this.$textarea.id}-info`
2221
)
2322

2423
// Move the fallback count message to be immediately after the textarea
2524
// Kept for backwards compatibility
26-
$textarea.insertAdjacentElement('afterend', $fallbackLimitMessage)
25+
this.$textarea.insertAdjacentElement('afterend', $fallbackLimitMessage)
2726

2827
// Create the *screen reader* specific live-updating counter
2928
// This doesn't need any styling classes, as it is never visible
@@ -53,7 +52,7 @@ class CharacterCount {
5352
$fallbackLimitMessage.classList.add('nhsuk-u-visually-hidden')
5453

5554
// Read options set using dataset ('data-' values)
56-
this.options = CharacterCount.getDataset($module)
55+
this.options = CharacterCount.getDataset(this.$module)
5756

5857
// Determine the limit attribute (characters or words)
5958
let countAttribute = this.defaults.characterCountAttribute
@@ -62,15 +61,15 @@ class CharacterCount {
6261
}
6362

6463
// Save the element limit
65-
this.maxLength = $module.getAttribute(countAttribute)
64+
this.maxLength = this.$module.getAttribute(countAttribute)
6665

6766
// Check for limit
6867
if (!this.maxLength) {
6968
return
7069
}
7170

7271
// Remove hard limit if set
73-
$textarea.removeAttribute('maxlength')
72+
this.$textarea.removeAttribute('maxlength')
7473

7574
this.bindChangeEvents()
7675

@@ -273,6 +272,6 @@ module.exports = ({ scope = document } = {}) => {
273272
'[data-module="nhsuk-character-count"]'
274273
)
275274
characterCounts.forEach((el) => {
276-
new CharacterCount(el).init()
275+
new CharacterCount(el)
277276
})
278277
}

packages/components/header/header.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,41 @@
55

66
class Header {
77
constructor() {
8-
this.menuIsEnabled = false
9-
this.menuIsOpen = false
10-
11-
this.navigation = document.querySelector('.nhsuk-navigation')
12-
this.navigationList = null
13-
this.navigationItems = null
14-
15-
if (!this.navigation) {
8+
const $navigation = document.querySelector('.nhsuk-navigation')
9+
if (!$navigation) {
1610
return
1711
}
1812

13+
this.navigation = $navigation
14+
1915
this.navigationList = this.navigation.querySelector(
2016
'.nhsuk-header__navigation-list'
2117
)
2218
this.navigationItems = this.navigation.querySelectorAll(
2319
'.nhsuk-header__navigation-item'
2420
)
2521

26-
this.mobileMenu = document.createElement('ul')
2722
this.mobileMenuToggleButton = document.querySelector(
2823
'.nhsuk-header__menu-toggle'
2924
)
3025
this.mobileMenuContainer = document.querySelector(
3126
'.nhsuk-mobile-menu-container'
3227
)
3328

34-
this.width = 0
35-
}
36-
37-
init() {
3829
if (
39-
!this.navigation ||
4030
!this.navigationList ||
4131
!this.navigationItems ||
4232
!this.navigationItems.length ||
4333
!this.mobileMenuToggleButton ||
4434
!this.mobileMenuContainer
4535
) {
46-
return
36+
return this
4737
}
4838

39+
this.mobileMenu = document.createElement('ul')
40+
this.menuIsEnabled = false
41+
this.menuIsOpen = false
42+
4943
this.handleEscapeKey = this.onEscapeKey.bind(this)
5044
this.handleUpdateNavigation = this.debounce(this.updateNavigation)
5145
this.handleToggleMobileMenu = this.toggleMobileMenu.bind(this)
@@ -280,5 +274,5 @@ class Header {
280274
}
281275

282276
module.exports = () => {
283-
new Header().init()
277+
new Header()
284278
}

packages/components/tabs/tabs.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class Tabs {
1616

1717
this.showEvent = new CustomEvent('tab.show')
1818
this.hideEvent = new CustomEvent('tab.hide')
19-
}
2019

21-
init() {
2220
if (typeof window.matchMedia === 'function' && this.responsive) {
2321
this.setupResponsiveChecks()
2422
} else {
@@ -340,6 +338,6 @@ module.exports = ({
340338
} = {}) => {
341339
const tabs = scope.querySelectorAll(`[data-module="${namespace}"]`)
342340
tabs.forEach((el) => {
343-
new Tabs(el, namespace, responsive, historyEnabled).init()
341+
new Tabs(el, namespace, responsive, historyEnabled)
344342
})
345343
}

0 commit comments

Comments
 (0)