Skip to content

Commit 4d751f4

Browse files
author
Andrew Leach
authored
Merge pull request #82 from github/small-nav-patch
Fix `FirstOptionSelectionMode`:`selected` clearing on typing
2 parents 6a87759 + dbf90b5 commit 4d751f4

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

src/index.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default class Combobox {
7979
this.input.addEventListener('input', this.inputHandler)
8080
;(this.input as HTMLElement).addEventListener('keydown', this.keyboardEventHandler)
8181
this.list.addEventListener('click', commitWithElement)
82-
this.indicateDefaultOption()
82+
this.resetSelection()
8383
}
8484

8585
stop(): void {
@@ -138,13 +138,15 @@ export default class Combobox {
138138

139139
clearSelection(): void {
140140
this.input.removeAttribute('aria-activedescendant')
141-
for (const el of this.list.querySelectorAll('[aria-selected="true"]')) {
141+
for (const el of this.list.querySelectorAll('[aria-selected="true"], [data-combobox-option-default="true"]')) {
142142
el.removeAttribute('aria-selected')
143+
el.removeAttribute('data-combobox-option-default')
143144
}
145+
}
144146

145-
if (this.firstOptionSelectionMode === 'active') {
146-
this.indicateDefaultOption()
147-
}
147+
resetSelection(): void {
148+
this.clearSelection()
149+
this.indicateDefaultOption()
148150
}
149151
}
150152

@@ -189,7 +191,7 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
189191
break
190192
default:
191193
if (event.ctrlKey) break
192-
combobox.clearSelection()
194+
combobox.resetSelection()
193195
}
194196
}
195197

test/test.js

+43-4
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ describe('combobox-nav', function () {
285285
assert.equal(document.querySelector('[data-combobox-option-default]'), options[0])
286286
})
287287

288+
it('first item remains active when typing', () => {
289+
const text = 'R2-D2'
290+
for (let i = 0; i < text.length; i++) {
291+
press(input, text[i])
292+
}
293+
294+
assert.equal(document.querySelector('[data-combobox-option-default]'), options[0])
295+
})
296+
288297
it('applies default option on Enter', () => {
289298
let commits = 0
290299
document.addEventListener('combobox-commit', () => commits++)
@@ -299,12 +308,18 @@ describe('combobox-nav', function () {
299308
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 0)
300309
})
301310

302-
it('resets default indication when selection cleared', () => {
311+
it('resets default indication when selection reset', () => {
303312
combobox.navigate(1)
304-
combobox.clearSelection()
313+
combobox.resetSelection()
305314
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 1)
306315
})
307316

317+
it('removes default indication when selection cleared', () => {
318+
combobox.navigate(1)
319+
combobox.clearSelection()
320+
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 0)
321+
})
322+
308323
it('does not error when no options are visible', () => {
309324
assert.doesNotThrow(() => {
310325
document.getElementById('list-id').style.display = 'none'
@@ -325,8 +340,6 @@ describe('combobox-nav', function () {
325340
<li><del>BB-8</del></li>
326341
<li id="hubot" role="option">Hubot</li>
327342
<li id="r2-d2" role="option">R2-D2</li>
328-
<li id="johnny-5" hidden role="option">Johnny 5</li>
329-
<li id="wall-e" role="option" aria-disabled="true">Wall-E</li>
330343
<li><a href="#link" role="option" id="link">Link</a></li>
331344
</ul>
332345
`
@@ -349,6 +362,32 @@ describe('combobox-nav', function () {
349362
assert.equal(list.children[0].getAttribute('aria-selected'), 'true')
350363
})
351364

365+
it('first item remains selected when typing', () => {
366+
const text = 'R2-D2'
367+
for (let i = 0; i < text.length; i++) {
368+
press(input, text[i])
369+
}
370+
371+
assert.equal(list.children[0].getAttribute('aria-selected'), 'true')
372+
})
373+
374+
it('pressing key down off the last item will have no items selected', () => {
375+
// Get all visible options in the list
376+
const options = document.querySelectorAll('[role=option]:not([aria-hidden=true])')
377+
// Key press down for each item and ensure the next is selected
378+
for (let i = 0; i < options.length; i++) {
379+
if (i > 0) {
380+
assert.equal(options[i - 1].getAttribute('aria-selected'), null)
381+
}
382+
383+
assert.equal(options[i].getAttribute('aria-selected'), 'true')
384+
press(input, 'ArrowDown')
385+
}
386+
387+
const selected = document.querySelectorAll('[aria-selected]')
388+
assert.equal(selected.length, 0)
389+
})
390+
352391
it('indicates first option when restarted', () => {
353392
combobox.stop()
354393
combobox.start()

0 commit comments

Comments
 (0)