Skip to content

Commit 4365fb9

Browse files
author
Andrew Leach
committed
More changes
1 parent dfbf7c5 commit 4365fb9

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export default class Combobox {
8080
;(this.input as HTMLElement).addEventListener('keydown', this.keyboardEventHandler)
8181
this.list.addEventListener('click', commitWithElement)
8282
this.indicateDefaultOption()
83-
this.selectFirstItemIfNeeded()
8483
}
8584

8685
stop(): void {
@@ -98,11 +97,7 @@ export default class Combobox {
9897
Array.from(this.list.querySelectorAll<HTMLElement>('[role="option"]:not([aria-disabled="true"])'))
9998
.filter(visible)[0]
10099
?.setAttribute('data-combobox-option-default', 'true')
101-
}
102-
}
103-
104-
selectFirstItemIfNeeded(): void {
105-
if (this.firstOptionSelectionMode === 'selected') {
100+
} else if (this.firstOptionSelectionMode === 'selected') {
106101
this.navigate(1)
107102
}
108103
}
@@ -113,7 +108,7 @@ export default class Combobox {
113108
const focusIndex = els.indexOf(focusEl)
114109

115110
if ((focusIndex === els.length - 1 && indexDiff === 1) || (focusIndex === 0 && indexDiff === -1)) {
116-
this.clearSelection()
111+
this.resetSelection()
117112
this.input.focus()
118113
return
119114
}
@@ -146,10 +141,11 @@ export default class Combobox {
146141
for (const el of this.list.querySelectorAll('[aria-selected="true"]')) {
147142
el.removeAttribute('aria-selected')
148143
}
144+
}
149145

150-
if (this.firstOptionSelectionMode === 'active') {
151-
this.indicateDefaultOption()
152-
}
146+
resetSelection(): void {
147+
this.clearSelection()
148+
this.indicateDefaultOption()
153149
}
154150
}
155151

@@ -194,8 +190,7 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
194190
break
195191
default:
196192
if (event.ctrlKey) break
197-
combobox.clearSelection()
198-
combobox.selectFirstItemIfNeeded()
193+
combobox.resetSelection()
199194
}
200195
}
201196

test/test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,18 @@ describe('combobox-nav', function () {
308308
assert.equal(document.querySelectorAll('[data-combobox-option-default]').length, 0)
309309
})
310310

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

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+
317323
it('does not error when no options are visible', () => {
318324
assert.doesNotThrow(() => {
319325
document.getElementById('list-id').style.display = 'none'

0 commit comments

Comments
 (0)