UNPKG

@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.

48 lines (45 loc) 1.88 kB
import { stringifyAsLabel } from "../../../internals/resolveValueLabel.mjs"; /** * Derives the default id assigned to `Combobox.Popup` when the input is rendered inside it. * Shared by the popup (which applies it) and the trigger (which references it via `aria-controls`) * so the convention only lives in one place. */ export function getComboboxPopupId(rootId) { return rootId == null ? undefined : `${rootId}-popup`; } /** * Enhanced filter using Intl.Collator for more robust string matching. * Uses the provided `itemToStringLabel` function if available, otherwise falls back to: * • When `item` is an object with a `value` property, that property is used. * • When `item` is a primitive (e.g. `string`), it is used directly. */ export function createCollatorItemFilter(collatorFilter, itemToStringLabel) { return (item, query) => { if (item == null) { return false; } const itemString = stringifyAsLabel(item, itemToStringLabel); return collatorFilter.contains(itemString, query); }; } /** * Enhanced filter for single selection mode using Intl.Collator that shows all items * when query is empty or matches the current selection, making it easier to browse options. */ export function createSingleSelectionCollatorFilter(collatorFilter, itemToStringLabel, selectedValue) { return (item, query) => { if (item == null) { return false; } if (!query) { return true; } const itemString = stringifyAsLabel(item, itemToStringLabel); const selectedString = selectedValue != null ? stringifyAsLabel(selectedValue, itemToStringLabel) : ''; // Handle case-insensitive matching consistently if (selectedString && collatorFilter.contains(selectedString, query) && selectedString.length === query.length) { return true; } return collatorFilter.contains(itemString, query); }; }