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ACtC,OAAO;IAGT,OAAO;AACT","sources":["packages/react-aria/src/selection/useTypeSelect.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {DOMAttributes, Key, KeyboardDelegate} from '@react-types/shared';\nimport {getEventTarget, nodeContains} from '../utils/shadowdom/DOMFunctions';\nimport {KeyboardEvent, useEffect, useRef} from 'react';\nimport {MultipleSelectionManager} from 'react-stately/useMultipleSelectionState';\n\n/**\n * Controls how long to wait before clearing the typeahead buffer.\n */\nconst TYPEAHEAD_DEBOUNCE_WAIT_MS = 1000; // 1 second\n\nexport interface AriaTypeSelectOptions {\n /**\n * A delegate that returns collection item keys with respect to visual layout.\n */\n keyboardDelegate: KeyboardDelegate;\n /**\n * An interface for reading and updating multiple selection state.\n */\n selectionManager: MultipleSelectionManager;\n /**\n * Called when an item is focused by typing.\n */\n onTypeSelect?: (key: Key) => void;\n}\n\nexport interface TypeSelectAria {\n /**\n * Props to be spread on the owner of the options.\n */\n typeSelectProps: DOMAttributes;\n}\n\n/**\n * Handles typeahead interactions with collections.\n */\nexport function useTypeSelect(options: AriaTypeSelectOptions): TypeSelectAria {\n let {keyboardDelegate, selectionManager, onTypeSelect} = options;\n let state = useRef<{search: string; timeout: ReturnType<typeof setTimeout> | undefined}>({\n search: '',\n timeout: undefined\n });\n\n let onKeyDownCapture = (e: KeyboardEvent) => {\n // if we're in the middle of a search, then a spacebar should be treated as a search and we should not propagate the event\n // since we handle this one in a capture phase, we should ignore it in the bubble phase\n if (state.current.search.length > 0 && e.key === ' ') {\n e.preventDefault();\n if (\n !('continuePropagation' in e) ||\n ('continuePropagation' in e && !e.isPropagationStopped())\n ) {\n e.stopPropagation();\n }\n state.current.search += ' ';\n\n if (keyboardDelegate.getKeyForSearch != null) {\n // Use the delegate to find a key to focus.\n // Prioritize items after the currently focused item, falling back to searching the whole list.\n let key = keyboardDelegate.getKeyForSearch(\n state.current.search,\n selectionManager.focusedKey\n );\n\n // If no key found, search from the top.\n if (key == null) {\n key = keyboardDelegate.getKeyForSearch(state.current.search);\n }\n\n if (key != null) {\n selectionManager.setFocusedKey(key);\n if (onTypeSelect) {\n onTypeSelect(key);\n }\n }\n }\n\n clearTimeout(state.current.timeout);\n state.current.timeout = setTimeout(() => {\n state.current.search = '';\n }, TYPEAHEAD_DEBOUNCE_WAIT_MS);\n }\n };\n\n let onKeyDown = (e: KeyboardEvent) => {\n let character = getStringForKey(e.key);\n if (\n !character ||\n e.ctrlKey ||\n e.metaKey ||\n e.altKey ||\n !nodeContains(e.currentTarget, getEventTarget(e) as HTMLElement) ||\n (state.current.search.length === 0 && character === ' ')\n ) {\n return;\n }\n\n state.current.search += character;\n\n if (keyboardDelegate.getKeyForSearch != null) {\n // Use the delegate to find a key to focus.\n // Prioritize items after the currently focused item, falling back to searching the whole list.\n let key = keyboardDelegate.getKeyForSearch(state.current.search, selectionManager.focusedKey);\n\n if (key == null) {\n key = keyboardDelegate.getKeyForSearch(state.current.search);\n }\n\n if (key != null) {\n selectionManager.setFocusedKey(key);\n if (onTypeSelect) {\n onTypeSelect(key);\n }\n e.preventDefault();\n if (!('continuePropagation' in e)) {\n e.stopPropagation();\n }\n } else {\n // if still nothing then the type to select is done and everything is reset\n state.current.search = '';\n clearTimeout(state.current.timeout);\n state.current.timeout = undefined;\n return;\n }\n }\n\n clearTimeout(state.current.timeout);\n state.current.timeout = setTimeout(() => {\n state.current.search = '';\n }, TYPEAHEAD_DEBOUNCE_WAIT_MS);\n };\n\n useEffect(() => {\n let timeout = state.current.timeout;\n return () => {\n clearTimeout(timeout);\n };\n }, [state]);\n\n return {\n typeSelectProps: {\n // Using a capturing listener to catch the keydown event before\n // other hooks in order to handle the Spacebar event.\n onKeyDownCapture: keyboardDelegate.getKeyForSearch ? onKeyDownCapture : undefined,\n onKeyDown: keyboardDelegate.getKeyForSearch ? onKeyDown : undefined\n }\n };\n}\n\nfunction getStringForKey(key: string) {\n // If the key is of length 1, it is an ASCII value.\n // Otherwise, if there are no ASCII characters in the key name,\n // it is a Unicode character.\n // See https://www.w3.org/TR/uievents-key/\n if (key.length === 1 || !/^[A-Z]/i.test(key)) {\n return key;\n }\n\n return '';\n}\n"],"names":[],"version":3,"file":"useTypeSelect.mjs.map"}