Skip to content

Commit fe00b50

Browse files
fix(select): typeahead fixed looping of chars, tests pass
1 parent 7deb935 commit fe00b50

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

apps/website/src/routes/docs/headless/select/select.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,21 @@ test.describe('Keyboard Behavior', () => {
540540
const highlightedOpt = getRoot().locator('[data-highlighted]');
541541
await expect(highlightedOpt).toContainText('abby', { ignoreCase: true });
542542
});
543+
544+
test(`GIVEN an open select with typeahead support and repeated characters
545+
WHEN the user types in a letter three times
546+
THEN the data-highlighted value should cycle through the options`, async ({
547+
page,
548+
}) => {
549+
const { getRoot, getTrigger, openListbox } = await setup(
550+
page,
551+
'select-typeahead-test',
552+
);
553+
await openListbox('ArrowDown');
554+
await getTrigger().pressSequentially('jjj', { delay: 1250 });
555+
const highlightedOpt = getRoot().locator('[data-highlighted]');
556+
await expect(highlightedOpt).toContainText('jim', { ignoreCase: true });
557+
});
543558
});
544559
});
545560

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export function useTypeahead() {
66
const inputStrSig = useSignal('');
77
const indexDiffSig = useSignal<number | undefined>(undefined);
88
const prevTimeoutSig = useSignal<undefined | NodeJS.Timeout>(undefined);
9-
console.log(inputStrSig.value, indexDiffSig.value, prevTimeoutSig.value);
109

1110
const firstCharOptionsSig = useComputed$(() => {
1211
return context.optionsSig.value.map((opt) => opt.value.slice(0, 1).toLowerCase());
@@ -49,16 +48,16 @@ export function useTypeahead() {
4948

5049
// index the next time we could see the same character
5150
const repeatIndex = nextCharSearch.indexOf(key);
52-
console.log('me first ', repeatIndex, indexDiffSig.value);
5351
if (repeatIndex !== -1) {
5452
const nextIndex = repeatIndex + indexDiffSig.value;
5553

5654
context.highlightedIndexSig.value = nextIndex;
5755
indexDiffSig.value = nextIndex + 1;
58-
console.log('me repeats ', nextIndex, indexDiffSig.value);
5956
return;
6057
}
61-
// actual magical return
58+
59+
indexDiffSig.value = undefined;
60+
context.highlightedIndexSig.value = firstCharIndex;
6261
return;
6362
}
6463
indexDiffSig.value = firstCharIndex + 1;

0 commit comments

Comments
 (0)