Skip to content

Commit aef3982

Browse files
fix: typeahead logic passed to the select items
1 parent f571e9c commit aef3982

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import {
99
type PropsOf,
1010
useContextProvider,
1111
sync$,
12+
useOnWindow,
13+
QRL,
1214
} from '@builder.io/qwik';
1315
import { isServer, isBrowser } from '@builder.io/qwik/build';
1416
import SelectContextId, {
1517
SelectItemContext,
1618
selectItemContextId,
1719
} from './select-context';
18-
import { useSelect, useTypeahead } from './use-select';
20+
import { useSelect } from './use-select';
1921

2022
export type SelectItemProps = PropsOf<'li'> & {
2123
/** Internal index we get from the inline component. Please see select-inline.tsx */
@@ -35,11 +37,18 @@ export const HSelectItem = component$<SelectItemProps>((props) => {
3537
const itemRef = useSignal<HTMLLIElement>();
3638
const localIndexSig = useSignal<number | null>(null);
3739
const itemId = `${context.localId}-${_index}`;
40+
const typeaheadFnSig = useSignal<QRL<(key: string) => Promise<void>>>();
3841

3942
const { selectionManager$, getNextEnabledItemIndex$, getPrevEnabledItemIndex$ } =
4043
useSelect();
4144

42-
const { typeahead$ } = useTypeahead();
45+
// we're getting the same function instance from the trigger, without needing to restructure context
46+
useOnWindow(
47+
'typeaheadFn',
48+
$((e: CustomEvent) => {
49+
typeaheadFnSig.value = e.detail;
50+
}),
51+
);
4352

4453
const isSelectedSig = useComputed$(() => {
4554
const index = _index ?? null;
@@ -144,7 +153,7 @@ export const HSelectItem = component$<SelectItemProps>((props) => {
144153
});
145154

146155
const handleKeyDown$ = $(async (e: KeyboardEvent) => {
147-
typeahead$(e.key);
156+
typeaheadFnSig.value?.(e.key);
148157

149158
switch (e.key) {
150159
case 'ArrowDown':

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { $, Slot, component$, sync$, useContext, type PropsOf } from '@builder.io/qwik';
1+
import {
2+
$,
3+
Slot,
4+
component$,
5+
sync$,
6+
useContext,
7+
useSignal,
8+
type PropsOf,
9+
} from '@builder.io/qwik';
210
import SelectContextId from './select-context';
311
import { useSelect, useTypeahead } from './use-select';
412

@@ -9,7 +17,7 @@ export const HSelectTrigger = component$<SelectTriggerProps>((props) => {
917
useSelect();
1018
const labelId = `${context.localId}-label`;
1119
const descriptionId = `${context.localId}-description`;
12-
20+
const initialKeyDownSig = useSignal(true);
1321
const { typeahead$ } = useTypeahead();
1422

1523
const handleClickSync$ = sync$((e: MouseEvent) => {
@@ -106,6 +114,9 @@ export const HSelectTrigger = component$<SelectTriggerProps>((props) => {
106114
await new Promise((resolve) => setTimeout(resolve, 5));
107115
context.highlightedItemRef.value?.focus();
108116
}
117+
118+
if (!initialKeyDownSig.value) return;
119+
document.dispatchEvent(new CustomEvent('typeaheadFn', { detail: typeahead$ }));
109120
});
110121

111122
return (

0 commit comments

Comments
 (0)