Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 08b753f

Browse files
authored
Merge pull request #15 from alphagov/trigger-remove-topic-event
Trigger remove topic event
2 parents b6729f0 + 6aed897 commit 08b753f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ function nodesToArray(nodes: NodeList<HTMLElement> | HTMLCollection<HTMLElement>
44
return Array.prototype.slice.call(nodes)
55
}
66

7+
function triggerEvent(element: HTMLElement, eventName: string, detail: Object) {
8+
const params = {bubbles: true, cancelable: true, detail: detail || null}
9+
let event
10+
11+
if (typeof window.CustomEvent === 'function') {
12+
event = new window.CustomEvent(eventName, params)
13+
} else {
14+
event = document.createEvent('CustomEvent')
15+
event.initCustomEvent(eventName, params.bubbles, params.cancelable, params.detail)
16+
}
17+
18+
element.dispatchEvent(event)
19+
}
20+
721
/**
822
* This models the taxonomy shown in the miller columns and the current state
923
* of it.
@@ -577,6 +591,7 @@ class MillerColumnsSelectedElement extends HTMLElement {
577591
button.textContent = 'Remove topic'
578592
button.setAttribute('type', 'button')
579593
button.addEventListener('click', () => {
594+
triggerEvent(button, 'remove-topic', topic)
580595
if (this.taxonomy) {
581596
this.taxonomy.removeTopic(topic)
582597
}

test/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ describe('miller-columns', function() {
155155

156156
const selectedItems = document.querySelector('#selected-items')
157157
assert.equal(selectedItems.textContent, 'No selected topics')
158+
159+
const millerColumnsSelected = document.querySelector('miller-columns-selected')
160+
millerColumnsSelected.addEventListener('remove-topic', function(e) {
161+
assert.equal(e.detail.topicName, "Parenting, childcare and children's services")
162+
})
158163
})
159164

160165
it('creates entries of selected item for adjacent topics', function() {

0 commit comments

Comments
 (0)