@@ -25,23 +25,30 @@ export function useTypeahead() {
25
25
// First opens the listbox if it is not already displayed and then moves visual focus to the first option that matches the typed character.
26
26
const singleInputChar = key . toLowerCase ( ) ;
27
27
28
- const charIndex = firstCharOptionsSig . value . indexOf ( singleInputChar ) ;
28
+ // index the first time we see the same character
29
+ const firstCharIndex = firstCharOptionsSig . value . indexOf ( singleInputChar ) ;
29
30
30
- if ( charIndex === - 1 || charIndex === undefined ) {
31
+ if ( firstCharIndex === - 1 || firstCharIndex === undefined ) {
31
32
return null ;
32
33
}
33
34
if ( indexDiffSig . value === undefined ) {
34
- indexDiffSig . value = charIndex + 1 ;
35
- context . highlightedIndexSig . value = charIndex ;
35
+ indexDiffSig . value = firstCharIndex + 1 ;
36
+ context . highlightedIndexSig . value = firstCharIndex ;
36
37
return ;
37
38
}
38
39
39
40
// If the same character is typed in succession, visual focus cycles among the options starting with that character.
40
- const isRepeatedChar = firstCharOptionsSig . value [ indexDiffSig . value - 1 ] === key ;
41
+
42
+ const prevIndex = indexDiffSig . value - 1 ;
43
+
44
+ const isRepeatedChar = firstCharOptionsSig . value [ prevIndex ] === key ;
41
45
42
46
if ( isRepeatedChar ) {
43
- const nextChars = firstCharOptionsSig . value . slice ( indexDiffSig . value ) ;
44
- const repeatIndex = nextChars . indexOf ( key ) ;
47
+ // Slices the options' first characters from indexDiffSig.value for finding the next matching character.
48
+ const nextCharSearch = firstCharOptionsSig . value . slice ( indexDiffSig . value ) ;
49
+
50
+ // index the next time we could see the same character
51
+ const repeatIndex = nextCharSearch . indexOf ( key ) ;
45
52
console . log ( 'me first ' , repeatIndex , indexDiffSig . value ) ;
46
53
if ( repeatIndex !== - 1 ) {
47
54
const nextIndex = repeatIndex + indexDiffSig . value ;
@@ -51,10 +58,11 @@ export function useTypeahead() {
51
58
console . log ( 'me repeats ' , nextIndex , indexDiffSig . value ) ;
52
59
return ;
53
60
}
61
+ // actual magical return
54
62
return ;
55
63
}
56
- indexDiffSig . value = charIndex + 1 ;
57
- context . highlightedIndexSig . value = charIndex ;
64
+ indexDiffSig . value = firstCharIndex + 1 ;
65
+ context . highlightedIndexSig . value = firstCharIndex ;
58
66
59
67
return ;
60
68
} ) ;
@@ -82,9 +90,7 @@ export function useTypeahead() {
82
90
firstCharOnly$ ( ) ;
83
91
return ;
84
92
}
85
- // const firstCharOptionsSig = useComputed$(() => {
86
- // return context.optionsSig.value.map((opt) => opt.value.slice(0, 1).toLowerCase());
87
- // });
93
+
88
94
multipleChars$ ( ) ;
89
95
} ) ;
90
96
0 commit comments