package.dist.components.combobox.use-combobox.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of react Show documentation
Show all versions of react Show documentation
A collection of unstyled, accessible UI components for React, utilizing state machines for seamless interaction.
The newest version!
'use client';
import * as combobox from '@zag-js/combobox';
import { useMachine, normalizeProps } from '@zag-js/react';
import { useId, useEffect } from 'react';
import { useEnvironmentContext } from '../../providers/environment/use-environment-context.js';
import { useLocaleContext } from '../../providers/locale/use-locale-context.js';
import { useEvent } from '../../utils/use-event.js';
import { useFieldContext } from '../field/use-field-context.js';
const useCombobox = (props) => {
const { collection, ...comboboxProps } = props;
const { dir } = useLocaleContext();
const { getRootNode } = useEnvironmentContext();
const field = useFieldContext();
const initialContext = {
id: useId(),
ids: {
label: field?.ids.label,
input: field?.ids.control
},
disabled: field?.disabled,
readOnly: field?.readOnly,
required: field?.required,
invalid: field?.invalid,
dir,
getRootNode,
collection,
open: props.defaultOpen,
value: props.defaultValue,
"open.controlled": props.open !== void 0,
...comboboxProps
};
const context = (() => {
const { collection: _, ...restProps } = initialContext;
return {
...restProps,
value: props.value,
onValueChange: useEvent(props.onValueChange),
onInputValueChange: useEvent(props.onInputValueChange, { sync: true }),
onHighlightChange: useEvent(props.onHighlightChange),
onOpenChange: useEvent(props.onOpenChange)
};
})();
const [state, send, service] = useMachine(combobox.machine(initialContext), {
context
});
useEffect(() => {
service.setContext({ collection });
}, [collection]);
return combobox.connect(state, send, normalizeProps);
};
export { useCombobox };
© 2015 - 2025 Weber Informatics LLC | Privacy Policy