UNPKG

@zag-js/dom-query

Version:

The dom helper library for zag.js machines

39 lines (37 loc) 1.12 kB
import "./chunk-QZ7TP4HQ.mjs"; // src/typeahead.ts import { getByText } from "./searchable.mjs"; function getByTypeaheadImpl(baseItems, options) { const { state, activeId, key, timeout = 350, itemToId } = options; const search = state.keysSoFar + key; const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]); const query = isRepeated ? search[0] : search; let items = baseItems.slice(); const next = getByText(items, query, activeId, itemToId); function cleanup() { clearTimeout(state.timer); state.timer = -1; } function update(value) { state.keysSoFar = value; cleanup(); if (value !== "") { state.timer = +setTimeout(() => { update(""); cleanup(); }, timeout); } } update(search); return next; } var getByTypeahead = /* @__PURE__ */ Object.assign(getByTypeaheadImpl, { defaultOptions: { keysSoFar: "", timer: -1 }, isValidEvent: isValidTypeaheadEvent }); function isValidTypeaheadEvent(event) { return event.key.length === 1 && !event.ctrlKey && !event.metaKey; } export { getByTypeahead };