Skip to content

Commit

Permalink
Merge pull request #110 from github/use-default-option-in-combobox
Browse files Browse the repository at this point in the history
use default option in combobox
  • Loading branch information
keithamus authored Jul 4, 2023
2 parents 9f4250d + 1b46652 commit 68ff328
Showing 1 changed file with 4 additions and 12 deletions.
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

0 comments on commit 68ff328

Please sign in to comment.