Skip to content

Commit ae7984c

Browse files
authored
Move ctrlBindings to class property
1 parent 1edabde commit ae7984c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default class Combobox {
55
keyboardEventHandler: (event: KeyboardEvent) => void
66
compositionEventHandler: (event: Event) => void
77
inputHandler: (event: Event) => void
8+
ctrlBindings: boolean
89

910
constructor(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement) {
1011
this.input = input
@@ -15,9 +16,9 @@ export default class Combobox {
1516
list.id = `combobox-${Math.random().toString().slice(2, 6)}`
1617
}
1718

18-
const ctrlBindings = !!navigator.userAgent.match(/Macintosh/)
19+
this.ctrlBindings = !!navigator.userAgent.match(/Macintosh/)
1920

20-
this.keyboardEventHandler = event => keyboardBindings(event, this, ctrlBindings)
21+
this.keyboardEventHandler = event => keyboardBindings(event, this)
2122
this.compositionEventHandler = event => trackComposition(event, this)
2223
this.inputHandler = this.clearSelection.bind(this)
2324
input.setAttribute('role', 'combobox')
@@ -95,9 +96,9 @@ export default class Combobox {
9596
}
9697
}
9798

98-
function keyboardBindings(event: KeyboardEvent, combobox: Combobox, ctrlBindings: boolean) {
99+
function keyboardBindings(event: KeyboardEvent, combobox: Combobox) {
99100
if (event.shiftKey || event.metaKey || event.altKey) return
100-
if (!ctrlBindings && event.ctrlKey) return
101+
if (!combobox.ctrlBindings && event.ctrlKey) return
101102
if (combobox.isComposing) return
102103

103104
switch (event.key) {
@@ -119,13 +120,13 @@ function keyboardBindings(event: KeyboardEvent, combobox: Combobox, ctrlBindings
119120
event.preventDefault()
120121
break
121122
case 'n':
122-
if (ctrlBindings && event.ctrlKey) {
123+
if (combobox.ctrlBindings && event.ctrlKey) {
123124
combobox.navigate(1)
124125
event.preventDefault()
125126
}
126127
break
127128
case 'p':
128-
if (ctrlBindings && event.ctrlKey) {
129+
if (combobox.ctrlBindings && event.ctrlKey) {
129130
combobox.navigate(-1)
130131
event.preventDefault()
131132
}

0 commit comments

Comments
 (0)