Forms
useTypeAhead
Composable interno per ricerca type-ahead nei dropdown tramite digitazione rapida con buffer e timeout.
Composable interno — utilizzato dai componenti dropdown della libreria (Select, MultiSelect) per navigazione rapida tramite tastiera.
Firma TypeScript
export function useTypeAhead<T>(params: UseTypeAheadParams<T>): UseTypeAheadResult
export interface UseTypeAheadParams<T> {
items: () => T[];
getLabel: (item: T) => string;
timeout?: number; // default: 500ms
}
export interface UseTypeAheadResult {
typeAheadBuffer: Ref<string>;
searchByTypeAhead: (char: string) => number;
}
Parametri
| Parametro | Tipo | Descrizione |
|---|---|---|
params.items | () => T[] | Getter reattivo della lista in cui cercare |
params.getLabel | (item: T) => string | Funzione per estrarre il testo visualizzato da un elemento |
params.timeout | number | Timeout in ms per il reset del buffer (default: 500) |
Valore di Ritorno
| Proprietà | Tipo | Descrizione |
|---|---|---|
typeAheadBuffer | Ref<string> | Caratteri digitati accumulati nel buffer corrente |
searchByTypeAhead | (char: string) => number | Aggiunge char al buffer e ritorna l'indice del primo match, -1 se non trovato |
Esempio
const { searchByTypeAhead } = useTypeAhead({
items: () => filteredOptions.value,
getLabel: (item) => item.label,
timeout: 500,
})
// In un handler keydown
const onKeydown = (event: KeyboardEvent) => {
if (event.key.length === 1) {
const matchIndex = searchByTypeAhead(event.key)
if (matchIndex >= 0) {
focusedIndex.value = matchIndex
}
}
}
Note
- Il buffer si resetta automaticamente dopo
timeoutms dall'ultimo tasto premuto. - La ricerca e case-insensitive (
toLowerCase()). - Usa
startsWithper il match: digitare'ma'trova'Mario','Marco'ecc. - Ogni chiamata a
searchByTypeAheadresetta il timer del buffer e aggiunge il carattere. - Cleanup del timer in
onBeforeUnmountautomatico.