@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.
117 lines (111 loc) • 3.67 kB
JavaScript
;
'use client';
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hasNullItemLabel = hasNullItemLabel;
exports.isGroupedItems = isGroupedItems;
exports.resolveMultipleLabels = resolveMultipleLabels;
exports.resolveSelectedLabel = resolveSelectedLabel;
exports.stringifyAsLabel = stringifyAsLabel;
exports.stringifyAsValue = stringifyAsValue;
var React = _interopRequireWildcard(require("react"));
var _serializeValue = require("./serializeValue");
var _jsxRuntime = require("react/jsx-runtime");
function isGroupedItems(items) {
return items != null && items.length > 0 && typeof items[0] === 'object' && items[0] != null && 'items' in items[0];
}
/**
* Checks if the items array contains an item with a null value that has a non-null label.
*/
function hasNullItemLabel(items) {
if (!Array.isArray(items)) {
return items != null && !('null' in items);
}
if (isGroupedItems(items)) {
for (const group of items) {
for (const item of group.items) {
if (item && item.value == null && item.label != null) {
return true;
}
}
}
return false;
}
for (const item of items) {
if (item && item.value == null && item.label != null) {
return true;
}
}
return false;
}
function stringifyAsLabel(item, itemToStringLabel) {
if (itemToStringLabel && item != null) {
return itemToStringLabel(item) ?? '';
}
if (item && typeof item === 'object') {
if ('label' in item && item.label != null) {
return String(item.label);
}
if ('value' in item) {
return String(item.value);
}
}
return (0, _serializeValue.serializeValue)(item);
}
function stringifyAsValue(item, itemToStringValue) {
if (itemToStringValue && item != null) {
return itemToStringValue(item) ?? '';
}
if (item && typeof item === 'object' && 'value' in item && 'label' in item) {
return (0, _serializeValue.serializeValue)(item.value);
}
return (0, _serializeValue.serializeValue)(item);
}
function resolveSelectedLabel(value, items, itemToStringLabel) {
function fallback() {
return stringifyAsLabel(value, itemToStringLabel);
}
if (itemToStringLabel && value != null) {
return itemToStringLabel(value);
}
// Custom object with explicit label takes precedence
if (value && typeof value === 'object' && 'label' in value && value.label != null) {
return value.label;
}
// Items provided as plain record map
if (items && !Array.isArray(items)) {
return items[value] ?? fallback();
}
// Items provided as array (flat or grouped)
if (Array.isArray(items)) {
const flatItems = isGroupedItems(items) ? items.flatMap(g => g.items) : items;
if (value == null || typeof value !== 'object') {
const match = flatItems.find(item => item.value === value);
if (match && match.label != null) {
return match.label;
}
return fallback();
}
// Object without explicit label: try matching by its `value` property
if ('value' in value) {
const match = flatItems.find(item => item && item.value === value.value);
if (match && match.label != null) {
return match.label;
}
}
}
return fallback();
}
function resolveMultipleLabels(values, items, itemToStringLabel) {
return values.reduce((acc, value, index) => {
if (index > 0) {
acc.push(', ');
}
acc.push(/*#__PURE__*/(0, _jsxRuntime.jsx)(React.Fragment, {
children: resolveSelectedLabel(value, items, itemToStringLabel)
}, index));
return acc;
}, []);
}