@chakra-ui/react
Version:
Responsive and accessible React UI components built with React and Emotion
114 lines (111 loc) • 3.53 kB
JavaScript
;
"use client";
import { useCombobox } from '@ark-ui/react/combobox';
import { useRef, useEffect } from 'react';
import { useControllableState } from '../../hooks/use-controllable-state.js';
import { useLiveRef } from '../../hooks/use-live-ref.js';
import { createSplitProps } from '../../utils/split-props.js';
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({
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 = 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, 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, 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(combobox.setHighlightValue);
const preferredRef = useRef(preferred);
const pendingRef = useRef(null);
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 = createSplitProps([
...comboboxPropKeys,
"collection",
"command",
"loading",
"closeOnSelect",
"onClose"
]);
export { splitCommandProps, useCommand };