UNPKG

@zag-js/dom-query

Version:

The dom helper library for zag.js machines

62 lines (60 loc) 2.15 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/typeahead.ts var typeahead_exports = {}; __export(typeahead_exports, { getByTypeahead: () => getByTypeahead }); module.exports = __toCommonJS(typeahead_exports); var import_searchable = require("./searchable.js"); 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 = (0, import_searchable.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; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getByTypeahead });