File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change
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
+
1
13
export default class Combobox {
2
14
isComposing : boolean
3
15
list : HTMLElement
@@ -6,10 +18,17 @@ export default class Combobox {
6
18
compositionEventHandler : ( event : Event ) => void
7
19
inputHandler : ( event : Event ) => void
8
20
ctrlBindings : boolean
21
+ tabInsertsSuggestions : boolean
9
22
10
- constructor ( input : HTMLTextAreaElement | HTMLInputElement , list : HTMLElement ) {
23
+ constructor (
24
+ input : HTMLTextAreaElement | HTMLInputElement ,
25
+ list : HTMLElement ,
26
+ { tabInsertsSuggestions} : ComboboxSettings = { }
27
+ ) {
11
28
this . input = input
12
29
this . list = list
30
+ this . tabInsertsSuggestions = tabInsertsSuggestions ?? true
31
+
13
32
this . isComposing = false
14
33
15
34
if ( ! list . id ) {
@@ -103,11 +122,15 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
103
122
104
123
switch ( event . key ) {
105
124
case 'Enter' :
106
- case 'Tab' :
107
125
if ( commit ( combobox . input , combobox . list ) ) {
108
126
event . preventDefault ( )
109
127
}
110
128
break
129
+ case 'Tab' :
130
+ if ( combobox . tabInsertsSuggestions && commit ( combobox . input , combobox . list ) ) {
131
+ event . preventDefault ( )
132
+ }
133
+ break
111
134
case 'Escape' :
112
135
combobox . clearSelection ( )
113
136
break
You can’t perform that action at this time.
0 commit comments