Skip to content

Commit 68ff328

Browse files
authored
Merge pull request #110 from github/use-default-option-in-combobox
use default option in combobox
2 parents 9f4250d + 1b46652 commit 68ff328

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

src/autocomplete.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export default class Autocomplete {
2727
this.container = container
2828
this.input = input
2929
this.results = results
30-
this.combobox = new Combobox(input, results)
30+
this.combobox = new Combobox(input, results, {
31+
defaultFirstOption: autoselectEnabled,
32+
})
3133
this.feedback = (container.getRootNode() as Document).getElementById(`${this.results.id}-feedback`)
3234
this.autoselectEnabled = autoselectEnabled
3335
this.clearButton = (container.getRootNode() as Document).getElementById(`${this.input.id || this.input.name}-clear`)
@@ -108,17 +110,6 @@ export default class Autocomplete {
108110
}
109111

110112
onKeydown(event: KeyboardEvent): void {
111-
// if autoselect is enabled, Enter key will select the first option
112-
if (event.key === 'Enter' && this.container.open && this.autoselectEnabled) {
113-
const firstOption = this.results.children[0]
114-
if (firstOption) {
115-
event.stopPropagation()
116-
event.preventDefault()
117-
118-
this.onCommit({target: firstOption})
119-
}
120-
}
121-
122113
if (event.key === 'Escape' && this.container.open) {
123114
this.container.open = false
124115
event.stopPropagation()
@@ -212,6 +203,7 @@ export default class Autocomplete {
212203
// eslint-disable-next-line github/no-inner-html
213204
this.results.innerHTML = html as string
214205
this.identifyOptions()
206+
this.combobox.indicateDefaultOption()
215207
const allNewOptions = this.results.querySelectorAll('[role="option"]')
216208
const hasResults = !!allNewOptions.length
217209
const numOptions = allNewOptions.length

0 commit comments

Comments
 (0)