UNPKG

@chakra-ui/react

Version:

Responsive and accessible React UI components built with React and Emotion

117 lines (113 loc) • 3.65 kB
"use strict"; "use client"; 'use strict'; var combobox = require('@ark-ui/react/combobox'); var React = require('react'); var useControllableState = require('../../hooks/use-controllable-state.cjs'); var useLiveRef = require('../../hooks/use-live-ref.cjs'); var splitProps = require('../../utils/split-props.cjs'); const comboboxPropKeys = [ "defaultHighlightedValue", "defaultInputValue", "highlightedValue", "id", "ids", "inputValue", "navigate", "onHighlightChange", "onInputValueChange", "onSelect", "scrollToIndexFn" ]; function useCommand(props) { const { loading: loadingProp, closeOnSelect = true, onClose, inputValue: inputValueProp, defaultInputValue = "", onInputValueChange, command, collection: collectionProp, ...restProps } = props; const collection = command?.collection ?? collectionProp; const loading = loadingProp ?? command?.loading; const [inputValue, setInputValue] = useControllableState.useControllableState({ value: command ? command.query : inputValueProp, defaultValue: defaultInputValue }); const onSelect = restProps.onSelect; const runSelect = (details, item) => { if (typeof item?.onSelect === "function") item.onSelect(details); onSelect?.(details); if (command?.onSelect(details)) return; const shouldClose = closeOnSelect && !hasModifierKey(details.modifiers); if (item?.closeOnSelect ?? shouldClose) onClose?.(); }; const combobox$1 = combobox.useCombobox({ inputBehavior: "autohighlight", selectionBehavior: "preserve", loopFocus: false, defaultHighlightedValue: collection.firstValue, ...restProps, collection, loading: !!loading, inputValue, onInputValueChange(details) { setInputValue(details.inputValue); command?.search(details.inputValue); onInputValueChange?.(details); }, open: true, disableLayer: true, onSelect(details) { runSelect(details, collection.find(details.itemValue)); }, value: [] }); useRestoreHighlight(combobox$1, command?.highlightedValue ?? null); const selectItem = (item, options = {}) => { const itemValue = collection.getItemValue(item) ?? ""; const modifiers = options.modifiers ?? NO_MODIFIERS; runSelect({ itemValue, value: [], modifiers }, item); }; return { loading, command, combobox: combobox$1, selectItem }; } const NO_MODIFIERS = { metaKey: false, ctrlKey: false, shiftKey: false, altKey: false }; const hasModifierKey = (modifiers) => modifiers.metaKey || modifiers.ctrlKey || modifiers.shiftKey || modifiers.altKey; function useRestoreHighlight(combobox, preferred) { const { collection, highlightedValue } = combobox; const setHighlightRef = useLiveRef.useLiveRef(combobox.setHighlightValue); const preferredRef = React.useRef(preferred); const pendingRef = React.useRef(null); React.useEffect(() => { if (preferred !== preferredRef.current) { preferredRef.current = preferred; pendingRef.current = preferred; } const pending = pendingRef.current; if (pending && pending !== highlightedValue && collection.has(pending)) { const frame = requestAnimationFrame( () => setHighlightRef.current(pending) ); return () => cancelAnimationFrame(frame); } pendingRef.current = null; }, [collection, preferred, highlightedValue, setHighlightRef]); } const splitCommandProps = splitProps.createSplitProps([ ...comboboxPropKeys, "collection", "command", "loading", "closeOnSelect", "onClose" ]); exports.splitCommandProps = splitCommandProps; exports.useCommand = useCommand;