'use client'; import { Combobox as ComboboxPrimitive, Select as SelectPrimitive } from '@base-ui/react'; import { cx } from 'class-variance-authority'; import { ReactNode, useLayoutEffect } from 'react'; import { Checkbox } from '../checkbox'; import { getMatch } from '../menu/utils'; import { Text } from '../text'; import styles from './select.module.css'; import { useSelectContext } from './select-root'; export interface SelectItemProps extends SelectPrimitive.Item.Props { leadingIcon?: ReactNode; trailingIcon?: ReactNode; } export function SelectItem({ className, children, value: providedValue, leadingIcon, trailingIcon, disabled, ...props }: SelectItemProps) { const value = String(providedValue); const { registerItem, unregisterItem, autocomplete, inputValue, value: selectValue, shouldFilter, hasItems, multiple } = useSelectContext(); const isSelected = multiple ? selectValue?.includes(value) : value === selectValue; const isMatched = getMatch(value, children, inputValue); const isHidden = shouldFilter && !hasItems && isSelected && !isMatched; const element = typeof children === 'string' ? ( <> {leadingIcon && (
{leadingIcon}
)} {autocomplete ? ( {children} ) : ( } className={styles.itemText} data-slot='select-item-text' > {children} )} {trailingIcon && (
{trailingIcon}
)} ) : ( children ); useLayoutEffect(() => { registerItem({ leadingIcon, children, value }); return () => { unregisterItem(value); }; }, [value, children, registerItem, unregisterItem, leadingIcon]); if (shouldFilter && !hasItems && !isMatched && !isSelected) { return null; } const ItemPrimitive = autocomplete ? ComboboxPrimitive.Item : SelectPrimitive.Item; return ( (
{multiple && } {element}
)} /> ); } SelectItem.displayName = 'Select.Item';