Skip to content

Commit 418e72b

Browse files
authored
Make Tab insertion optional through a settings object
1 parent b27b2e9 commit 418e72b

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/index.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
export type ComboboxSettings = {
2+
/**
3+
* Control whether pressing the `Tab` key should insert a suggestion (`Enter` will always
4+
* insert a suggestion regardless of this setting). When `true`, tab-navigation will be
5+
* hijacked when open (which can have negative impacts on accessibility) but the combobox
6+
* will more closely imitate a native IDE experience.
7+
*
8+
* Defaults to `true`.
9+
*/
10+
tabInsertsSuggestions?: boolean
11+
}
12+
113
export default class Combobox {
214
isComposing: boolean
315
list: HTMLElement
@@ -6,10 +18,17 @@ export default class Combobox {
618
compositionEventHandler: (event: Event) => void
719
inputHandler: (event: Event) => void
820
ctrlBindings: boolean
21+
tabInsertsSuggestions: boolean
922

10-
constructor(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement) {
23+
constructor(
24+
input: HTMLTextAreaElement | HTMLInputElement,
25+
list: HTMLElement,
26+
{tabInsertsSuggestions}: ComboboxSettings = {}
27+
) {
1128
this.input = input
1229
this.list = list
30+
this.tabInsertsSuggestions = tabInsertsSuggestions ?? true
31+
1332
this.isComposing = false
1433

1534
if (!list.id) {
@@ -103,11 +122,15 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
103122

104123
switch (event.key) {
105124
case 'Enter':
106-
case 'Tab':
107125
if (commit(combobox.input, combobox.list)) {
108126
event.preventDefault()
109127
}
110128
break
129+
case 'Tab':
130+
if (combobox.tabInsertsSuggestions && commit(combobox.input, combobox.list)) {
131+
event.preventDefault()
132+
}
133+
break
111134
case 'Escape':
112135
combobox.clearSelection()
113136
break

0 commit comments

Comments
 (0)