Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default class Autocomplete {
this.container = container
this.input = input
this.results = results
this.combobox = new Combobox(input, results)
this.combobox = new Combobox(input, results, {
defaultFirstOption: autoselectEnabled,
})
this.feedback = (container.getRootNode() as Document).getElementById(`${this.results.id}-feedback`)
this.autoselectEnabled = autoselectEnabled
this.clearButton = (container.getRootNode() as Document).getElementById(`${this.input.id || this.input.name}-clear`)
Expand Down Expand Up @@ -108,17 +110,6 @@ export default class Autocomplete {
}

onKeydown(event: KeyboardEvent): void {
// if autoselect is enabled, Enter key will select the first option
if (event.key === 'Enter' && this.container.open && this.autoselectEnabled) {
const firstOption = this.results.children[0]
if (firstOption) {
event.stopPropagation()
event.preventDefault()

this.onCommit({target: firstOption})
}
}

if (event.key === 'Escape' && this.container.open) {
this.container.open = false
event.stopPropagation()
Expand Down Expand Up @@ -212,6 +203,7 @@ export default class Autocomplete {
// eslint-disable-next-line github/no-inner-html
this.results.innerHTML = html as string
this.identifyOptions()
this.combobox.indicateDefaultOption()
const allNewOptions = this.results.querySelectorAll('[role="option"]')
const hasResults = !!allNewOptions.length
const numOptions = allNewOptions.length
Expand Down