Skip to content

Commit 9b87f70

Browse files
committed
Take over attribute setting responsibilities
1 parent e2f4bd6 commit 9b87f70

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $ npm install @github/combobox-nav
1515
```html
1616
<label>
1717
Robot
18-
<input id="robot-input" aria-controls="list-id" role="combobox" type="text" aria-expanded="false">
18+
<input id="robot-input" type="text">
1919
</label>
2020
<ul role="listbox" id="list-id" hidden>
2121
<li id="baymax" role="option">Baymax</li>

src/combobox-nav.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
import {scrollTo} from './scroll'
44

55
export function install(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void {
6+
if (!list.id) {
7+
list.id = `combobox-${Math.random()
8+
.toString()
9+
.slice(2, 6)}`
10+
}
11+
input.setAttribute('role', 'combobox')
12+
input.setAttribute('aria-controls', list.id)
13+
if (!input.hasAttribute('aria-expanded')) input.setAttribute('aria-expanded', 'false')
614
input.addEventListener('compositionstart', trackComposition)
715
input.addEventListener('compositionend', trackComposition)
816
input.addEventListener('keydown', keyboardBindings)
@@ -11,6 +19,9 @@ export function install(input: HTMLTextAreaElement | HTMLInputElement, list: HTM
1119

1220
export function uninstall(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void {
1321
input.removeAttribute('aria-activedescendant')
22+
input.removeAttribute('role')
23+
input.removeAttribute('aria-controls')
24+
input.removeAttribute('aria-expanded')
1425
input.removeEventListener('compositionstart', trackComposition)
1526
input.removeEventListener('compositionend', trackComposition)
1627
input.removeEventListener('keydown', keyboardBindings)

test/test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('combobox-nav', function() {
1010
describe('with API', function() {
1111
beforeEach(function() {
1212
document.body.innerHTML = `
13-
<input aria-controls="list-id" role="combobox" type="text">
13+
<input type="text">
1414
<ul role="listbox" id="list-id">
1515
<li id="baymax" role="option">Baymax</li>
1616
<li><del>BB-8</del></li>
@@ -29,6 +29,10 @@ describe('combobox-nav', function() {
2929
const list = document.querySelector('ul')
3030
comboboxNav.install(input, list)
3131

32+
assert.equal(input.getAttribute('role'), 'combobox')
33+
assert.equal(input.getAttribute('aria-expanded'), 'false')
34+
assert.equal(input.getAttribute('aria-controls'), 'list-id')
35+
3236
press(input, 'ArrowDown')
3337
assert.equal(list.children[0].getAttribute('aria-selected'), 'true')
3438
comboboxNav.navigate(input, list, 1)
@@ -44,7 +48,7 @@ describe('combobox-nav', function() {
4448
describe('with default setup', function() {
4549
beforeEach(function() {
4650
document.body.innerHTML = `
47-
<input aria-controls="list-id" role="combobox" type="text">
51+
<input type="text">
4852
<ul role="listbox" id="list-id">
4953
<li id="baymax" role="option">Baymax</li>
5054
<li><del>BB-8</del></li>

0 commit comments

Comments
 (0)