Skip to content

Commit 2965e68

Browse files
authored
Merge pull request #5 from github/fix-disable-click
Disabled option behavior fix
2 parents 235b3ec + aef3213 commit 2965e68

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

combobox-nav.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ function commitWithElement(event: MouseEvent) {
6363
if (!(event.target instanceof Element)) return
6464
const target = event.target.closest('[role="option"]')
6565
if (!target) return
66-
fireCommitEvent(target)
6766
event.preventDefault()
67+
if (target.getAttribute('aria-disabled') === 'true') return
68+
fireCommitEvent(target)
6869
}
6970

7071
function commit(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): boolean {
7172
const target = list.querySelector('[aria-selected="true"]')
72-
if (!target || target.getAttribute('aria-disabled') === 'true') return false
73+
if (!target) return false
74+
if (target.getAttribute('aria-disabled') === 'true') return true
7375
fireCommitEvent(target)
7476
return true
7577
}

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</label>
1616
<ul role="listbox" id="list-id">
1717
<li id="baymax" role="option">Baymax</li>
18-
<li><del>BB-8</del></li>
18+
<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>
2121
</ul>

test/test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ function press(input, key, ctrlKey) {
22
input.dispatchEvent(new KeyboardEvent('keydown', {key, ctrlKey}))
33
}
44

5+
function click(element) {
6+
element.dispatchEvent(new MouseEvent('click', {bubbles: true}))
7+
}
8+
59
describe('combobox-nav', function() {
610
describe('with API', function() {
711
beforeEach(function() {
@@ -84,6 +88,7 @@ describe('combobox-nav', function() {
8488
assert.equal(options[4].getAttribute('aria-selected'), 'true')
8589
assert.equal(input.getAttribute('aria-activedescendant'), 'wall-e')
8690
press(input, 'Enter')
91+
click(options[4])
8792

8893
press(input, 'p', true)
8994
assert.equal(options[3].getAttribute('aria-selected'), 'true')
@@ -107,9 +112,9 @@ describe('combobox-nav', function() {
107112
expectedTargets.push(target.id)
108113
})
109114

110-
options[2].dispatchEvent(new MouseEvent('click', {bubbles: true}))
111-
options[1].dispatchEvent(new MouseEvent('click', {bubbles: true}))
112-
options[0].dispatchEvent(new MouseEvent('click', {bubbles: true}))
115+
click(options[2])
116+
click(options[1])
117+
click(options[0])
113118

114119
assert.equal(expectedTargets.length, 2)
115120
assert.equal(expectedTargets[0], 'hubot')

0 commit comments

Comments
 (0)