Skip to content

Commit 7deb935

Browse files
refactor(select): improved naming, code readabiliy, notes on typeahead
1 parent 2fb2d50 commit 7deb935

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

packages/kit-headless/src/components/select/use-select.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,30 @@ export function useTypeahead() {
2525
// First opens the listbox if it is not already displayed and then moves visual focus to the first option that matches the typed character.
2626
const singleInputChar = key.toLowerCase();
2727

28-
const charIndex = firstCharOptionsSig.value.indexOf(singleInputChar);
28+
// index the first time we see the same character
29+
const firstCharIndex = firstCharOptionsSig.value.indexOf(singleInputChar);
2930

30-
if (charIndex === -1 || charIndex === undefined) {
31+
if (firstCharIndex === -1 || firstCharIndex === undefined) {
3132
return null;
3233
}
3334
if (indexDiffSig.value === undefined) {
34-
indexDiffSig.value = charIndex + 1;
35-
context.highlightedIndexSig.value = charIndex;
35+
indexDiffSig.value = firstCharIndex + 1;
36+
context.highlightedIndexSig.value = firstCharIndex;
3637
return;
3738
}
3839

3940
// 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;
4145

4246
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);
4552
console.log('me first ', repeatIndex, indexDiffSig.value);
4653
if (repeatIndex !== -1) {
4754
const nextIndex = repeatIndex + indexDiffSig.value;
@@ -51,10 +58,11 @@ export function useTypeahead() {
5158
console.log('me repeats ', nextIndex, indexDiffSig.value);
5259
return;
5360
}
61+
// actual magical return
5462
return;
5563
}
56-
indexDiffSig.value = charIndex + 1;
57-
context.highlightedIndexSig.value = charIndex;
64+
indexDiffSig.value = firstCharIndex + 1;
65+
context.highlightedIndexSig.value = firstCharIndex;
5866

5967
return;
6068
});
@@ -82,9 +90,7 @@ export function useTypeahead() {
8290
firstCharOnly$();
8391
return;
8492
}
85-
// const firstCharOptionsSig = useComputed$(() => {
86-
// return context.optionsSig.value.map((opt) => opt.value.slice(0, 1).toLowerCase());
87-
// });
93+
8894
multipleChars$();
8995
});
9096

0 commit comments

Comments
 (0)