Skip to content

Commit d867225

Browse files
authored
Merge pull request #7 from github/clicky
Fix a[href] as option not navigating
2 parents 1f287ba + 0f9f915 commit d867225

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

combobox-nav.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ function commitWithElement(event: MouseEvent) {
6363
if (!(event.target instanceof Element)) return
6464
const target = event.target.closest('[role="option"]')
6565
if (!target) return
66-
event.preventDefault()
6766
if (target.getAttribute('aria-disabled') === 'true') return
6867
fireCommitEvent(target)
6968
}
@@ -72,7 +71,7 @@ function commit(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement
7271
const target = list.querySelector('[aria-selected="true"]')
7372
if (!target) return false
7473
if (target.getAttribute('aria-disabled') === 'true') return true
75-
fireCommitEvent(target)
74+
target.click()
7675
return true
7776
}
7877

examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<li id="bb-8" role="option" aria-disabled="true"><del>BB-8</del></li>
1919
<li id="hubot" role="option">Hubot</li>
2020
<li id="r2-d2" role="option">R2-D2</li>
21+
<li id="wall-e"><a href="#wall-e" role="option">Wall-E</a></li>
2122
</ul>
2223
</form>
2324
<pre class="events"></pre>

test/test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function press(input, key, ctrlKey) {
33
}
44

55
function click(element) {
6-
element.dispatchEvent(new MouseEvent('click', {bubbles: true}))
6+
element.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true}))
77
}
88

99
describe('combobox-nav', function() {
@@ -51,6 +51,7 @@ describe('combobox-nav', function() {
5151
<li id="hubot" role="option">Hubot</li>
5252
<li id="r2-d2" role="option">R2-D2</li>
5353
<li id="wall-e" role="option" aria-disabled="true">Wall-E</li>
54+
<li><a href="#wall-e" role="option">Wall-E</a></li>
5455
</ul>
5556
`
5657
comboboxNav.install(document.querySelector('input'), document.querySelector('ul'))
@@ -120,5 +121,16 @@ describe('combobox-nav', function() {
120121
assert.equal(expectedTargets[0], 'hubot')
121122
assert.equal(expectedTargets[1], 'baymax')
122123
})
124+
125+
it('fires event and follows the link on click', function() {
126+
let eventFired = false
127+
document.addEventListener('combobox-commit', function() {
128+
eventFired = true
129+
})
130+
131+
click(document.querySelectorAll('[role=option]')[4])
132+
assert(eventFired)
133+
assert.equal(window.location.hash, '#wall-e')
134+
})
123135
})
124136
})

0 commit comments

Comments
 (0)