UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

46 lines (44 loc) 1.69 kB
'use client'; import * as React from 'react'; import { useStore } from '@base-ui-components/utils/store'; import { useRenderElement } from "../../utils/useRenderElement.js"; import { ComboboxChipsContext } from "./ComboboxChipsContext.js"; import { CompositeList } from "../../composite/list/CompositeList.js"; import { useComboboxRootContext } from "../root/ComboboxRootContext.js"; import { selectors } from "../store.js"; /** * A container for the chips in a multiselectable input. * Renders a `<div>` element. */ import { jsx as _jsx } from "react/jsx-runtime"; export const ComboboxChips = /*#__PURE__*/React.forwardRef(function ComboboxChips(componentProps, forwardedRef) { const { render, className, ...elementProps } = componentProps; const store = useComboboxRootContext(); const open = useStore(store, selectors.open); const [highlightedChipIndex, setHighlightedChipIndex] = React.useState(undefined); if (open && highlightedChipIndex !== undefined) { setHighlightedChipIndex(undefined); } const chipsRef = React.useRef([]); const element = useRenderElement('div', componentProps, { ref: [forwardedRef, store.state.chipsContainerRef], props: elementProps }); const contextValue = React.useMemo(() => ({ highlightedChipIndex, setHighlightedChipIndex, chipsRef }), [highlightedChipIndex, setHighlightedChipIndex, chipsRef]); return /*#__PURE__*/_jsx(ComboboxChipsContext.Provider, { value: contextValue, children: /*#__PURE__*/_jsx(CompositeList, { elementsRef: chipsRef, children: element }) }); }); if (process.env.NODE_ENV !== "production") ComboboxChips.displayName = "ComboboxChips";