UNPKG

@ariakit/react

Version:

Toolkit for building accessible web apps with React

125 lines (105 loc) 4.86 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _FHQLDZSWcjs = require('./FHQLDZSW.cjs'); var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs'); // ../ariakit-react-components/dist/composite/composite-typeahead.js var TagName = "div"; var typeaheadStates = /* @__PURE__ */ new WeakMap(); function getTypeaheadState(store) { let typeaheadState = typeaheadStates.get(store); if (!typeaheadState) { typeaheadState = { chars: "", cleanupTimeout: 0 }; typeaheadStates.set(store, typeaheadState); } return typeaheadState; } function clearChars(typeaheadState) { typeaheadState.chars = ""; } function isValidTypeaheadEvent(event, typeaheadState) { const target = event.target; if (target && _LZ6L3ECGcjs.isTextField.call(void 0, target)) return false; if (event.key === " " && typeaheadState.chars.length) return true; return event.key.length === 1 && !event.ctrlKey && !event.altKey && !event.metaKey && /^[\p{Letter}\p{Number}]$/u.test(event.key); } function isSelfTargetOrItem(event, items) { if (_LZ6L3ECGcjs.isSelfTarget.call(void 0, event)) return true; const target = event.target; if (!target) return false; return items.some((item) => item.element === target); } function getEnabledItems(items) { return items.filter((item) => !item.disabled); } function itemTextStartsWith(item, text) { var _a; const itemText = ((_a = item.element) == null ? void 0 : _a.textContent) || item.children || "value" in item && item.value; if (!itemText) return false; return _LZ6L3ECGcjs.normalizeString.call(void 0, itemText).trim().toLowerCase().startsWith(text.toLowerCase()); } function getSameInitialItems({ activeId, char, items, typeaheadState }) { if (!activeId) return items; const activeItem = items.find((item) => item.id === activeId); if (!activeItem) return items; if (!itemTextStartsWith(activeItem, char)) return items; const { chars } = typeaheadState; if (chars !== char && itemTextStartsWith(activeItem, chars)) return items; typeaheadState.chars = char; return _FHQLDZSWcjs.flipItems.call(void 0, items.filter((item) => itemTextStartsWith(item, char)), activeId).filter((item) => item.id !== activeId); } var useCompositeTypeahead = _LZ6L3ECGcjs.createHook.call(void 0, function useCompositeTypeahead2({ store, typeahead = true, ...props }) { const context = _FHQLDZSWcjs.useCompositeScopedContext.call(void 0, ); store = store || context; _LZ6L3ECGcjs.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "CompositeTypeahead must be a Composite component"); const onKeyDownCaptureProp = props.onKeyDownCapture; const onKeyDownCapture = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => { onKeyDownCaptureProp == null ? void 0 : onKeyDownCaptureProp(event); if (event.defaultPrevented) return; if (!typeahead) return; if (!store) return; const typeaheadState = getTypeaheadState(store); if (!isValidTypeaheadEvent(event, typeaheadState)) return clearChars(typeaheadState); const { renderedItems, items, activeId, id } = store.getState(); let enabledItems = getEnabledItems(items.length > renderedItems.length ? items : renderedItems); const document = _LZ6L3ECGcjs.getDocument.call(void 0, event.currentTarget); const selector = `[data-offscreen-id="${id}"]`; const offscreenItems = document.querySelectorAll(selector); for (const element of offscreenItems) { if (element.hasAttribute("data-disabled")) continue; if ("disabled" in element && !!element.disabled) continue; enabledItems.push({ id: element.id, element }); } if (offscreenItems.length) enabledItems = _LZ6L3ECGcjs.sortBasedOnDOMPosition.call(void 0, enabledItems, (i) => i.element); if (!isSelfTargetOrItem(event, enabledItems)) return clearChars(typeaheadState); event.preventDefault(); window.clearTimeout(typeaheadState.cleanupTimeout); typeaheadState.cleanupTimeout = window.setTimeout(() => { clearChars(typeaheadState); }, 500); const char = event.key.toLowerCase(); typeaheadState.chars += char; enabledItems = getSameInitialItems({ activeId, char, items: enabledItems, typeaheadState }); const item = enabledItems.find((item2) => itemTextStartsWith(item2, typeaheadState.chars)); if (item) store.move(item.id); else clearChars(typeaheadState); }); props = { ...props, onKeyDownCapture }; return _LZ6L3ECGcjs.removeUndefinedValues.call(void 0, props); }); var CompositeTypeahead = _LZ6L3ECGcjs.forwardRef.call(void 0, function CompositeTypeahead2(props) { return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useCompositeTypeahead(props)); }); exports.useCompositeTypeahead = useCompositeTypeahead; exports.CompositeTypeahead = CompositeTypeahead;