Skip to content

Commit

Permalink
More changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anleac committed Feb 23, 2024
1 parent dfbf7c5 commit 4365fb9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
19 changes: 7 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export default class Combobox {
;(this.input as HTMLElement).addEventListener('keydown', this.keyboardEventHandler)
this.list.addEventListener('click', commitWithElement)
this.indicateDefaultOption()
this.selectFirstItemIfNeeded()
}

stop(): void {
Expand All @@ -98,11 +97,7 @@ export default class Combobox {
Array.from(this.list.querySelectorAll<HTMLElement>('[role="option"]:not([aria-disabled="true"])'))
.filter(visible)[0]
?.setAttribute('data-combobox-option-default', 'true')
}
}

selectFirstItemIfNeeded(): void {
if (this.firstOptionSelectionMode === 'selected') {
} else if (this.firstOptionSelectionMode === 'selected') {
this.navigate(1)
}
}
Expand All @@ -113,7 +108,7 @@ export default class Combobox {
const focusIndex = els.indexOf(focusEl)

if ((focusIndex === els.length - 1 && indexDiff === 1) || (focusIndex === 0 && indexDiff === -1)) {
this.clearSelection()
this.resetSelection()
this.input.focus()
return
}
Expand Down Expand Up @@ -146,10 +141,11 @@ export default class Combobox {
for (const el of this.list.querySelectorAll('[aria-selected="true"]')) {
el.removeAttribute('aria-selected')
}
}

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

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

Expand Down
10 changes: 8 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,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 Down

0 comments on commit 4365fb9

Please sign in to comment.