@base-ui/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.
44 lines (42 loc) • 1.79 kB
JavaScript
'use client';
import * as React from 'react';
import { useStore } from '@base-ui/utils/store';
import { useComboboxRootContext } from "../root/ComboboxRootContext.js";
import { resolveMultipleLabels, resolveSelectedLabel } from "../../utils/resolveValueLabel.js";
import { selectors } from "../store.js";
/**
* The current value of the combobox.
* Doesn't render its own HTML element.
*
* Documentation: [Base UI Combobox](https://base-ui.com/react/components/combobox)
*/
import { jsx as _jsx } from "react/jsx-runtime";
export function ComboboxValue(props) {
const {
children: childrenProp,
placeholder
} = props;
const store = useComboboxRootContext();
const itemToStringLabel = useStore(store, selectors.itemToStringLabel);
const selectedValue = useStore(store, selectors.selectedValue);
const items = useStore(store, selectors.items);
const multiple = useStore(store, selectors.selectionMode) === 'multiple';
const hasSelectedValue = useStore(store, selectors.hasSelectedValue);
const shouldCheckNullItemLabel = !hasSelectedValue && placeholder != null && childrenProp == null;
const hasNullLabel = useStore(store, selectors.hasNullItemLabel, shouldCheckNullItemLabel);
let children = null;
if (typeof childrenProp === 'function') {
children = childrenProp(selectedValue);
} else if (childrenProp != null) {
children = childrenProp;
} else if (!hasSelectedValue && placeholder != null && !hasNullLabel) {
children = placeholder;
} else if (multiple && Array.isArray(selectedValue)) {
children = resolveMultipleLabels(selectedValue, items, itemToStringLabel);
} else {
children = resolveSelectedLabel(selectedValue, items, itemToStringLabel);
}
return /*#__PURE__*/_jsx(React.Fragment, {
children: children
});
}