Skip to content

Commit 9b97467

Browse files
authored
Merge pull request #8 from github/feat-export-clearselection
feat: export clearSelection
2 parents 239e191 + 4542c6b commit 9b97467

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

combobox-nav.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function keyboardBindings(event: KeyboardEvent) {
3434
}
3535
break
3636
case 'Escape':
37-
clearSelection(list)
37+
clearSelection(input, list)
3838
break
3939
case 'ArrowDown':
4040
navigate(input, list, 1)
@@ -109,7 +109,8 @@ export function navigate(
109109
}
110110
}
111111

112-
function clearSelection(list): void {
112+
export function clearSelection(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void {
113+
input.removeAttribute('aria-activedescendant')
113114
const target = list.querySelector('[aria-selected="true"]')
114115
if (!target) return
115116
target.setAttribute('aria-selected', 'false')
@@ -123,5 +124,5 @@ function trackComposition(event: Event): void {
123124
const list = document.getElementById(input.getAttribute('aria-owns') || '')
124125
if (!list) return
125126

126-
clearSelection(list)
127+
clearSelection(input, list)
127128
}

combobox-nav.js.flow

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
declare export function install(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void;
44
declare export function navigate(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement, indexDiff?: -1 | 1): void;
55
declare export function uninstall(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void;
6+
declare export function clearSelection(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void;

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,20 @@ describe('combobox-nav', function() {
132132
assert(eventFired)
133133
assert.equal(window.location.hash, '#wall-e')
134134
})
135+
136+
it('clears aria-activedescendant and sets aria-selected=false when cleared', function() {
137+
const input = document.querySelector('input')
138+
const list = document.querySelector('ul')
139+
const options = document.querySelectorAll('li')
140+
141+
press(input, 'ArrowDown')
142+
assert.equal(options[0].getAttribute('aria-selected'), 'true')
143+
assert.equal(input.getAttribute('aria-activedescendant'), 'baymax')
144+
145+
comboboxNav.clearSelection(input, list)
146+
147+
assert.equal(options[0].getAttribute('aria-selected'), 'false')
148+
assert.equal(input.hasAttribute('aria-activedescendant'), false)
149+
})
135150
})
136151
})

0 commit comments

Comments
 (0)