Skip to content

Commit

Permalink
Merge pull request #82 from github/small-nav-patch
Browse files Browse the repository at this point in the history
Fix `FirstOptionSelectionMode`:`selected` clearing on typing
  • Loading branch information
anleac authored Feb 26, 2024
2 parents 6a87759 + dbf90b5 commit 4d751f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class Combobox {
this.input.addEventListener('input', this.inputHandler)
;(this.input as HTMLElement).addEventListener('keydown', this.keyboardEventHandler)
this.list.addEventListener('click', commitWithElement)
this.indicateDefaultOption()
this.resetSelection()
}

stop(): void {
Expand Down Expand Up @@ -138,13 +138,15 @@ export default class Combobox {

clearSelection(): void {
this.input.removeAttribute('aria-activedescendant')
for (const el of this.list.querySelectorAll('[aria-selected="true"]')) {
for (const el of this.list.querySelectorAll('[aria-selected="true"], [data-combobox-option-default="true"]')) {
el.removeAttribute('aria-selected')
el.removeAttribute('data-combobox-option-default')
}
}

if (this.firstOptionSelectionMode === 'active') {
this.indicateDefaultOption()
}
resetSelection(): void {
this.clearSelection()
this.indicateDefaultOption()
}
}

Expand Down Expand Up @@ -189,7 +191,7 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
break
default:
if (event.ctrlKey) break
combobox.clearSelection()
combobox.resetSelection()
}
}

Expand Down
47 changes: 43 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ describe('combobox-nav', function () {
assert.equal(document.querySelector('[data-combobox-option-default]'), options[0])
})

it('first item remains active when typing', () => {
const text = 'R2-D2'
for (let i = 0; i < text.length; i++) {
press(input, text[i])
}

assert.equal(document.querySelector('[data-combobox-option-default]'), options[0])
})

it('applies default option on Enter', () => {
let commits = 0
document.addEventListener('combobox-commit', () => commits++)
Expand All @@ -299,12 +308,18 @@ describe('combobox-nav', function () {
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 0)
})

it('resets default indication when selection cleared', () => {
it('resets default indication when selection reset', () => {
combobox.navigate(1)
combobox.clearSelection()
combobox.resetSelection()
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 1)
})

it('removes default indication when selection cleared', () => {
combobox.navigate(1)
combobox.clearSelection()
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 0)
})

it('does not error when no options are visible', () => {
assert.doesNotThrow(() => {
document.getElementById('list-id').style.display = 'none'
Expand All @@ -325,8 +340,6 @@ describe('combobox-nav', function () {
<li><del>BB-8</del></li>
<li id="hubot" role="option">Hubot</li>
<li id="r2-d2" role="option">R2-D2</li>
<li id="johnny-5" hidden role="option">Johnny 5</li>
<li id="wall-e" role="option" aria-disabled="true">Wall-E</li>
<li><a href="#link" role="option" id="link">Link</a></li>
</ul>
`
Expand All @@ -349,6 +362,32 @@ describe('combobox-nav', function () {
assert.equal(list.children[0].getAttribute('aria-selected'), 'true')
})

it('first item remains selected when typing', () => {
const text = 'R2-D2'
for (let i = 0; i < text.length; i++) {
press(input, text[i])
}

assert.equal(list.children[0].getAttribute('aria-selected'), 'true')
})

it('pressing key down off the last item will have no items selected', () => {
// Get all visible options in the list
const options = document.querySelectorAll('[role=option]:not([aria-hidden=true])')
// Key press down for each item and ensure the next is selected
for (let i = 0; i < options.length; i++) {
if (i > 0) {
assert.equal(options[i - 1].getAttribute('aria-selected'), null)
}

assert.equal(options[i].getAttribute('aria-selected'), 'true')
press(input, 'ArrowDown')
}

const selected = document.querySelectorAll('[aria-selected]')
assert.equal(selected.length, 0)
})

it('indicates first option when restarted', () => {
combobox.stop()
combobox.start()
Expand Down

0 comments on commit 4d751f4

Please sign in to comment.