Skip to content

Commit 2fb2d50

Browse files
committed
fix(select): typehead
1 parent be22989 commit 2fb2d50

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test.describe('Mouse Behavior', () => {
3030
await expect(getTrigger()).toHaveAttribute('aria-expanded', 'true');
3131
});
3232

33-
test(`GIVEN a hero select with an open listbox
33+
test(`GIVEN a hero select with an open listbox
3434
WHEN the trigger is clicked
3535
THEN close the listbox AND aria-expanded should be false`, async ({ page }) => {
3636
const { getTrigger, getListbox, openListbox } = await setup(page, 'select-hero-test');
@@ -42,7 +42,7 @@ test.describe('Mouse Behavior', () => {
4242
await expect(getTrigger()).toHaveAttribute('aria-expanded', 'false');
4343
});
4444

45-
test(`GIVEN a hero select with an open listbox
45+
test(`GIVEN a hero select with an open listbox
4646
WHEN an option is clicked
4747
THEN close the listbox AND aria-expanded should be false`, async ({ page }) => {
4848
const { getTrigger, getListbox, getOptions, openListbox } = await setup(
@@ -59,7 +59,7 @@ test.describe('Mouse Behavior', () => {
5959
await expect(getTrigger()).toHaveAttribute('aria-expanded', 'false');
6060
});
6161

62-
test(`GIVEN a hero select with an open listbox
62+
test(`GIVEN a hero select with an open listbox
6363
WHEN the 2nd option is clicked
6464
THEN the 2nd option should have aria-selected`, async ({ page }) => {
6565
const { getOptions, openListbox } = await setup(page, 'select-hero-test');
@@ -72,7 +72,7 @@ test.describe('Mouse Behavior', () => {
7272
await expect(options[1]).toHaveAttribute('aria-selected', 'true');
7373
});
7474

75-
test(`GIVEN a hero select with an open listbox
75+
test(`GIVEN a hero select with an open listbox
7676
WHEN the 3rd option is clicked
7777
THEN the 3rd option should be the selected value`, async ({ page }) => {
7878
const { getOptions, getValue, openListbox } = await setup(page, 'select-hero-test');
@@ -178,7 +178,7 @@ test.describe('Keyboard Behavior', () => {
178178

179179
test(`GIVEN a hero select with an opened listbox
180180
WHEN pressing the escape key
181-
THEN the listbox should close
181+
THEN the listbox should close
182182
AND aria-expanded should be false`, async ({ page }) => {
183183
const { getTrigger, getListbox, openListbox } = await setup(
184184
page,
@@ -212,7 +212,7 @@ test.describe('Keyboard Behavior', () => {
212212
test.describe('data-highlighted navigation', () => {
213213
test(`GIVEN a hero select
214214
WHEN pressing the down arrow key
215-
THEN open up the listbox
215+
THEN open up the listbox
216216
AND the first option should have data-highlighted`, async ({ page }) => {
217217
const { getTrigger, getListbox, getOptions } = await setup(
218218
page,
@@ -229,7 +229,7 @@ test.describe('Keyboard Behavior', () => {
229229

230230
test(`GIVEN a hero select
231231
WHEN pressing the enter key
232-
THEN open up the listbox
232+
THEN open up the listbox
233233
AND the first option should have data-highlighted`, async ({ page }) => {
234234
const { getTrigger, getListbox, getOptions } = await setup(
235235
page,
@@ -246,7 +246,7 @@ test.describe('Keyboard Behavior', () => {
246246

247247
test(`GIVEN a hero select
248248
WHEN pressing the space key
249-
THEN open up the listbox
249+
THEN open up the listbox
250250
AND the first option should have data-highlighted`, async ({ page }) => {
251251
const { getTrigger, getListbox, getOptions } = await setup(
252252
page,
@@ -263,7 +263,7 @@ test.describe('Keyboard Behavior', () => {
263263

264264
test(`GIVEN a hero select
265265
WHEN pressing the up arrow
266-
THEN open up the listbox
266+
THEN open up the listbox
267267
AND the first option should have data-highlighted`, async ({ page }) => {
268268
const { getTrigger, getListbox, getOptions } = await setup(
269269
page,
@@ -497,7 +497,7 @@ test.describe('Keyboard Behavior', () => {
497497
});
498498

499499
test(`GIVEN an open select with a typeahead support
500-
WHEN the user types in the letters "jjt"
500+
WHEN the user types in the letters "jjt"
501501
THEN the first option starting with the letter "t" should have data-highlighted`, async ({
502502
page,
503503
}) => {
@@ -522,8 +522,8 @@ test.describe('Keyboard Behavior', () => {
522522
'select-typeahead-test',
523523
);
524524
await openListbox('ArrowDown');
525-
await getTrigger().pressSequentially('a', { delay: 1250 });
526-
await getTrigger().pressSequentially('je', { delay: 250 });
525+
await getTrigger().pressSequentially('a', { delay: 1500 });
526+
await getTrigger().pressSequentially('je', { delay: 100 });
527527
const highlightedOpt = getRoot().locator('[data-highlighted]');
528528
await expect(highlightedOpt).toContainText('jessie', { ignoreCase: true });
529529
});
@@ -682,7 +682,7 @@ test.describe('Props', () => {
682682
THEN fallback to the placeholder`, async ({ page }) => {
683683
const { getValue } = await setup(page, 'select-wrong-value-test');
684684

685-
/**
685+
/**
686686
we also currently give a console warning in the terminal if an option does not match. Ideally, I would like to have a union type of options, and it gives an error if there is no matching option
687687
*/
688688
expect(await getValue()).toEqual('wrong value placeholder');

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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);
910

1011
const firstCharOptionsSig = useComputed$(() => {
1112
return context.optionsSig.value.map((opt) => opt.value.slice(0, 1).toLowerCase());
@@ -41,11 +42,14 @@ export function useTypeahead() {
4142
if (isRepeatedChar) {
4243
const nextChars = firstCharOptionsSig.value.slice(indexDiffSig.value);
4344
const repeatIndex = nextChars.indexOf(key);
45+
console.log('me first ', repeatIndex, indexDiffSig.value);
4446
if (repeatIndex !== -1) {
4547
const nextIndex = repeatIndex + indexDiffSig.value;
4648

4749
context.highlightedIndexSig.value = nextIndex;
4850
indexDiffSig.value = nextIndex + 1;
51+
console.log('me repeats ', nextIndex, indexDiffSig.value);
52+
return;
4953
}
5054
return;
5155
}
@@ -56,12 +60,12 @@ export function useTypeahead() {
5660
});
5761

5862
const multipleChars$ = $(() => {
59-
console.log('im running');
6063
// If multiple keys are typed in quick succession, visual focus moves to the first option that matches the full string.
6164
clearTimeout(prevTimeoutSig.value);
6265
prevTimeoutSig.value = setTimeout(() => {
6366
inputStrSig.value = '';
6467
}, 1000);
68+
6569
const firstPossibleOpt = fullStrOptionsSig.value.findIndex((str) => {
6670
const size = inputStrSig.value.length;
6771
return str.substring(0, size) === inputStrSig.value;
@@ -70,12 +74,13 @@ export function useTypeahead() {
7074
context.highlightedIndexSig.value = firstPossibleOpt;
7175
return;
7276
}
73-
inputStrSig.value = '';
77+
inputStrSig.value = key;
7478
firstCharOnly$();
7579
});
7680

7781
if (inputStrSig.value.length === 1) {
7882
firstCharOnly$();
83+
return;
7984
}
8085
// const firstCharOptionsSig = useComputed$(() => {
8186
// return context.optionsSig.value.map((opt) => opt.value.slice(0, 1).toLowerCase());

0 commit comments

Comments
 (0)